Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do mutex's in C++ have to be tied to some object or variable?

I'm somewhat new to threading and I'm trying to understand how it works in C++11. The professor in my class gave us this sample code to demonstrate the use of a mutex:

#include <list> 
#include <mutex> 
#include <algorithm>

std::list<int> some_list; // A data structure accessed by multiple threads
std::mutex some_mutex; // This lock will prevent concurrent access to the shared data structure

void
add_to_list(int new_value) {
    std::lock_guard<std::mutex> guard(some_mutex); // Since I am going to access the shared data struct, acquire the lock
    some_list.push_back(new_value); // Now it is safe to use some_list. RAII automatically releases lock at end of function }
}

bool
list_contains(int value_to_find) {
    std::lock_guard<std::mutex> guard(some_mutex); // Must get lock every time I access some_list return
    std::find (some_list.begin(),some_list.end(),value_to_find) != some_list.end();
}

I think the code is somewhat self-explanatory but I had some specific questions.

  1. Is there no need to specifically associate the mutex with the list?
  2. And if not, does that mean that any time a mutex is used, all threads halt until the mutex is destroyed? Or is it only a subset of threads; perhaps threads in some threadpool or otherwise associated with each other?
  3. And whichever is the case, isn't it better to only halt threads which are attempting to access the data structure? Because otherwise, we're not worried about data races and the like.
  4. Finally, what's the difference between a mutex and a lock? Is a mutex simply an RAII lock? Or is the RAII happening via the guard?
like image 967
limp_chimp Avatar asked Feb 02 '13 19:02

limp_chimp


People also ask

Does a mutex lock all variables?

This is not a question about the scope (global or otherwise) of a "mutex object", it is a question about what scope of variables are "locked" by a mutex. I believe the answer to be that a mutex locks access to all variables, ie; all global and locally scoped variables.

Why a mutex lock is needed when using Pthread condition variable?

The mutex is used to protect the condition variable itself. That's why you need it locked before you do a wait. The wait will "atomically" unlock the mutex, allowing others access to the condition variable (for signalling).

What are mutexes and condition variables and how they are used?

The associated mutex is used to ensure that a condition can be checked atomically, and that the thread can block on the associated condition variable without missing either a change to the condition or a signal that the condition has changed.

Do you need a mutex to read a shared variable?

Unless you use a mutex or another form of memory barrier. So if you want correct behavior, you don't need a mutex as such, and it's no problem if another thread writes to the variable while you're reading it.


1 Answers

  1. The mutex is associated with the list, but this association is completely manual -- the compiler and runtime library do not know that the two are associated. The association exists entirely in your documentation and in your head, and you are responsible for ensuring that any thread which accesses the list locks/acquires the mutex first.

  2. Whenever a mutex is used, the thread which locks/acquires the mutex will halt (the term is actually block) until no other thread owns the mutex. Threads which are not using the mutex will be unaffected.

  3. You are responsible for ensuring that only the threads which access the list lock/acquire the mutex, and you are also responsible for ensuring that all of the threads which access the list lock/acquire the mutex. Again, only those threads can block waiting for the mutex.

  4. The same object goes by a number of different names: "mutex", "lock", or "critical section". The guard uses RAII to lock/acquire the mutex.

like image 135
Dietrich Epp Avatar answered Oct 11 '22 13:10

Dietrich Epp