【发布时间】:2016-04-15 01:59:06
【问题描述】:
我的代码:
#include <iostream>
#include <thread>
void function_1()
{
std::cout << "Thread t1 started!\n";
for (int j=0; j>-100; j--) {
std::cout << "t1 says: " << j << "\n";
}
}
int main()
{
std::thread t1(function_1); // t1 starts running
for (int i=0; i<100; i++) {
std::cout << "from main: " << i << "\n";
}
t1.join(); // main thread waits for t1 to finish
return 0;
}
我创建了一个thread,它按降序打印数字,而main 按升序打印。
样本输出here。为什么我的代码打印垃圾?
【问题讨论】:
-
我不知道你为什么不能访问它。这是一个公共要点。
标签: c++ multithreading synchronization