a lock guard locks the mutex when it is constructed and unlocks it when it leaves scope (when the destructor is called). So the protected range is up to the '}' after print_even(id+1).
By the way, the reference pages on this site are excellent.
In your example the second thread would wait until print_even(id2); is done.
Note that if print_even(...) tries to lock mtx again it will result in a deadlock (since mutex is not recursive).
If you want to protect the variable id1 only you may write it like so:
1 2 3 4 5 6 7 8
void print_thread_id (int & id1, int & id2) {
{ // Note this limits the scope for mtx
std::lock_guard<std::mutex> lck (mtx);
id1++;
} // The execution of the following functions are not protected (which improves parallelism)
print_even(id1);
print_even(id2);
}
Note that a variable needs protection only if it is in fact shared. In your case each thread could provide its own variable which would make the protection useless.
You mentioned "No. There's no connection between a variable and the mutex. " So then what mutex lock?
A mutex does not actually lock anything except itself. It is used by programmers to isolate critical sections of code so that multiple parallel threads cannot be in a a critical section at the same time. Usually critical sections involve modifying one or more variables that are shared across threads. However, this requires developer intuition--the mutex is not explicitly associated with a variable.
You could have one thread obtain a mutex protecting a variable and have another thread modify that variable without obtaining the mutex. In this case the mutex did not do its job because the programmer failed to identify all of the critical sections of code. In other words, a mutex is a tool used by a developer to protect access to a variable, not a safe access to the variable in and of itself.
In this example, if I run functions numPlus and numMinus in two threads, then the variable "num" will not be protected because numMinus() even doesn't have mtx inside it. So it will modfiy num no matter whether numPlus is excuting or not, correct?