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. RAM,ROM,NAND Flash,NOR Flash(A)

    他们四者相互独立 RAM掉电易失数据: RAM又分两种,一种是静态RAM,SRAM:一种是动态RAM,DRAM.前者的存储速度要比后者快得多,我们现在使用的内存一般都是动态RAM. DDR是Doubl ...

  2. Educational Codeforces Round 21 Problem D(Codeforces 808D)

    Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into t ...

  3. Java求两个数平均值

    如何正确的求2个数的平均值.在练习算法二分查找的时候发现的,以前没有注意到的bug 备注:数据以int类型为例 一.以前的通用写法 /** * 求a+b平均值 * @param a * @param ...

  4. AD快捷键

    * 在PCB电气层之间切换.在布线的过程中,按此键则换层并自动添加过孔并换层. Q 在公制和英制之间切换 J+C 定位到指定的元件处.在弹出的对话框内输入该元件的编号. G+G 设定栅格吸附尺寸. T ...

  5. Python3基础 os listdir 列举指定的所有文件及文件夹的名字

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. 乘积最大|2018年蓝桥杯B组题解析第十题-fishers

    标题:乘积最大 给定N个整数A1, A2, ... AN.请你从中选出K个数,使其乘积最大. 请你求出最大的乘积,由于乘积可能超出整型范围,你只需输出乘积除以1000000009的余数. 注意,如果X ...

  7. BZOJ5170: Fable 树状数组

    Description 有这么一则传闻,O(nlogn)的排序发明之前,滋滋国的排序都是采用的冒泡排序.即使是冒泡排序,对当时的国民 来说也太复杂太难以理解,于是滋滋国出现了这样一个职业——排序使,收 ...

  8. shiro(1) 介绍

    一.什么是shiro (1)属性:java框架 (2)用途:身份验证.用户授权.加密.会话管理 (3)优点:轻量.易用 二.三大组件 (1)subject:代表当前主体,与当前应用交互的任何东西都是s ...

  9. 【Coursera】Security Introduction -Ninth Week(1)

    前言 Coursera 的 Internet History,Technology,and Security 进入最后一周的学习了,在这最后一周内,需要进行的内容是 public-key 公钥系统的讲 ...

  10. UVa 10048 噪音恐惧症(Floyd)

    https://vjudge.net/problem/UVA-10048 题意: 输入一个C个点S条边的无向带权图,边权表示该路径上的噪声值.输入一些询问,每次询问两个点,输出这两点间最大噪声值最小的 ...