gpt4 book ai didi

c++ - boost 获取锁失败

转载 作者:行者123 更新时间:2023-11-30 03:01:38 25 4
gpt4 key购买 nike

我正在开发一个实时音乐应用程序。作为新手使用 boost 库。我使用 protected SyncronizedQueue 实现了生产者/消费者关系,实际上我实现了 https://www.quantnet.com/cplusplus-multithreading-boost/ 的 18.11 和 18.12 部分。 .解析 midi 输入时出现以下异常:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl
<boost::exception_detail::error_info_injector<boost::lock_error> >'
what(): boost::lock_error

producer中解析输入的代码:

RtMidiIn *midiin = new RtMidiIn();
std::vector<unsigned char> message;
int nBytes, i;
double stamp;
m_queue=queue;

// Check available ports.
unsigned int nPorts = midiin->getPortCount();
if ( nPorts == 0 ) {
std::cout << "No ports available!\n";
goto cleanup;
}

midiin->openPort( 1 );

// Don't ignore sysex, timing, or active sensing messages.
midiin->ignoreTypes( false, false, false );

// Install an interrupt handler function.
done = false;
(void) signal(SIGINT, finish);

// Periodically check input queue.
std::cout << "Reading MIDI from port ... quit with Ctrl-C.\n";
while ( !done ) {
stamp = midiin->getMessage( &message );
nBytes = message.size();
if(nBytes>0){
if((message.size()!=1)&&((int)message.at(0)!=137)){
for ( i=0; i<nBytes; i++ )
std::cout << "Byte " << i << " = " << (int)message[i] << ", ";
if ( nBytes > 0 )
std::cout << "stamp = " << stamp << std::endl;
m_queue->Enqueue("asd");
}
}
}

它在第一次遇到时就崩溃了:

m_queue->Enqueue("asd");

尝试执行时:

boost::unique_lock<boost::mutex> lock(m_mutex);

感谢任何帮助!

编辑1:

这是 SynchronizedQueue 对象。调用 Enqueue() 时抛出异常。

template <typename T>
class SynchronizedQueue
{
private:
std::queue<T> m_queue; // Use STL queue to store data
mutable boost::mutex m_mutex; // The mutex to synchronise on
boost::condition_variable m_cond; // The condition to wait for

public:

// Add data to the queue and notify others
void Enqueue(const T& data)
{
// Acquire lock on the queue
boost::unique_lock<boost::mutex> lock(m_mutex);
// Add the data to the queue
m_queue.push(data);
// Notify others that data is ready
m_cond.notify_one();
} // Lock is automatically released here

// Get data from the queue. Wait for data if not available
T Dequeue()
{

// Acquire lock on the queue
boost::unique_lock<boost::mutex> lock(m_mutex);
//lock();
// When there is no data, wait till someone fills it.
// Lock is automatically released in the wait and obtained
// again after the wait
while (m_queue.size()==0) m_cond.wait(lock);

// Retrieve the data from the queue
T result=m_queue.front(); m_queue.pop();
return result;
} // Lock is automatically released here
};

最佳答案

大多数时候,当我遇到这样的问题时,是因为被锁定的互斥量已经被破坏或尚未构建。您确定,当解析器线程开始解析时,您传递给解析器的队列已经构建好了吗?或者可能是,包含队列作为局部变量的循环的退出停止了?也许您可以进行快速测试并向队列中添加一个 d'tor 并在那里设置一个断点。

问候托尔斯滕

关于c++ - boost 获取锁失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10900566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com