#include <pthread.h> #include <unistd.h> #include <cstdio> void *run(void *p) { int t = *(static_cast<int*>(p)); printf("sleeping %d seconds..\n",t); sleep(t); // unit is msec pthread_exit(NULL); } int main() { pthread_t th; void* th_result; int duration = 2; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE); pthread_create(&th,&attr,run,&duration); pthread_attr_destroy(&attr); pthread_join(th,&th_result); pthread_exit(NULL); }
0 件のコメント:
コメントを投稿