Linux中以PTHREAD_CREATE_DETACHED属性创建线程
2013-05-03 11:05
781 查看
在线程创建时将其属性设为分离状态(detached),也可在线程创建后将其属性设为分离的(detached)。
这里使用在创建时指定线程为PTHREAD_CREATE_DETACHED属性。
[b]一、实例[/b]
程序输出:
[root@robot ~]# gcc thread_detach.c -lpthread
[root@robot ~]# ./a.out
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
Leave main thread!
[root@robot ~]#
这里使用在创建时指定线程为PTHREAD_CREATE_DETACHED属性。
[b]一、实例[/b]
#include <dirent.h> #include <pthread.h> #include <errno.h> #include <signal.h> #include <time.h> void* thread1(void *arg) { while (1) { usleep(100 * 1000); printf("thread1 running...!\n"); } printf("Leave thread1!\n"); return NULL; } int main(int argc, char** argv) { pthread_t tid; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); // 设置分离属性 pthread_create(&tid, &attr, (void*)thread1, NULL); pthread_attr_destroy(&attr); sleep(1); printf("Leave main thread!\n"); return 0; }
程序输出:
[root@robot ~]# gcc thread_detach.c -lpthread
[root@robot ~]# ./a.out
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
Leave main thread!
[root@robot ~]#
相关文章推荐
- 线程创建之重要属性PTHREAD_CREATE_DETACHED
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- linux 线程创建函数pthread_create的几个传参方式
- ubuntu线程的属性PTHREAD_CREATE_JOINABL和PTHREAD_CREATE_DETACHED
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- pthread_create是类Unix操作系统(Unix、Linux、Mac OS X等)的创建线程的函数。
- 线程退出【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- linux创建线程之pthread_create
- Linux学习-线程的创建和终止-(出现错误-undefined reference to 'pthread_create')
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- linux 创建线程pthread_create
- linux 线程创建函数pthread_create的几个传参方式
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- Linux下多线程编程__线程的创建pthread_create与退出pthread_exit,等待当前线程退出pthread_join,获取线程ID pthread_self
- linux 创建线程问题undefined reference to 'pthread_create'
- linux创建线程之pthread_create
- linux 下线程使用(使用pthread_create)转
- pthread_create线程创建的过程剖析
- 使用pthread_create创建大量线程时,创建线程失败,Resource temporarily unavailable的解决办法