Rodrigo Rutsatz
Rodrigo Rutsatz

Reputation: 285

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. What exacly happens in this situation?

EDIT: I did some debugging, and the problme is Joinable method.

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. 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.

Upvotes: 0

Views: 1046

Answers (2)

Jonathan Wakely
Jonathan Wakely

Reputation: 171413

A thread cannot join() itself, but there's nothing wrong with a thread calling joinable() on itself.

All t.joinable() does is test t.get_id() != std::thread::id{} so it makes no difference which thread you call it from.

Upvotes: 1

Rodrigo Rutsatz
Rodrigo Rutsatz

Reputation: 285

Cameron was right, my mistake was elsewhere in the code, so that those functions are not necessary and the locking gets done properly.

@πάντα ῥεῖ: 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...

Thanks guys

Upvotes: 0

Related Questions