线程的创建,pthread_create,pthread_self,pthread_once
2017-02-22 16:07
387 查看
typedef unsigned long int pthread_t;
//come from /usr/include/bits/pthreadtypes.h
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);创建新的线程
pthread_t pthread_self(void);获取本线程的线程ID
int pthread_equal(pthread_t t1, pthread_t t2);判断两个线程ID是否指向同一线程
int pthread_once(pthread_once_t *once_control, void (*init_routine) (void));用来保证init_routine线程函数在进程中只执行一次。
某些情况下,函数执行次数要被限制为1次,这种情况下,要使用pthread_once,代码示例:
运行结果:
main thread, ID is 3076200128
current thread ID is 3067804480
function run is running in thread:3067804480
thread2 end
current thread ID is 3076197184
thread1 end
main thread exit
虽然在thread1 跟thread2中都调用了run函数,但是run函数只执行了一次。
//come from /usr/include/bits/pthreadtypes.h
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);创建新的线程
pthread_t pthread_self(void);获取本线程的线程ID
int pthread_equal(pthread_t t1, pthread_t t2);判断两个线程ID是否指向同一线程
int pthread_once(pthread_once_t *once_control, void (*init_routine) (void));用来保证init_routine线程函数在进程中只执行一次。
#include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h> int* thread_func(void* arg) { pthread_t new_thid; new_thid = pthread_self();//打印线程自己的线程ID printf("the new thread, ID is %lu\n", new_thid); return NULL; } int main() { pthread_t thid; printf("main thread, ID is %lu\n", pthread_self());//打印主线程自己的线程ID if (pthread_create(&thid, NULL, (void*)thread_func, NULL) != 0) { printf("create thread failed\n"); exit(0); } sleep(5); return 0; }
某些情况下,函数执行次数要被限制为1次,这种情况下,要使用pthread_once,代码示例:
#include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h> pthread_once_t once = PTHREAD_ONCE_INIT; void run(void) { printf("function run is running in thread:%lu\n", pthread_self()); } int* thread1(void* arg) { pthread_t new_thid; new_thid = pthread_self(); printf("current thread ID is %lu\n", new_thid); pthread_once(&once, run); printf("thread1 end\n"); return NULL; } int* thread2(void* arg) { pthread_t new_thid; new_thid = pthread_self(); printf("current thread ID is %lu\n", new_thid); pthread_once(&once, run); printf("thread2 end\n"); return NULL; } int main() { pthread_t thid1, thid2; printf("main thread, ID is %lu\n", pthread_self()); pthread_create(&thid1, NULL, (void*)thread1, NULL); pthread_create(&thid2, NULL, (void*)thread2, NULL); sleep(5); printf("main thread exit\n"); return 0; }
运行结果:
main thread, ID is 3076200128
current thread ID is 3067804480
function run is running in thread:3067804480
thread2 end
current thread ID is 3076197184
thread1 end
main thread exit
虽然在thread1 跟thread2中都调用了run函数,但是run函数只执行了一次。
相关文章推荐
- Linux下多线程编程__线程的创建pthread_create与退出pthread_exit,等待当前线程退出pthread_join,获取线程ID pthread_self
- 同一线程多次创建 的 线程资源释放问题在每个线程的一开始,用pthred_detach(pthread_self())来使自己detach掉
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- linux 线程创建函数pthread_create的几个传参方式
- POSIX多线程之创建线程pthread_create && 线程清理pthread_cleanup
- pthread线程创建出现undefined reference to `pthread_create' collect2: error: ld returned 1 exit status
- 同一线程多次创建 的 线程资源释放问题在每个线程的一开始,用pthred_detach(pthread_self())来使自己detach掉
- pthread_create线程创建的过程剖析(转)
- pthread_create()给创建的线程传递参数
- 线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- 线程创建之重要属性PTHREAD_CREATE_DETACHED
- 主进程中如何退出pthread_create创建的线程?
- pthread_create()创建线程最大个数
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- pthread_create线程创建的过程剖析
- Linux中以PTHREAD_CREATE_DETACHED属性创建线程
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- pthread_create主线程与创建的新线程之间退出关系
- pthread_create线程创建的过程剖析
- c与c++在使用pthread_create创建线程时的区别