线程简洁

时间:2017-06-12 18:10:22

标签: c++ multithreading

(这是我想要做的更大的更简单的版本。) 而不是做所有这个程序打印你好三次:

void *PrintHello(void *threadarg) {
 cout << "hello world" << endl;
}

main:
 data thread_data[3];

for(int i=0; i<3; i++)
  rc = pthread_create(&threads[i], NULL, PrintHello, NULL);

for(i=0; i < blocks; i++)
  pthread_join(threads[i], &status);

是否有相同的方法可以打印hello world三次而不跳转到函数?如果有的话,我可以得到一个简单的例子吗?

这样的事情:

void *PrintHello(void *threadarg) {
 cout << "hello world" << endl;
}

main:
 data thread_data[3];

for(int i=0; i<3; i++)
  rc = pthread_create(&threads[i], NULL, NULL, NULL);

cout << "hello world" << endl;

for(i=0; i < blocks; i++)
  pthread_join(threads[i], &status);

我的问题是我需要我的线程在项目中使用很多结构和变量。通过引用建立通行证需要一段时间,但我想我别无选择。

0 个答案:

没有答案