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线程函数在进程中只执行一次。

#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) != )
{
printf("create thread failed\n");
exit();
} sleep(); return ;
}

某些情况下,函数执行次数要被限制为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();
printf("main thread exit\n"); return ;
}

运行结果:

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_create,pthread_self,pthread_once的更多相关文章

  1. 线程的创建pthread_create.c

    #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <errno.h&g ...

  2. 关于linux的一点好奇心(五):进程线程的创建

    一直以来,进程和线程的区别,这种问题一般会被面试官拿来考考面试者,可见这事就不太简单.简单说一点差异是,进程拥有独立的内存资源信息,而线程则共享父进程的资源信息.也就是说线程不拥有内存资源,所以对系统 ...

  3. Linux多线程编程——线程的创建与退出

    POSIX线程标准:该标准定义了创建和操纵线程的一整套API.在类Unix操作系统(Unix.Linux.Mac OS X等)中,都使用Pthreads作为操作系统的线程.Windows操作系统也有其 ...

  4. 三十六、Linux 线程——线程基本概念及线程的创建和终止

    36.1 线程介绍 36.1.1 线程的基本概念 进程是资源管理的最小单位,线程是程序执行的最小单位 每个进程都有自己的数据段.代码段和堆栈段. 线程通常叫做轻型的进程,它包含独立的栈和 CPU 寄存 ...

  5. POSIX 线程的创建与退出

    前言 创建线程: pthread_create() 退出线程: pthread_exit()return pthread_cancel() 线程的创建 使用多线程,首先就需要创建一个新线程.那么线程是 ...

  6. Linux线程的创建

    一.线程与进程的区别 1.线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的全部资源. 2.进程是资源分配的基本单位.所有与该进程有关的资源,都 ...

  7. 线程概念( 线程的特点,进程与线程的关系, 线程和python理论知识,线程的创建)

    参考博客: https://www.cnblogs.com/xiao987334176/p/9041318.html 线程概念的引入背景 进程 之前我们已经了解了操作系统中进程的概念,程序并不能单独运 ...

  8. python 全栈开发,Day41(线程概念,线程的特点,进程和线程的关系,线程和python 理论知识,线程的创建)

    昨日内容回顾 队列 队列 : 先进先出.数据进程安全 队列实现方式: 管道 + 锁 生产者消费者模型 : 解决数据供需不平衡 管道 双向通信 数据进程不安全 EOFError: 管道是由操作系统进行引 ...

  9. python全栈开发,Day41(线程概念,线程的特点,进程和线程的关系,线程和python理论知识,线程的创建)

    昨日内容回顾 队列 队列:先进先出.数据进程安全 队列实现方式:管道+锁 生产者消费者模型:解决数据供需不平衡 管道 双向通信,数据进程不安全 EOFError: 管道是由操作系统进行引用计数的 必须 ...

随机推荐

  1. 计算两个集合的差集——第六期 Power8 算法挑战赛

    第六期Power8大赛 1.1 比赛题目 题目: 计算两个集合的差集: 详细说明: 分别有集合A和B两个大数集合,求解集合A与B的差集(A中有,但B中无的元素),并将结果保存在集合C中,要求集合C中的 ...

  2. 05: MySQL高级查询

    MySQL其他篇 目录: 参考网站 1.1 GROUP BY分组使用 1.2 mysql中NOW(),CURDATE(),CURTIME()的使用 1.3 DATEDIFF() 函数 1.4 DATE ...

  3. win7 + MySQL 5.6.35 免安装版部署

    之前项目开发一直用的asp.net技术,所以数据库自然而然的就用的Sql Server了,最近想着手看一下MySQL数据库,部署免安装版的MySQL的过程记录一下. 准备工作:window 7   6 ...

  4. 【前端】javascript+jQuery实现360开机时间显示效果

    实现效果: 实现原理: 给关闭按钮绑定点击事件,点击以后触发动画效果.利用jQuery的animate方法,先让显示天气的盒子高度变为0,接着让整个包含天气和事件的盒子宽度变为0,最后通过将displ ...

  5. BZOJ 4552: [Tjoi2016&Heoi2016]排序 线段树 二分

    目录 此代码是个假代码,只能糊弄luogu,以后再改,路过大佬也可以帮一下辣 update 10.6 此代码是个假代码,只能糊弄luogu,以后再改,路过大佬也可以帮一下辣 /* //fang zhi ...

  6. POJ 2155 Matrix (二维树状数组)题解

    思路: 没想到二维树状数组和一维的比只差了一行,update单点更新,query求和 这里的函数用法和平时不一样,query直接算出来就是某点的值,怎么做到的呢? 我们在更新的时候不止更新一个点,而是 ...

  7. centos 安装iftop

    iftop是linux下的一个流量监控工具,用于查看实时网络流量.官网:http://www.ex-parrot.com/pdw/iftop/ 1.安装必须软件包yum install libpcap ...

  8. mysql-5.7.20-winx64.zip Zip版、解压版MySQL安装

    1.  zip下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-winx64.zip 2.官方文档位置: http:// ...

  9. Python 逗号的几种作用

    转自http://blog.csdn.net/liuzx32/article/details/7831247 最近研究Python  遇到个逗号的问题 一直没弄明白 今天总算搞清楚了 1.逗号在参数传 ...

  10. ASP.NET MVC 中使用Ckeditor4.5 编辑器

    一.在项目中添加Ckeditor4.5.11 (1) 新建 ASP.NET MVC5项目,解压缩ckeditor_4.5.11_standard.zip,在VS2015的解决方案资源管理器中将得到的“ ...