为啥重新执行我的多线程代码后输出不一样?
Posted
技术标签:
【中文标题】为啥重新执行我的多线程代码后输出不一样?【英文标题】:Why are not outputs the same after re-executing my multithreading code?为什么重新执行我的多线程代码后输出不一样? 【发布时间】:2014-02-15 11:08:42 【问题描述】:当我执行以下代码时,每次重新编译和重新执行后,答案(输出)都不相同。这是什么原因?
#include <iostream>
#include <cstdlib>
//#include <pthread.h>
using namespace std;
#define NUM_THREADS 5
void *PrintHello(void *threadid)
long tid;
tid = (long)threadid;
cout << "Hello World! Thread ID, " << tid << endl;
pthread_exit(NULL);
int main ()
pthread_t threads[NUM_THREADS];
int rc;
int i;
for( i=0; i < NUM_THREADS; i++ )
cout << "main() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL,
PrintHello, (void *)i);
if (rc)
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
pthread_exit(NULL);
g++ test.cpp -o test -lpthread ./测试
输出1:
main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
Hello World! Thread ID, 0
main() : creating thread, 3
Hello World! Thread ID, 1
Hello World! Thread ID, 2
main() : creating thread, 4
Hello World! Thread ID, 3
Hello World! Thread ID, 4
g++ test.cpp -o test -lpthread ./测试
输出2:
main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
Hello World! Thread ID, 0
main() : creating thread, 4
Hello World! Thread ID, 3
Hello World! Thread ID, 2
Hello World! Thread ID, 4
Hello World! Thread ID, 1
【问题讨论】:
【参考方案1】:由于您的线程不同步,因此执行顺序不是精确确定的,并且取决于您的执行上下文,例如同时运行的其他进程等,每次运行时可能会有所不同程序。
【讨论】:
感谢您的回答,如何确定执行顺序? @Code 提前?你不能,真的。回想起来,你可以在那里看到它。在Output1
中是 0、1、2、3、4,在 Output2
中是 0、3、2、4、1。
您可以使用pthread_mutex_lock()
、pthread_cond_wait()
等同步原语来控制它。请参阅computing.llnl.gov/tutorials/pthreads/#Mutexes 和computing.llnl.gov/tutorials/pthreads/#ConditionVariables 了解更多详情
是的,那么它需要OS课程的概念。但是当我得到tutorialspoint.com/cplusplus/cpp_multithreading.htm的代码时,我认为顺序必须是:main():0,1,2,3,ID:0,1,2,3,4。【参考方案2】:
是的。我用过 pthread_mutex_lock(&mutex);和 pthread_mutex_unlock(&mutex);如下
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>
using namespace std;
#define NUM_THREADS 5
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *PrintHello(void *threadid)
long tid;
tid = (long)threadid;
//sleep(1);
cout << "Hello World! Thread ID, " << tid << endl;
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
int main ()
pthread_t threads[NUM_THREADS];
int rc;
int i;
//pthread_attr_t attr;
//pthread_attr_init(&attr);
//pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for( i=0; i < NUM_THREADS; i++ )
pthread_mutex_lock(&mutex);
cout << "main() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i);
//sleep(0.5);
if (rc)
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
pthread_exit(NULL);
我的输出总是:
main() : creating thread, 0
Hello World! Thread ID, 0
main() : creating thread, 1
Hello World! Thread ID, 1
main() : creating thread, 2
Hello World! Thread ID, 2
main() : creating thread, 3
Hello World! Thread ID, 3
main() : creating thread, 4
Hello World! Thread ID, 4
【讨论】:
以上是关于为啥重新执行我的多线程代码后输出不一样?的主要内容,如果未能解决你的问题,请参考以下文章
实现Runnable的多线程代码中,while(true)表示的啥含义?为啥没有while(t