C , C++ unsynchronized threads returning a strange result

后端 未结 4 1476
执念已碎
执念已碎 2020-12-07 02:54

Okay, i have this question in one regarding threads.

there are two unsynchronized threads running simultaneously and using a global resource \"int num\" 1st:

4条回答
  •  眼角桃花
    2020-12-07 03:07

    As Thomas said, the results are unpredictable because your increment and decrement are non-atomic. You can use InterlockedIncrement and InterlockedDecrement -- which are atomic -- to see a predictable result.

    • Interlocked Variable Access (MSDN)

提交回复
热议问题