这是这个问题的后续问题 :
我想我不理解Interlocked Acquire / Release API。 我把下面的小程序放在一起。 据我了解,g_val_1,g_val_2和g_val_3应始终以相同的顺序更新,并且最终应该以相同的值结束。 但他们没有(对于多个线程)。
我错过了什么? 谢谢。
#include "windows.h" #include "stdio.h" #define _THREADS_ 100 #define _TICKS_ 1000 int volatile g_threads = 0; DWORD volatile g_val_1 = 0; DWORD volatile g_val_2 = 0; DWORD volatile g_val_3 = 0; BOOL g_running = TRUE; DWORD TestThread(PVOID ignore) { while (g_running) { InterlockedIncrementAcquire(&g_val_1); g_val_2++; InterlockedIncrementRelease(&g_val_3); } InterlockedDecrement(&g_threads); return(0); } int __cdecl main(int argc, char* argv[]) { int th, duration; int success; DWORD ticks; duration = _TICKS_; g_threads = _THREADS_; printf("Max=%d Threads=%d Entries=%dn", duration, g_threads); printf("Creating Threadsn"); th = g_threads; while (th-- > 0) { CreateThread(NULL, 0, TestThread, NULL, NORMAL_PRIORITY_CLASS, NULL); } printf("Starting Threadsn"); ticks = GetTickCount(); while ((GetTickCount() - ticks) 0); ticks = GetTickCount() - ticks; success = ((g_val_1 == g_val_2) && (g_val_1 == g_val_2)); printf("Duration=%d g_val_1=%d g_val_2=%d g_val_3=%d OK=%dn", ticks, g_val_1, g_val_2, g_val_3, success); }
以上就是c/c++开发分享InterlockedIncrement vs InterlockedIncrementAcquire / Release(Redux)相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/549223.html