pthread线程初始化(pthread_once)
pthread_once 语法
int pthread_once(pthread_once_t *once_control,
void (*init_routine)(void));
#include <pthread.h> pthread_once_t once_control = PTHREAD_ONCE_INIT; int ret; ret = pthread_once(&once_control, init_routine);
once_control 参数用来确定是否已调用相关的初始化例程。
pthread_once 返回值
pthread_once() 在成功完成之后返回零。其他任何返回值都表示出现了错误。如果出现以下情况,pthread_once() 将失败并返回相应的值。
EINVAL
描述:
once_control 或 init_routine 是 NULL。
解析:
在多线程环境中,有些事仅需要执行一次。通常当初始化应用程序时,可以比较容易地将其放在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();
cout<<"main thread exit"<<endl;
;
}
结果:
hello thread enter once_run thread return thread enter thread return main thread exit
pthread线程初始化(pthread_once)的更多相关文章
- [并发并行]_[线程模型]_[Pthread线程使用模型之三 客户端/服务端模型(Client/Server]
Pthread线程使用模型之三 客户端/服务端模型(Client/Server) 场景 1.在客户端/服务端模型时,客户端向服务端请求一些数据集的操作. 服务端执行执行操作独立的(多进程或跨网络)– ...
- [并发并行]_[线程模型]_[Pthread线程使用模型之一管道Pipeline]
场景 1.经常在Windows, MacOSX 开发C多线程程序的时候, 经常需要和线程打交道, 如果开发人员的数量不多时, 同时掌握Win32和pthread线程 并不是容易的事情, 而且使用Win ...
- 分布式缓存系统 Memcached 工作线程初始化
Memcached采用典型的Master-Worker模式,其核心思想是:有Master和Worker两类进程(线程)协同工作,Master进程负责接收和分配任务,Worker进程负责处理子任务.当各 ...
- 分享一个关于pthread线程栈在mm_struct里面的分布问题
大家好,本人被下面这个问题困扰了一段时间,最近似乎找到了答案. 这里和大家分享一下,可能对有相同困惑的同学有点帮助,同时也请各位帮忙看看错漏的地方. 1================问题: 在使用p ...
- 【C/C++多线程编程之四】终止pthread线程
多线程编程之终止pthread线程 Pthread是 POSIX threads 的简称,是POSIX的线程标准. 终止线程似乎是多线程编程的最后一步,但绝不是本系列教 ...
- [并发并行]_[线程模型]_[Pthread线程使用模型之二 工作组work crew]
Pthread线程使用模型之二工作组(Work crew) 场景 1.一些耗时的任务,比如分析多个类型的数据, 是独立的任务, 并不像 pipeline那样有序的依赖关系, 这时候pipeline就显 ...
- 【C/C++多线程编程之五】pthread线程深入理解
多线程编程之pthread线程深入理解 Pthread是 POSIX threads 的简称,是POSIX的线程标准. 前几篇博客已经能给你初步的多线程概念.在进一步学 ...
- 【C/C++多线程编程之十】pthread线程私有数据
多线程编程之线程私有数据 Pthread是 POSIX threads 的简称.是POSIX的线程标准. 线程同步从相互排斥量[C/C++多线程编程之六]pthread相互排 ...
- [转]c++多线程编程之pthread线程深入理解
多线程编程之pthread线程深入理解 Pthread是 POSIX threads 的简称,是POSIX的线程标准. 前几篇博客已经能给你初步的多线程概念.在进一 ...
随机推荐
- c++第二十五天
p129~p131: 1.赋值运算的左侧运算对象必须是一个可修改的左值. 2.赋值运算满足右结合律. 3.赋值运算的结果是它的左侧对象,并且是一个左值. 验证: #include<iostrea ...
- 20145315 《Java程序设计》第九周学习总结
20145315 <Java程序设计>第九周学习总结 教材学习内容总结 第16章--整合数据库 16.1.1JDBC简介 应用程序通过通信协议对数据库进行指令交换,以进行对数据的的增删查找 ...
- SprigBoot核心技术
启动原理 运行流程 自动配置原理 一.启动原理 SpringApplication.run(主程序类)– new SpringApplication(主程序类)• 判断是否web应用• 加载并保存所有 ...
- Linux清除Windows密码
下载安装ntfs-3g 下载驱动让linux挂载windows磁盘 https://tuxera.com/opensource/ntfs-3g_ntfsprogs-2017.3.23.tgz 安装 t ...
- P4factory ReadMe 剩余部分
Building and Running a Target Each P4 program (called a 'target') is set up in a directory under tar ...
- codeforces 355 div2 C. Vanya and Label 水题
C. Vanya and Label time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- python 列表元素的筛选
color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow'] color = [x ,,)] print(color)
- Java的System.out.println()的解析
Java的System.out.println()的解析 System 是java.lang中的一个类. System.out 中的out, 代表了System类中的静态对象PrintStream, ...
- JVM知识总结-运行时区域划分
区域简介 JVM运行时区域有些随着虚拟机进程的启动而存在,有些依赖于用户线程的启动和结束而建立和销毁,大致分为以下几类:方法区,虚拟机栈,本地方法栈,堆,程序计数器,概念图如下(源于<深入理解J ...
- 用PendingIntent传送数据丢失解决办法
当要设置一个闹钟时,可以把数据放在Intent里,再用intent对象生成一个PendingIntent对象,然后用AlarmManager 来邦定PendingIntent对象设置闹钟,具体代码如下 ...