简体   繁体   English

Win32线程

[英]Win32 threading

My application uses CreateThread to create a secondary (and only) thread that executes every 10ms , like the following pseudocode :我的应用程序使用CreateThread创建一个每10ms执行一次的辅助(也是唯一)线程,如下面的伪代码

map<string, int32_t> a_map;

DWORD WINAPI Table::manual_action_execute_thread(LPVOID lpParameter) {
   while(Table::manual_action_execute_thread_run) {
      ...
      if (!Table::automatic_action_execute_inprogress) {
         ...
      }
      ...
      if (a_map["blah"] == 0) {
         ...
      }
      ...
      Sleep(10);
   }
   return 0;
}

The variables are declared as following:变量声明如下:

static volatile bool manual_action_execute_thread_run;
static volatile bool automatic_action_execute_inprogress;

The first one takes the value of true before even start my thread so i don't use locking on this.第一个甚至在启动我的线程之前就采用true的值,所以我不使用锁定。 The second takes a false at first.第二个首先取false

I am using ::automatic_action_execute_inprogress to control some behaviour on second thread which only changes on the main thread .我正在使用::automatic_action_execute_inprogress来控制仅在主线程上更改的第二个线程上的某些行为。

QUESTION(s):问题):

1) Since i only update ::automatic_action_execute_inprogress on main thread and just reading it on second thread i still need to lock it first using EnterCriticalSection ? 1)由于我只在主线程上更新::automatic_action_execute_inprogress而只是在第二个线程上读取它,我仍然需要先使用EnterCriticalSection锁定它 Or the locking is only restricted to the shared variables that are changed on both threads?或者锁定仅限于在两个线程上更改的共享变量?

2) What about a <map> that's used on multiple threads and modified by just one? 2)在多个线程上使用并仅由一个修改的<map>怎么样? Certainly i have to lock it with EnterCriticalSection whenever changes but what about read access?当然,每当更改时我都必须使用EnterCriticalSection锁定它,但是read访问呢? Should i lock it when i read from it (like if (a_map["foo"] == 0) ) if can change even by a single thread?我应该在读取锁定它(比如if (a_map["foo"] == 0) )如果可以通过单个线程改变? Like this for example?比如这个?

EnterCriticalSection(&cs);
   bool val = a_map["foo"];
LeaveCriticalSection(&cs);

   if (val == 0) {
      ...
   }

Since you only update your variable in one thread and just read it in other threads, and since the type of the variable is atomic , you do not need any locking mechanism.由于您只在一个线程中更新您的变量并在其他线程中读取它,并且由于变量的类型是atomic ,因此您不需要任何锁定机制。 All you need to do is declare it volatile , which you have already done.您需要做的就是将它声明为volatile ,您已经完成了。 If the variable was of a non-atomic type, (say, a map , or even just a 64-bit integer on a 32-bit architecture,) then you would need locking.如果变量是非原子类型(例如, map ,或者甚至只是 32 位体系结构上的 64 位整数),那么您将需要锁定。

The C & C++ standard does not presume atomicity of operations even on simple machine-word operations like reading an int , but if you are using winapi then you are on x86 , which guarantees it.即使在读取int等简单的机器字操作上,C 和 C++ 标准也不假定操作的原子性,但如果您使用的是winapi那么您使用的是x86 ,这保证了它。 (So, if you want to write portable code, use locking even if you are writing an int on one thread and only reading it from other threads.) (因此,如果您想编写可移植代码,即使您在一个线程上编写int并且仅从其他线程读取它,也要使用锁定。)

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

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