简体   繁体   English

C ++,多线程环境中的全局数据行为

[英]Global data behaviour in Multi-thread environment, C++

I have an application running three threads. 我有一个运行三个线程的应用程序。 All three threads are doing some operations based on the status of a global variable. 这三个线程都基于全局变量的状态进行一些操作。 2 of the threads are running the same piece of code and 1 thread is running another piece of code. 其中两个线程正在运行同一段代码,而一个线程正在运行另一段代码。

Ex: 例如:

Initial value of global variable is false 全局变量的初始值为false

gGlobalVarLock = false;

thread 1:
while(true == gGlobalVarLock)
{
  /*wait for flag to become false*/
}
/*after the flag becomes false*/    
{
  mutex.lock();
  gGlobalVarLock = true;

  /*run some code*/

  gGlobalVarLock = false;
  mutex.unlock();
}

thread 2 and 3,    
while(true == gGlobalVarLock)
{
  /*wait for flag to become false*/
}
/*after the flag becomes false*/
{
  mutex.lock();
  gGlobalVarLock = true;

  /*run some other code*/

  gGlobalVarLock = false;
  mutex.unlock();
}

Now, after the application starts due to the init false value of gGlobalVarLock, one of the threads starts execution, and should lock the other threads from executing. 现在,由于gGlobalVarLock的初始false值启动了应用程序之后,其中一个线程开始执行,并应锁定其他线程以使其无法执行。 As per my understanding the above implementation is somehow ensuring that the gGlobalVarLock is locked by the execution thread, and no other thread is getting access to it once the code under the mutex.lock() for any particular thread is executing. 根据我的理解,上述实现可以某种方式确保gGlobalVarLock被执行线程锁定,并且一旦执行了特定线程的Mutex.lock()下的代码,其他线程就无法访问它。 It looks like it is also locking the global variable under the lock. 看起来它也在锁定全局变量。 But I don't know how. 但是我不知道如何。

My code seems to be running fine. 我的代码似乎运行良好。 But I have one doubt. 但是我有一个疑问。 Mutex will lock the code for thread 2 and 3 since these two threads use the same piece of code for execution. 互斥锁将锁定线程2和3的代码,因为这两个线程使用同一段代码来执行。 But for the thread 1 which is running some other piece of code, how does the locking work? 但是对于正在运行其他代码的线程1,锁定如何工作? Does the mutex.lock() in that thread ensure that th gGlobalVarLock is locked for other threads when it is executing the code under its mutex.lock(). 该线程中的Mutex.lock()是否确保gGlobalVarLock在执行其Mutex.lock()下的代码时被其他线程锁定。 Does that mutex ensure the gGlobalVarLock is not changed my the other threads which are running some other piece of code? 该互斥锁是否确保gGlobalVarLock不会在运行其他代码的其他线程中更改? My understanding is mutex.lock() locks common code used by different threads. 我的理解是Mutex.lock()锁定不同线程使用的通用代码。 Then how is the global variable getting locked in my case where different threads are running different codes. 那么,在不同线程运行不同代码的情况下,如何锁定全局变量。

Does mutex.lock() lock global variables in a multi-threaded environment? Mutex.lock()是否在多线程环境中锁定全局变量?

The point is precisely that if you create a mutex object in the parent process and share it between several threads, this object's memory is shared by the threads (those are not processes) 关键是,如果您在父进程中创建一个互斥对象并在多个线程之间共享,则该对象的内存将由线程共享(不是进程的线程)

Should you create processes instead (using fork() ) the memory would be copied and it would not work. 如果您改为创建进程(使用fork() ),则会复制内存并且它将无法正常工作。

But between threads, memory is shared so the object is shared. 但是在线程之间,内存是共享的,因此对象是共享的。 You have a single instance of mutex in memory for all your threads. 您的所有线程在内存中只有一个mutex实例。

This object will ensure that only one thread at a time is accessing the critical section located between the calls of lock and unlock . 该对象将确保一次仅一个线程正在访问位于lockunlock调用之间的关键部分。

In a nutshell, your code is OK. 简而言之,您的代码就可以了。

The mutex assures that after the lock() just the thread that acquires the mutex can execute the code until reach the unlock() call. 互斥锁确保在lock()之后,仅获取该互斥锁的线程才能执行代码,直到到达unlock()调用为止。 Unless you have a code very slow, you can think it's being executing simultaneously. 除非您的代码非常慢,否则您可以认为它正在同时执行。 However the block of code between the lock() and unlock() is going to executed by just one thread each time. 但是,每次只由一个线程执行lock()unlock()之间的代码块。

A mutex does not lock variables, it only locks itself. 互斥锁不会锁定变量,而只会锁定自身。 By restricting access to a variable behind a mutex you can protect that variable, but if the variable is global, there is no guarantee that elsewhere in your code you might unintentionally access it without going through the mutex. 通过限制对互斥量后面的变量的访问,可以保护该变量,但是如果该变量是全局变量,则不能保证您在代码的其他位置可能会无意间访问它而无需通过互斥量。

For example, in your code, nobody uses the mutex until they have already accessed the, global variable and found it to be false. 例如,在您的代码中,没有人使用互斥体,直到他们已经访问了全局变量并且发现它为false。 So it is entirely possible that all three threads would contend for the mutex lock. 因此,这三个线程完全有可能争夺互斥锁。

In your example, the global variable seems redundant, you seem to be using it as a filter to protect access to the mutex. 在您的示例中,全局变量似乎是多余的,您似乎正在将其用作过滤器以保护对互斥量的访问。

You may be looking for something called a condition variable to use in conjunction with your mutex. 您可能正在寻找一种称为条件变量的东西来与您的互斥锁一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM