(转载)http://bbs.chinaunix.net/thread-836577-1-1.html

仅执行一次的操作
int pthread_once(pthread_once_t *once_control, void (*init_routine) (void))

本函数使用初值为PTHREAD_ONCE_INIT的once_control变量保证init_routine()函数在本进程执行序列中仅执行一次。

#include <semaphore.h>
#include <sys/types.h>
#include <dirent.h>
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include <time.h>pthread_once_t once = PTHREAD_ONCE_INIT;void once_run(void)
{printf("once_run in thread %u\n", pthread_self());
}void* task1(void* arg)
{int tid = pthread_self();printf("thread1 enter %u\n", tid);pthread_once(&once, once_run);printf("thread1 returns %u\n", tid);return NULL;
}void* task2(void* arg)
{int tid = pthread_self();printf("thread2 enter %u\n", tid);pthread_once(&once, once_run);printf("thread2 returns %u\n", tid);return NULL;
}int main(int argc, char *argv[])
{pthread_t thrd1, thrd2;pthread_create(&thrd1, NULL, (void*)task1, NULL);pthread_create(&thrd2, NULL, (void*)task2, NULL);sleep(5);printf("Main thread exit...\n");return 0;
}

程序输出:

[root@robot ~]# ./thread_once
thread2 enter 3067722608
once_run in thread 3067722608
thread2 returns 3067722608
thread1 enter 3078212464
thread1 returns 3078212464
Main thread exit...
[root@robot ~]# 

once_run()函数仅执行一次,且究竟在哪个线程中执行是不定的,尽管pthread_once(&once,once_run)出现在两个线程中。

LinuxThreads 使用互斥锁和条件变量保证由pthread_once()指定的函数执行且仅执行一次,而once_control则表征是否执行过。如果 once_control的初值不是PTHREAD_ONCE_INIT(LinuxThreads定义为0),pthread_once()的行为就会不正常。在LinuxThreads中,实际"一次性函数"的执行状态有三种:NEVER(0)、IN_PROGRESS(1)、DONE(2),如果 once初值设为1,则由于所有pthread_once()都必须等待其中一个激发"已执行一次"信号,因此所有pthread_once()都会陷入永久的等待中;如果设为2,则表示该函数已执行过一次,从而所有pthread_once()都会立即返回0。

转载于:https://www.cnblogs.com/Robotke1/archive/2013/05/02/3053958.html

(转载)Linux下pthread_once()函数相关推荐

  1. [转载]Linux下getopt()函数的简单使用

    转载源地址:https://www.cnblogs.com/qingergege/p/5914218.html 1.getopt()函数的出处就是unistd.h头文件(哈哈),写代码的时候千万不要忘 ...

  2. Linux下pthread_once()函数

    函数声明 int pthread_once(pthread_once_t *once_control, void (*init_routine) (void)): 本函数使用初值为PTHREAD_ON ...

  3. linux下syscall函数,SYS_gettid,SYS_tgkill

    出处:http://blog.chinaunix.net/uid-28458801-id-4630215.html linux下syscall函数,SYS_gettid,SYS_tgkill 2014 ...

  4. linux下system函数的深入理解

    这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...

  5. Linux下select函数实现的聊天服务器

    转载: http://blog.csdn.net/microtong/article/details/4989902 Linux下select函数实现的聊天服务器  佟强 http://blog.cs ...

  6. Linux下time函数

    Linux下time函数都在time.h头文件中. 1.头文件 和时间有关的头文件有以下几个: time.h sys/time.h sys/times.h sys/timeb.h sys/timex. ...

  7. Linux下curses函数库的详细介绍

    Linux下curses函数库的详细介绍 curses库介绍 安装 curses库函数介绍 初始化和重置函数 管理屏幕的函数 输出到屏幕 从屏幕读取 清除屏幕 移动光标 字符属性 管理键盘的函数 键盘 ...

  8. linux下readlink函数详解

    linux下readlink函数详解 相关函数: stat, lstat, symlink 表头文件: #include <unistd.h> 定义函数:int  readlink(con ...

  9. linux 纪元时间转换,[转]Linux下时间函数time gettimeofday

    Linux下时间函数time & gettimeofday UNIX及Linux的时间系统是由「新纪元时间」Epoch开始计算起,单位为秒.Epoch是指定为1970年1月1日凌晨零点零分零秒 ...

最新文章

  1. 最小硬盘实现单原子信息存储 超现有硬盘500倍
  2. cuda安装配置VS2013
  3. Kafka和其他消息队列
  4. 服务端设置忽略更新_react服务端渲染: cookie如何透传给后端,后端如何设置cookie...
  5. 精准 iOS 内存泄露检测工具
  6. php选中文本区域,php – 将新行更改为文本区域
  7. directx9 截屏
  8. blender 快捷键
  9. 网易云团队前端单元测试技术方案总结,测试人员必备知识
  10. 关于上传文件到ftp服务器报错553 could not create file.
  11. php计算距离元旦还有多少天,2020年元旦顺口溜
  12. 爬虫 - 提高爬虫效率的方法
  13. uniapp设置页面背景颜色
  14. 电信ZNHG60光猫信息虚标
  15. uniapp使用高德地图微信小程序SDK生成地图轨迹
  16. UVA 10881 Piotr‘s Ants
  17. JQuery极果商城项目实战(附完整代码)
  18. char、char*、char**数组
  19. socket读写返回值的处理
  20. 网站多次切换服务器ip,站群多ip服务器怎么切换ip?

热门文章

  1. Django 静态资源路径问题(一)
  2. 数据结构之【树形结构】复习题
  3. 让VisualStudio的StartPage关联自己的博客
  4. Spring Boot 应用程序启动流程分析
  5. HTTP学习记录:二、请求方法
  6. VS2015智能提示由英文改为中文
  7. swift:简单使用翻页控制器UIPageViewController
  8. 我是越来越喜欢做产品了
  9. 1、cocos2dx开发学习第一篇-项目工程的创建
  10. linux 常用命令集