0
我使用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;
}
是的,謝謝..它的工作..請說什麼是睡眠()函數和它是哪個頭? –
@Vinoj [Sleep](http://pubs.opengroup.org/onlinepubs/009695399/functions/sleep.html) –