The scope of the lock depends on the scope of your lock, so in your test program, the main scope is locked. Of course, your sample program itself is meaningless because it is not used in multi-threading. Assume it is multi-threaded. At this time, other threads need to wait for your mutex lock to be released when trying to lock the mutex, which means that only one thread can access variables a and c at a certain time.
After locking, the mutex lock is held unless the current thread releases it. When the current thread acquires the lock, it has already completed executing this line of code and entered the next line of execution, so it is qualified to release the mutex lock. Other threads waiting for the lock will sleep in this line of code before the lock is released, until they are awakened from the waiting queue by the operating system.
Mutex lock controls resource access. Entering the critical section requires obtaining a lock. At this time, the thread has the power to consume resources, that is, to modify the shared data. Exiting the critical section requires releasing the lock and releasing the lock to other threads to continue execution.
To put it simply, no. Mutex is a kind of lock. It only ensures that one thread can lock it at the same time, but how to control the global variables is implemented by your own code. That is to say, it is an agreement that if another thread does not try to lock the Mutex first, but directly modifies those global variables, it will not be blocked.
The scope of the lock depends on the scope of your lock, so in your test program, the main scope is locked.
Of course, your sample program itself is meaningless because it is not used in multi-threading. Assume it is multi-threaded. At this time, other threads need to wait for your mutex lock to be released when trying to lock the mutex, which means that only one thread can access variables a and c at a certain time.
After locking, the mutex lock is held unless the current thread releases it. When the current thread acquires the lock, it has already completed executing this line of code and entered the next line of execution, so it is qualified to release the mutex lock. Other threads waiting for the lock will sleep in this line of code before the lock is released, until they are awakened from the waiting queue by the operating system.
Mutex lock controls resource access. Entering the critical section requires obtaining a lock. At this time, the thread has the power to consume resources, that is, to modify the shared data. Exiting the critical section requires releasing the lock and releasing the lock to other threads to continue execution.
To put it simply, no. Mutex is a kind of lock. It only ensures that one thread can lock it at the same time, but how to control the global variables is implemented by your own code. That is to say, it is an agreement that if another thread does not try to lock the Mutex first, but directly modifies those global variables, it will not be blocked.