我使用 pthread.h 运行以下代码...运行时,在线程完成之前,代码退出...
我附上代码...
#include<iostream>
#include<pthread.h>
using namespace std;
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid = (long)threadid;
cout<<"Hello World! Thread ID,"<<tid<<endl;
pthread_exit(NULL);
return &tid;
}
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);
//sleep(1);
if(rc)
{
cout<<"Error:Unable to create thread,"<<rc<<endl;
exit(-1);
}
}
pthread_exit(NULL);
return 0;
}