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. 使用dll,将image库开放出去

    这是很经典的想法了,但是如何来做,不经过摸索不知道细节.   最简单: dll处   #include "stdafx.h"   #ifdef _DEBUG #define new ...

  2. 洛谷月赛 Hello World(升级版) - 动态规划

    题目背景 T1答案要mod1000000007(10^9+7),请重新提交,非常抱歉! 一天,智障的pipapi正在看某辣鸡讲义学程序设计. 题目描述 在讲义的某一面,他看见了一篇文章.这篇文章由英文 ...

  3. 使用libcurl开源库和Duilib做的下载文件并显示进度条的小工具

    转载:http://blog.csdn.net/mfcing/article/details/43603525 转载:http://blog.csdn.net/infoworld/article/de ...

  4. DSDS,双模,双卡,双待,单待,双通,单通,概念及相互关系?【转】

    本文转载自:https://blog.csdn.net/dirk_it/article/details/7178058?utm_source=blogxgwz9 DSDS:双卡双待 DualSimDu ...

  5. Git 基础 —— 安装 配置 别名 对象

    Git 基础学习系列 Git 基础 -- 安装 配置 别名 对象 Git 基础 -- 常用命令 Git 基础 -- 常见使用场景 Git基础 -- Github 的使用 Git 安装 Git下载地址 ...

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

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

  7. Java-master(github)教材整理

    helloworld class HelloWorld { public static void main(String[] args) { System.out.println("hell ...

  8. C# WinCE项目 VS2008 单例窗体实现

    项目现有主界面FormMain,模板界面FormModel,其余5个子界面皆继承自模板. 现在想要实现在主界面下可以打开任意子界面,并且可以随时关闭.当打开的子窗体未执行Close事件时,要保证每次显 ...

  9. vim E437: terminal capability "cm" required

    报错E437: terminal capability "cm" required 解决方法:# export TERM=xterm

  10. 《A_Pancers》第二次作业 基于Android系统的音乐播放系统项目开题报告

    小组名 N A B C D 总分 Just_Do_IT! 8 8 9 9 8 42 Miracle-House 8 8 7 8 8 39 ymm3  9 8 8 8 8 41 Spring_Four  ...