pthread_once()
在asterisk 开源Sip系统中,经常会看到这个函数的使用,一直不理解,特意有了下面的总结,但是到现在也没用理解这个函数在Sip协议中使用的方法!
在多线程环境中,有些事仅需要执行一次。通常当初始化应用程序时,可以比较容易地将其放在main函数中。但当你写一个库时,就不能在main里面初始化了,你可以用静态初始化,但使用一次初始化(pthread_once)会比较容易些。
int pthread_once(pthread_once_t *once_control, void (*init_routine) (void));
功能:本函数使用初值为PTHREAD_ONCE_INIT的once_control变量保证init_routine()函数在本进程执行序列中仅执行一次。
在多线程编程环境下,尽管pthread_once()调用会出现在多个线程中,init_routine()函数仅执行一次,究竟在哪个线程中执行是不定的,是由内核调度来决定。
Linux Threads使用互斥锁和条件变量保证由pthread_once()指定的函数执行且仅执行一次,而once_control表示是否执行过。
如果once_control的初值不是PTHREAD_ONCE_INIT(Linux Threads定义为0),pthread_once() 的行为就会不正常。
在LinuxThreads中,实际"一次性函数"的执行状态有三种:NEVER(0)、IN_PROGRESS(1)、DONE (2),如果once初值设为1,则由于所有pthread_once()都必须等待其中一个激发"已执行一次"信号,因此所有pthread_once ()都会陷入永久的等待中;如果设为2,则表示该函数已执行过一次,从而所有pthread_once()都会立即返回0
具体的一个实例:
- #include<iostream>
- #include<pthread.h>
- using namespace std;
- pthread_once_t once = PTHREAD_ONCE_INIT;
- void once_run(void)
- {
- cout<<"once_run in thread "<<(unsigned int )pthread_self()<<endl;
- }
- void * child1(void * arg)
- {
- pthread_t tid =pthread_self();
- cout<<"thread "<<(unsigned int )tid<<" enter"<<endl;
- pthread_once(&once,once_run);
- cout<<"thread "<<tid<<" return"<<endl;
- }
- void * child2(void * arg)
- {
- pthread_t tid =pthread_self();
- cout<<"thread "<<(unsigned int )tid<<" enter"<<endl;
- pthread_once(&once,once_run);
- cout<<"thread "<<tid<<" return"<<endl;
- }
- int main(void)
- {
- pthread_t tid1,tid2;
- cout<<"hello"<<endl;
- pthread_create(&tid1,NULL,child1,NULL);
- pthread_create(&tid2,NULL,child2,NULL);
- sleep(10);
- cout<<"main thread exit"<<endl;
- return 0;
- }
#include<iostream>
#include<pthread.h>
using namespace std;pthread_once_t once = PTHREAD_ONCE_INIT;void once_run(void)
{cout<<"once_run in thread "<<(unsigned int )pthread_self()<<endl;
}void * child1(void * arg)
{pthread_t tid =pthread_self();cout<<"thread "<<(unsigned int )tid<<" enter"<<endl;pthread_once(&once,once_run);cout<<"thread "<<tid<<" return"<<endl;
}void * child2(void * arg)
{pthread_t tid =pthread_self();cout<<"thread "<<(unsigned int )tid<<" enter"<<endl;pthread_once(&once,once_run);cout<<"thread "<<tid<<" return"<<endl;
}int main(void)
{pthread_t tid1,tid2;cout<<"hello"<<endl;pthread_create(&tid1,NULL,child1,NULL);pthread_create(&tid2,NULL,child2,NULL);sleep(10);cout<<"main thread exit"<<endl;return 0;}
执行结果:
- hello
- thread 3086535584 enter
- once_run in thread 3086535584
- thread 3086535584 return
- thread 3076045728 enter
- thread 3076045728 return
- main thread exit
pthread_once()相关推荐
- (转载)Linux下pthread_once()函数
(转载)http://bbs.chinaunix.net/thread-836577-1-1.html 仅执行一次的操作 int pthread_once(pthread_once_t *once_c ...
- pthread_once()函数详解
转自:https://www.cnblogs.com/lit10050528/p/4325888.html 在多线程环境中,有些事仅需要执行一次.通常当初始化应用程序时,可以比较容易地将其放在main ...
- pthread_once重塑singleton模式
单件模式是非线程安全的: // Single threaded version class Foo {private Helper helper = null;public Helper getHel ...
- Linux下pthread_once()函数
函数声明 int pthread_once(pthread_once_t *once_control, void (*init_routine) (void)): 本函数使用初值为PTHREAD_ON ...
- 【C++】C/C++ 中的单例模式
目录 part 0:单例模式3种经典的实现方式 Meyer's Singleton Meyers Singleton版本二 Lazy Singleton Eager Singleton Testing ...
- Linux环境编程--多线程
本文作者: 姚继锋 (2001-08-11 09:05:00) 时隔多年 原文地址已经无从查... 居然是2001年就写了 今天看来对初学者还是很有参考意义 所以特转给大家 本人在原文基础 ...
- 200行代码解读TDEngine背后的定时器
作者 | beyondma来源 | CSDN博客 导读:最近几周,本文作者几篇有关陶建辉老师最新的创业项目-TdEngine代码解读文章出人意料地引起了巨大的反响,原以为C语言已经是昨日黄花,不过从读 ...
- linux cuda 异常退出,cudaErrorCudartUnloading问题排查及建议方案
原文请猛戳这里 敲黑板划重点--顺求异构计算/高性能计算/CUDA/ARM优化类开发职位 最近一段时间一直在负责做我厂神经网络前向框架库的优化,前几天接了一个bug report,报错信息大体是这样的 ...
- mysql数据库c 操作o_linux下c操作mysql数据库。gcc报错。谁能帮忙看下啊。
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 /usr/lib64/mysql/libmysqlclient.a(libmysql.c.o): In function `fetch_float_wit ...
最新文章
- 73款阿里巴巴开源软件详解
- 如何添加引文标_如何在Google文档中查找和添加引文
- jmeter中控制器3个请求其中一个访问不到_性能测试干货丨盘点JMeter常见的逻辑控制器...
- bzoj2500幸福的道路 树形dp+单调队列
- js去掉第一个换行符_通过异步迭代简化Node.js流程
- dylib java_使用单声道可执行文件捆绑.dylib文件
- Android 自己主动化測试之------ Monkey工具
- python是什么意思中文-python是什么意思中文翻译
- 12. Copy all parts of an object
- 重定向和请求转发后面加“return;”语句
- matlab prn文件,教你妙用PRN文件 实现文档的换机打印
- php zend studio 下载地址,Zend Studio
- 一行Python代码玩转emoji表情,已经玩坏了都...
- excel取消隐藏_这个毁人无数的Excel黑洞,却成就一批最牛X的高手
- 【Seedlabs】Local DNS Attack Lab
- #ASM 翻译系列第二十五弹:ASM 高级知识 When will my rebalance complete
- php图片写入带问号_关于编码:PHP输出显示带有问号的黑色小菱形
- 配置 SNAT 和NAT
- 【MFC】CSplitterWnd 重载限定窗口调整范围
- 安卓 文本框怎么贴近边缘_flash怎么设置帧频-Adobe flash修改帧频的方法
热门文章
- android studio annotations,AndroidStudio 添加AndroidAnnotations -电脑资料
- android build.time,Android SDK中的恒定Build.TIME
- php path separator,php 非常有用的高级函数PATH_SEPARATOR常量和set_include_path
- termux apache php,要啥自行车之Termux:将我们的(Android)安卓手机打造成全能的服务器...
- 简单英文题 25 Sequence Search(python)
- 我的世界服务器玩家在线指令,我的世界服务器中玩家间的传送功能 | 我的世界 | MC世界侠...
- 机器人炒饭小块头一一_一罐好饭,智能机器人炒饭出好味道,小块头坚守本心匠心制作!...
- 八个角最多可以把平面分成多少部分?_平面设计构图的基本形式
- python安装详细步骤mac_Mac OS X10.12.4安装的Python2.7升级Python3.5步骤详解
- 稳压电源的设计与制作_电子爱好设计的直流可调压电源电路,太完美了,有图,亲测可用...