#include <unistd.h>
#include <stdio.h>
#include <cstring>
#include <thread>
void test_cpu() {
printf("thread: test_cpu start\n");
int total = 0;
while (1) {
++total;
}
}
void test_mem() {
printf("thread: test_mem start\n");
int step = 20;
int size = 10 * 1024 * 1024; // 10Mb
for (int i = 0; i < step; ++i) {
char* tmp = new char[size];
memset(tmp, i, size);
sleep(1);
}
printf("thread: test_mem done\n");
}
int main(int argc, char** argv) {
std::thread t1(test_cpu);
std::thread t2(test_mem);
t1.join();
t2.join();
return 0;
}
用它编译 g++ -o test test.cc --std=c++11 -lpthread
我在Linux中运行该程序,然后运行top对其进行监视。
我希望看到一个过程,但是我看到了三个。
看起来std :: thread正在创建线程,为什么我最终要获取进程?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句