69,369
社区成员
发帖
与我相关
我的任务
分享
C:\demo\test>type test.cpp
#include <iostream>
#include <thread>
#include <mutex>
std::mutex m;
void foo(int id)
{
__declspec( thread ) static int i = 0;
for(; i < 29; ++i)
{
std::lock_guard<std::mutex> lock(m);
std::cout <<id << ":" << i << std::endl;
}
}
int main() {
std::thread t1(foo, 256);
std::thread t2(foo, 237);
t1.join();
t2.join();
return 0;
}
C:\demo\test>cl /EHsc /MT /Ox /DNDEBUG test.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.30501 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.cpp
Microsoft (R) Incremental Linker Version 12.00.30501.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:test.exe
test.obj
C:\demo\test>test
256:0
237:0
256:1
237:1
256:2
237:2
256:3
237:3
256:4
237:4
256:5
237:5
256:6
237:6
256:7
237:7
256:8
237:8
256:9
237:9
256:10
237:10
256:11
237:11
256:12
237:12
256:13
237:13
256:14
237:14
256:15
237:15
256:16
237:16
256:17
237:17
256:18
237:18
256:19
237:19
256:20
237:20
256:21
237:21
256:22
237:22
256:23
237:23
256:24
237:24
256:25
237:25
256:26
237:26
256:27
237:27
256:28
237:28
C:\demo\test>
#define MAXTHREADS 2000
POOL pool[MAXTHREADS];
pool[GetCurrentThreadId()%MAXTHREADS]=...;