pthread_once的pthread_once
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pthread_once的pthread_once相关的知识,希望对你有一定的参考价值。
参考技术APOSIX的只执行一次的pthread_once#ifdefWIN32#include<windows.h>#defineSLEEP(ms)Sleep(ms)#elseifdefined(LINUX)#include<stdio.h>#defineSLEEP(ms)sleep(ms)#endif#include<cassert>#include<pthread.h>//取出线程的IDintGetThreadID()#ifdefWIN32return(int)pthread_getw32threadhandle_np(pthread_self());#elseifdefined(LINUX)return(int)pthread_self();#endif//该静态变量被所有线程使用staticints_nThreadResult=0;staticpthread_once_tonce=PTHREAD_ONCE_INIT;//该初始化函数,我在多线程下只想执行一次voidthread_init()s_nThreadResult=-1;printf([Child%0.4x]loopingi(%0.8x)\\n,GetThreadID(),s_nThreadResult);void*theThread(void*param)//通过once的控制,thread_init只会被执行一次pthread_once(&once,&thread_init);printf([Child%0.4x]loopingi(%0.8x)\\n,GetThreadID(),s_nThreadResult);s_nThreadResult++;pthread_exit(&s_nThreadResult);returnNULL;intmain(intargc,char*argv[])pthread_ttid1,tid2;pthread_create(&tid1,NULL,&theThread,NULL);pthread_create(&tid2,NULL,&theThread,NULL);//无论是否休眠,两个子线程最终都会被主线程join到//因为两个子线程都是默认的PTHREAD_CREATE_JOINABLE类型//SLEEP(3);void*status=NULL;intrc=pthread_join(tid1,&status);assert(rc==0&&pthread_join1,rc);if(status!=PTHREAD_CANCELED&&status!=NULL)printf(Returnedvaluefromthread:%d\\n,*(int*)status);rc=pthread_join(tid2,&status);assert(rc==0&&pthread_join2,rc);if(status!=PTHREAD_CANCELED&&status!=NULL)printf(Returnedvaluefromthread:%d\\n,*(int*)status);return0;
线程的创建,pthread_create,pthread_self,pthread_once
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函数只执行了一次。
以上是关于pthread_once的pthread_once的主要内容,如果未能解决你的问题,请参考以下文章
线程的创建,pthread_create,pthread_self,pthread_once
ubuntu下调试ffmpeg程序出现undefined reference to pthread_once ,undefined reference to uncompress错误