• 6151阅读
  • 2回复

qt多线程问题请教各位 [复制链接]

上一主题 下一主题
离线ljp1205
 
只看楼主 正序阅读 楼主  发表于: 2008-10-23
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
现在自己闲着的时候在动手写一个带界面的录音程序
界面上有一录音按钮跟一停止按钮
点击录音按钮的时候将新起一个线程
这个线程的run()函数的具体代码如下:

    /* i,j为临时变量*/
    int i,j;
    int arg;    /* 用于ioctl调用的参数 */
    int status;  /* 系统调用的返回值 */

    /* 设置适当的参数,使得声音设备工作正常*/
    /* 详细情况请参考Linux关声卡编程的文档*/
    if ( ( id = open ( "/dev/dsp", O_RDONLY) ) < 0 )
    {
        fprintf (stderr, " Can't open sound device!\n");
        exit () ;
    }
    /* 打开输出文件,失败则退出*/
    char *path;
    path = merge_path(current_path,"test.wav");
    if ( ( fd = open (path,O_WRONLY|O_CREAT|O_EXCL,S_IRUSR|S_IWUSR))<0)
    {
        fprintf ( stderr, " Can't open output file!\n");
        close(id);
        exit ();
    }
   
    /* 设置采样时的量化位数 */
    arg = SIZE;
    status = ioctl(id, SOUND_PCM_WRITE_BITS, &arg);
    if (status == -1)
    {
        perror("SOUND_PCM_WRITE_BITS ioctl failed");
        close(fd);
        close(id);
    }
    if (arg != SIZE)
        perror("unable to set sample size");
   
    /* 设置采样时的声道数目 */
    arg = CHANNELS;
    status = ioctl(id,SOUND_PCM_WRITE_CHANNELS,&arg);
    if (status == -1)
    {
        perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
        close(fd);
        close(id);
    }
    if (arg != CHANNELS)
        perror("unable to set number of channels");
   
    /* 设置采样时的采样频率 */
    arg = RATE;
    status = ioctl(id,SOUND_PCM_WRITE_RATE,&arg);
    if (status == -1)
    {
        perror("SOUND_PCM_WRITE_WRITE ioctl failed");
        close(fd);
        close(id);
    }

    /* 读取一定数量的音频数据,并将之写到输出文件中去*/
    for ( j=0;;)
    {
        i=read(id,buf,MAXBUF);
//        int error = errno;
        if(i>0&&!isStop)
        {
            write(fd,buf,i);
            j++;
        }
        else if(j!=0)
        {
            break;
        }
    }
   
    /* 关闭输入、输出文件*/
    close(fd);
    close(id);
//    QThread::exit();

因为我需要在录音的时候随时按下停止按钮能够停止录音
所以我用一个标记isStop来标记是否从录音的死循环中跳出来。
而当停止按钮按下的时候,isStop将被设置成1,从而从跳出录音的循环
。。。。。。。。。。
但是现在出现的问题是经常在停止跟录音之间切换的话将导致程序死在里面
请问是什么原因
离线yj_yulin

只看该作者 2楼 发表于: 2008-10-24
Qt 4.5 brings a new performance benchmarking library, focused re-engineering of key functionality, and a new pluggable graphics system – all aimed at increasing the performance of Qt-based applications.  As most engineering work has been done to the core Qt API, in most cases Qt users need only to upgrade to Qt 4.5 to realize performance benefits immediately.
离线yj_yulin

只看该作者 1楼 发表于: 2008-10-24
你得调试一下看你的程序都在干什么去了


gprof介绍
gprof是GNU profiler工具。可以显示程序运行的“flat profile”,包括每个函数的调用次数,每个函数消耗的处理器时间。也可以显示“调用图”,包括函数的调用关系,每个函数调用花费了多少时间。还可以显示“注释的源代码”,是程序源代码的一个复本,标记有程序中每行代码的执行次数。
快速回复
限100 字节
 
上一个 下一个