Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unlock a thread from another thread in C++11

I'm not extensively knowledgeable in terms on the thread.h api of the C++11 (in fact I'm not familiar with thread progranmming either, but I read on it recently quite a lot and know about the concurrency and stuff like that) but I started using it and I'm facing a problem that I haven't really ever faced yet.

I have two thread functions, say these

std::thread(thread1, args); // Spawn thread 1
std::thread(thread2, args); // Spawn thread 2

[...]

int thread1(bunch of args)
{
     lock_thread_2();
     [do stuff]
     while (some condition) {
         [do stuff]
         unlock_thread_2();
     }
}

int thread2(bunch of args)
{
     while (some condition) {
         [do stuff]
         wait_for_thread1_to_unlock_me();
     }
}

I first thought about doing it with a std::mutex but I read it could be dangerous because the behavior is undefined if I unlock an already-unlocked mutex and on top of it it wouldn't work anyways as mutex.lock() doesn't necessarily pause the execution (it only does if the mutex is already locked) so it'd be quite horrendous to write, I'd have to couple unlock() and lock() calls together.

What's important to note here is that thread2's execution only is controlled by thread1, but thread2 will never lock thread1 in any fashion. Only thread2 gets locked by thread1, only thread1 controls the execution flow thread2, not otherwise.

How would you do that in a clean way, supported way? Would you be kind enough to give an example of code?

Thanks!

like image 266
Yannick Avatar asked May 09 '15 20:05

Yannick


People also ask

What is the difference between Unique_lock and Lock_guard?

A lock_guard always holds a lock from its construction to its destruction. A unique_lock can be created without immediately locking, can unlock at any point in its existence, and can transfer ownership of the lock from one instance to another.

Can multiple threads read the same array?

Would multiple threads each modifying a different array element be writing to the same location in memory? The answer is no. Each array element has a region of memory reserved for it alone within the region attributed the overall array.

Can you unlock mutex from different thread?

A normal mutex cannot be locked repeatedly by the owner. Attempts by a thread to relock an already held mutex, or to lock a mutex that was held by another thread when that thread terminated, cause a deadlock condition. A recursive mutex can be locked repeatedly by the owner.

What is std :: Unique_lock?

std::unique_lock The class unique_lock is a general-purpose mutex ownership wrapper allowing deferred locking, time-constrained attempts at locking, recursive locking, transfer of lock ownership, and use with condition variables.


Video Answer


1 Answers

Use a condition_variable:

std::condition_variable cv;

int thread1(bunch of args)
{
     [do stuff]
     while (some condition) {
         [do stuff]
         cv.notify_one();
     }
}

int thread2(bunch of args)
{
     std::mutex mtx;
     std::unique_lock<std::mutex> lk(mtx);

     while (some condition) {
         [do stuff]
         cv.wait(lk);
     }
}

When wait() returns, either cv will have been notify()-ed... or there will have been a spurious wakeup. In an effort to handle the latter, it's often helpful to add a predicate.

like image 126
Barry Avatar answered Oct 09 '22 00:10

Barry