Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a C++11 mutex compatible with threads NOT created with C++11?

I'm learning C++11 and have run into a threading issue. My general question: are C++11 mutexes compatible with threads not created with C++11's standard libraries?

I would like to safely share information between a thread created with C++11 and another thread created by a third-party library that I have no control over.

For example, my application uses PortAudio, which creates its own thread for audio output. I'm not sure if it's using pthreads, or OS-specific threading libraries, but I do know that PortAudio is NOT written in C++11. I want to safely share data between a GUI thread (using a C++11 thread) and the PortAudio thread using a mutex.

Similarly, can I use a C++11 mutex to synchronize QT QThreads and C++11 threads?

like image 375
mjango Avatar asked Dec 07 '13 18:12

mjango


People also ask

Do mutexes have to be global?

Mutexes are in a sense global, regardless of how you make them available. They're shared between disparate pieces of code. So you can make them globals, or you can pass them down the call chain, through functions that don't use them, until you eventually reach someone who does.

Can two threads share a mutex?

Normally, a mutex is not thread related. It ensures that a critical area is only accessed by a single thread. So if u have some shared areas, like processing the same array by multiple threads, then you must ensure exclusive access for this area. That means, you do not need a mutex for each thread.

How does mutex work in C?

Mutex lock will only be released by the thread who locked it. So this ensures that once a thread has locked a piece of code then no other thread can execute the same region until it is unlocked by the thread who locked it.

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


3 Answers

Are C++11 mutexes compatible with threads not created with C++11's standard libraries?

The C++ standard does not define a "thread" as something exclusively created by the C++ standard library.

1.10 Multi-threaded executions and data races [intro.multithread]

1 A thread of execution (also known as a thread) is a single flow of control within a program, including the initial invocation of a specific top-level function, and recursively including every function invocation subsequently executed by the thread.

So, I would conclude the answer to your question is "yes".

like image 156
chill Avatar answered Oct 18 '22 07:10

chill


Obviously, the C++ standard doesn't make any guarantees about compatebility with other systems. Part of the reason the C and C++ standards added threading facilities was to standardize on one threading system.

In practice it is expected that the C and C++ threads library is built to integrate with a platform threading system if there is one. For example, on platforms using pthreads the expectation is that pthreads are used where appropriate to buildtge standard library threading facilities (as far as I know there is no pthreads interface for the various atomic operations, i.e., the standard library may need to provide its own synchronization primitives).

The standard library classes provide access to the underlying representation through the native_handle() methods. A standard library should implement what is returned from these and, e.g., if pthreads types are provided it seems safe to assume that this particular standard library will play nice with pthreads.

like image 30
Dietmar Kühl Avatar answered Oct 18 '22 07:10

Dietmar Kühl


The C++11 standard specifies that mutexes should work with any kind of 'execution agent', including different thread libraries. Here are some relevant quotes from the standard which I think answer the question conclusively:

Mutex requirements

A mutex object facilitates protection against data races and allows safe synchronization of data between execution agents (30.2.5). An execution agent owns a mutex from the time it successfully calls one of the lock functions until it calls unlock.

Requirements for Lockable types

An execution agent is an entity such as a thread that may perform work in parallel with other execution agents. [Note: Implementations or users may introduce other kinds of agents such as processes or thread-pool tasks. —end note ] The calling agent is determined by context, e.g. the calling thread that contains the call, and so on.

like image 3
karadoc Avatar answered Oct 18 '22 06:10

karadoc