1 // 微软官方例程 稍微修改了下 测试刚刚好 2 3 #define THREAD_COUNT 20 4 static volatile UINT vol = 0; 5 void SimpleThreadA(void* id) { 6 UINT& max = *(PUINT)id; 7 8 for (size_t i = 0; i < max; i++) 9 { 10 WDS("[No.%u]%d %u", max, i, GetTickCount()); 11 InterlockedIncrement(&vol); 12 } 13 } 14 void MultiThreadTestA() { 15 DWORD num; 16 HANDLE threads[THREAD_COUNT]; 17 int args[THREAD_COUNT]; 18 int i; 19 20 for (i = 0; i < THREAD_COUNT; i++) { 21 args[i] = i + 1; 22 threads[i] = reinterpret_cast<HANDLE>(_beginthread(SimpleThreadA, 0, 23 args + i)); 24 if (threads[i] == reinterpret_cast<HANDLE>(-1)) 25 // error creating threads 26 break; 27 } 28 29 WaitForMultipleObjects(i, threads, true, INFINITE); 30 WDS("VOL %d", vol); 31 }
/* * @@静态类 * Locker 原子锁 * QQ : 4seaynl * */ class Locker { private: LONG m_locker = NULL; public: void Lock() { while (TRUE == m_locker) ::Sleep(1); InterlockedIncrement(&m_locker); } void Unlock() { InterlockedDecrement(&m_locker); } };
不喜欢用别人的代码 std::mutex,,,
标签:COUNT,THREAD,void,args,原子,win32,locker,threads,线程 From: https://www.cnblogs.com/ksanl/p/17016174.html