我正在尝试唤醒一个线程队列来处理一些数据,除非它没有醒来。不确定我的方法是否有缺陷。这是我的代码:
主:
struct ThreadOperationStruct _THREADIO;
int main(int argc, char** argv)
{
// threads get created here...
}
我的等待线程:
void DecompressLogFile::Wait()
{
while (!_THREADIO.EndEvent)
{
pthread_mutex_lock(&Lock);
size_t QueueSize = _THREADIO.DecompressLogQueue.size();
if (!QueueSize)
{
// Prevent further thread execution
fprintf(stdout, "DecompressLogFile() thread sleeping...\n");
pthread_cond_wait(&_THREADIO.DecompressQueueNotify, &Lock);
} else {
fprintf(stdout, "Processing compressed log files. Queue size is now %lu\n", QueueSize);
/*
* Async tasks need to go here...
*/
//BZIP2_DecompressFile();
_THREADIO.DecompressLogQueue.pop();
}
}
fprintf(stdout, "Wait() end\n");
}
另一个线程上的另一个类:
_THREADIO.DecompressLogQueue.push(NewLog);
pthread_cond_signal(&_THREADIO.DecompressQueueNotify);
struct
看起来像这样:
struct ThreadOperationStruct
{
std::queue<std::string> DecompressLogQueue;
std::queue<std::string> ProcessLogQueue;
void NotifyDecompressThread()
{
fprintf(stdout, "notifying..\n");
pthread_cond_signal(&this->DecompressQueueNotify);
}
pthread_cond_t DecompressQueueNotify;
pthread_cond_t ProcessQueueNotify;
bool EndEvent = false;
};
extern ThreadOperationStruct _THREADIO;
现在,这个问题与我上面的问题无关......但我只有在Windows上使用C ++ 11线程而不是在Linux上使用C ++ 11线程的经验。我已经读过可移植性,我应该继续使用Linux上的pthread。但这对C ++ 14来说是否适用?
由于