Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to mix boost::thread with C++11 std::mutex?

Is it safe to use std::mutex and its kin in a program that starts its threads via boost?

(Using std::thread is not an option for me (I think), as the application needs a lot of stack space, and on some platforms requires overriding the default stack size upon creation.)

like image 643
Christoph Lipka Avatar asked Feb 15 '19 21:02

Christoph Lipka


People also ask

Is a std mutex thread safe?

Shared mutexes and locks are an optimization for read-only pieces of multi-threaded code. It is totally safe for multiple threads to read the same variable, but std::mutex can not be locked by multiple threads simultaneously, even if those threads only want to read a value.

Can two threads share a mutex?

Mutex is an abbreviation for mutual exclusion, as in, a mutex allows only one thread to access some data at any given time.

How to add mutex in C++?

In C++, we create a mutex by constructing an instance of std::mutex, lock it with a call to the member function lock(), and unlock it with a call to the member function unlock().

Does boost use Pthread?

Boost. Sync supports building against one of the following underlying APIs: pthreads or Windows API. On most platforms pthreads are the only option. On Windows, Windows API is used by default, but pthreads can be enabled.


1 Answers

Yes, you can use std::mutex in threads created with boost::thread.

like image 83
Anthony Williams Avatar answered Oct 13 '22 19:10

Anthony Williams