Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ GUI阻止音频播放,尽管有单独的线程_C++_Multithreading_Qt - Fatal编程技术网

C++ GUI阻止音频播放,尽管有单独的线程

C++ GUI阻止音频播放,尽管有单独的线程,c++,multithreading,qt,C++,Multithreading,Qt,我写了一个由两部分组成的轻型音频播放器 核心:它有一个公共接口和一个作为pimpl实现的私有接口。在pimpl(AudioPlayerPrivate)中,我正在创建音频后端(解码、混合),并将其移动到工作线程 AudioPlayerPrivate::AudioPlayerPrivate(QObject *parent) : QObject(parent) { playerThread_ = new QThread(this); playerCore_

我写了一个由两部分组成的轻型音频播放器

  • 核心:它有一个公共接口和一个作为pimpl实现的私有接口。在pimpl(AudioPlayerPrivate)中,我正在创建音频后端(解码、混合),并将其移动到工作线程

    AudioPlayerPrivate::AudioPlayerPrivate(QObject *parent) :
                 QObject(parent)
    {
        playerThread_ = new QThread(this);
        playerCore_   = new AudioPlayerCore;
           qDebug()<<"AudioPlayerCore thread:"<<playerCore_->thread();
        playerCore_->moveToThread(playerThread_);
           qDebug()<<"AudioPlayerCore thread after moving to playerThread:"<<playerCore_->thread();
        playerThread_->start();
    }
    
    从输出来看,我似乎还没有成功地将核心移动到线程。3个线程调试中只有一个(上面最后3行)实际显示了新线程的ID。 我缺少的线程是否有明显的缺陷

    以下是我如何称呼播放开始(在AudioPlayerPrivate中):


    如何调用onPlayRequest()?我想它是从主线程直接调用的,将对象移动到工作线程没有任何区别。尝试使用(排队的)信号/插槽连接。我对帖子进行了编辑并添加了呼叫。我使用的是
    DirectConnection
    ,在您的建议后,我还尝试了
    QueuedConnection
    。使用
    QueuedConnection
    QThread::currentThread()
    现在显示与
    this->thread()
    相同的线程id(根据我上面的代码0x4a83a8)。但如果我将鼠标按在窗口的X按钮上,播放仍会停止。与此同时,我找到了解决方案。在AudioPlayerCore的构造函数中,我实例化了缓冲区之类的对象。这当然是在我做
    moveToThread
    之前。解决方案:我将
    playerThread\uuuu
    started()-信号连接到AudioPlayerCore的一个插槽,现在我在该插槽中实例化对象(因此它们在
    moveToThread
    之后实例化)。但是为什么他们之前没有移动到线程?
    PlayerGUI thread: 0x4a83a8
    AudioPlayer thread: 0x4a83a8
    AudioPlayerPrivate Constructor
    AudioPlayerPrivate thread (this->thread()): QThread(0x4a83a8)
    AudioPlayerPrivate thread (QCoreApplication::instance()->thread()): QThread(0x4a83a8)
    AudioPlayerPrivate thread (QThread::currentThread): QThread(0x4a83a8)
    AudioPlayerCore Constructor
    AudioPlayerCore thread: QThread(0x4a83a8)
    AudioPlayerCore thread after moving to playerThread: QThread(0x4bb808)
    AudioPlayerPrivate::play()
    AudioPlayerCore::onPlayRequest()
    AudioPlayerCore thread (this->thread()): QThread(0x4bb808)
    AudioPlayerCore thread (QCoreApplication::instance()->thread()): QThread(0x4a83a8)
    AudioPlayerCore thread (QThread::currentThread): QThread(0x4a83a8)
    
    connect(this,SIGNAL(playRequest(InputSource*,bool)),
            playerCore_,SLOT(onPlayRequest(InputSource*,bool)),
            Qt::DirectConnection);
    
    bool startPlaybackImmediately = false;
    emit playRequest(source,startPlaybackImmediately);