简体   繁体   English

如果std :: thread自身调用可联接,会发生什么?

[英]What happens if a std::thread calls joinable on itself?

I am having problems since I made changes in my program and it might be due to a thread calling joinable on itself. 由于我在程序中进行了更改,因此出现了问题,这可能是由于线程本身调用了joinable。 What exacly happens in this situation? 在这种情况下会发生什么?

EDIT: I did some debugging, and the problme is Joinable method. 编辑:我做了一些调试,问题是Joinable方法。

std::mutex threadMutex;
std::thread tAudioProcessingThread;

void getLock()
{
    if (tAudioProcessingThread.joinable())
        threadMutex.lock(); 
}

void releaseLock()
{
    if (tAudioProcessingThread.joinable())
        threadMutex.unlock();   
}  

The functions getLock() and releaseLock() are called from the two existing threads. 从两个现有线程中调用函数getLock()releaseLock() I had problems calling the threadMutex.lock() and threadMutex.unlock() functions before the thread was created, so I had to make these alternative functions, so that the locks only get called when the thread exists. 在创建线程之前,我在调用threadMutex.lock()threadMutex.unlock()函数时遇到问题,因此我必须制作这些替代函数,以便仅在线程存在时调用锁。

A thread cannot join() itself, but there's nothing wrong with a thread calling joinable() on itself. 线程本身无法join() ,但是线程本身调用joinable()并没有错。

All t.joinable() does is test t.get_id() != std::thread::id{} so it makes no difference which thread you call it from. t.joinable()所做的全部工作就是测试t.get_id() != std::thread::id{}因此从哪个线程调用它没有区别。

Cameron was right, my mistake was elsewhere in the code, so that those functions are not necessary and the locking gets done properly. Cameron是正确的,我的错误是代码中的其他地方,因此这些功能不是必需的,并且锁定已正确完成。

@πάντα ῥεῖ: I had locks on functions that were called before and after the creation of the thread, so I had to put the locks there. @πάνταῥεῖ:我在创建线程之前和之后调用的函数上有锁,所以我不得不将锁放在那里。

PS: Can't comment yet, thats why there are so many edits and so on... PS:现在还不能发表评论,这就是为什么编辑如此之多等等的原因。

Thanks guys 多谢你们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM