pthread 简要使用指南(一) pthread_create

Linux pthread_create源码分析 本文介绍pthread_create函数的使用和源码分析。 阅读详情

    

    POSIX thread 简称为pthread,Posix线程是一个POSIX标准线程。该标准定义内部API创建和操纵线程。 Pthreads定义了一套 C程序语言类型、函数与常量,它以 pthread.h 头文件和一个线程库实现。

     直接上代码,简单明了。


#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

// 线程ID
pthread_t       ntid;
// 互斥对象
pthread_mutex_t mutex;

int             count;

void printids(const char *s)
{
    pid_t 	pid;
    pthread_t 	tid;

    pid = getpid();
    tid = pthread_self();

    printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,
        (unsigned int)tid, (unsigned int)tid);
}

// 线程函数
void *thr_fn(void *arg)
{
    printids("new thread begin\n");

    // 加锁
    pthread_mutex_lock(&mutex);

    printids("new thread:");

    int i=0;
    for ( ; i < 5; ++i )
    {
        printf("thr_fn runing %d\n", count++);
    }

    // 释放互斥锁
    pthread_mutex_unlock(&mutex);

    return ( (void*)100);
}

int main(void)
{
    int err;

    count = 0;
    // 初始化互斥对象
    pthread_mutex_init(&mutex, NULL);

    // 创建线程
    err = pthread_create(&ntid, NULL, thr_fn, NULL);
    if ( 0 != err )
    {
        printf("can't create thread:%s\n", strerror(err));
    }

    // sleep(5);
    pthread_mutex_lock(&mutex);  
    printids("main thread:");
    int i=0;
    for ( ; i < 5; ++i )
    {
        printf("main runing %d \n", count++);
    }
    sleep(1);
    pthread_mutex_unlock(&mutex);

    int **ret;
    pthread_join(ntid, (void**)ret);
    printf("thr_fn return %d\n", *ret);

    pthread_mutex_destroy(&mutex);


    return 0;
}

     
    这个例子,用到了pthread的数据结构:
    pthread_t,  这个结构来标识线程ID;
    pthread_mutex_t, 这个用来做互斥;


    用到了如下函数:
    pthread_self() :  获得线程自身的ID;

    pthread_create(&ntid, NULL, thr_fn, NULL): 线程创建函数
    原型:int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void*(*start_routine)(void*),  void *arg)。
    tid : 新创建线程的线程ID;
    attr: 指定新创建线程的线程属性,为NULL即为使用默认属性;
    start_routine : 这是一个函数指针,用来指向线程函数;
    arg : 传递给线程函数的指针;    
    返回值 : 0代表成功。 失败,返回的则是错误号。

    pthread_join(ntid, (void*)ret):等待一个线程的结束   
    原型:int pthread_join(pthread_t thread, void **retval);
    thread : 需要等待的线程的ID;
    retval : 存放等待线程的返回值;
    返回值 : 0代表成功。 失败,返回的则是错误号。
    
    pthread_mutex_init(&mutex, NULL):初始化互斥锁,以动态方式创建互斥锁
    原型:int pthread_mutex_init(pthread_mutex_t *restrict mutex,
              const pthread_mutexattr_t *restrict attr);
    mutex : 被初始化的锁对象;
    attr  : 指定新建互斥锁的属性。为NULL,则使用默认的互斥锁属性,默认属性为快速互斥锁。

    pthread_mutex_lock(&mutex)     : 加锁
    pthread_mutex_unlock(&mutex) : 释放锁

    pthread_mutex_destroy(&mutex): 销毁锁对象
    
    
    编译的时候,需要加上编译参数“-lpthread”。
    可以屏蔽pthread_mutex_lock(),pthread_mutex_unlock()看下没有互斥的结果。



Pthread多线程编程指南 多线程编程指南,SUN的Pthread线程库手册,中文版的。 立即下载

相关推荐

多线程编程指南(中文版)+pthread primer(英文)

多线程编程指南(中文版)+pthread primer 啥都不说了,自己下吧

关于pthread_create的手册

要查询pthread_create函数的用法,需要执行以下命令:sudo apt-get install manpages-posix-dev

lanseshenhua的专栏 758

【STM32】STM32F407实现简单的Modbus协议

1.ModBus协议简介 MODBUS协议是一种串行通信协议,由Modicon公司(施耐德公司前身)发表,由于其公开发表且无版权要求,易于部署和维护,在工业界广泛应用。MODBUS采用主从通信(Master/Slave),MODBUS有三种报文格式:ASCII、RTU、TCP,本文主要讨论RTU。 如下图所示,串行通信上的MODBUS协议主要由地址,功能码,数据,CRC校验四部分数据帧构成。主机和从机的串行通信设备要求一致,参数要求一致,即假设使用串口,主从机波特率,奇偶校验...

BadBoyHolly的博客 8206

Pthread使用手册

#include 1、创建 int pthread_create( pthread_t *tid, const pthread_attr_t *attr, void *(* func) (void *), void *arg ); attr: 线程属性包括:优先级、初始栈大小,是否应该成为一个守护线程。 缺省设置,NULL 后面是线程要执行的函数和参数 成功返回 0 2、等待一个给定线程终止 int pthread_join( pthread_t tid, void **status);

起跑线的专栏 995

Shopee接口安全机制深度解析:从签名参数到稳定采集实战

本文深度解析了Shopee平台接口的安全机制,重点剖析了其核心签名参数`x-sap-ri`和`x-sap-sec`的动态生成逻辑。通过逆向分析与实战演示,文章提供了使用Python模拟签名算法、构建稳定数据采集方案的完整思路与代码框架,并探讨了应对密钥动态更新等长期运行挑战的策略。

weixin_29323049的博客 400

常微分方程 (ODE) 和 随机微分方程 (SDE)

常微分方程(Ordinary Differential Equations, ODE)和随机微分方程(Stochastic Differential Equations, SDE)是数学中描述系统动态行为的重要工具。它们有一些相似之处,但在处理随机性方面存在显著差异。

weixin_39699362的博客 4389

Linux应用程序:pthread_create基本用法

本文参照pthread_create的man手册,演示了pthread_create的基本用法。

qq_39444308的博客 318

ClaudeCode 通过 CC Switch 配置 Ollama 本地大模型

本文介绍了如何让Claude Code使用本地免费模型qwen3.5:9b而无需Anthropic API Key的方法。主要内容包括:1)安装Claude Code、CC Switch代理工具和Ollama本地模型运行时;2)配置环境变量和设置文件将请求转发到本地服务;3)详细说明了系统架构和工作原理,展示请求如何从Claude Code经CC Switch代理到Ollama本地模型。该方案通过代理拦截和转发机制,实现了使用免费开源模型替代官方API的功能。

稚枭天卓 3235

【linux】把进程/线程绑定到特定cpu核上运行

目的 某个进程需要较高的运行效率时,就有必要考虑将其绑定到单独的核上运行,以减小由于在不同的核上调度造成的开销。 把某个进程/线程绑定到特定的cpu核上后,该进程就会一直在此核上运行,不会再被操作系统调度到其他核上。但绑定的这个核还是可能会被调度运行其他应用程序的。 命令绑定 查看绑定情况 查看进程pid现在的绑核情况 taskset -p pid pid 2726's current affinity mask: 6 显示的十进制数字6--->转换为2进制是110,每个1对应一个cp

我的笔记本 2万+

手把手教你学Simulink--逆变器场景:基于Simulink的单相全桥逆变器PWM控制仿真建模

本文介绍了使用Simulink和Simscape Electrical库进行单相全桥逆变器PWM控制仿真建模的详细步骤。主要内容包括:从创建Simulink项目、添加电力电子组件、构建逆变器模型,到设置参数、运行仿真和结果分析。重点讲解了逆变器核心模块的配置、PWM信号生成、负载建模等关键环节,并提供了优化改进建议。该指南适用于电力电子初学者,通过仿真可以帮助理解单相全桥逆变器的工作原理和PWM控制技术,为实际应用和算法开发提供基础平台。

MHD0815的博客 671

llama-factory微调qwen2.5-vl

使用llama-factory完成qwen2.5-vl微调

qq_44908396的博客 2940

g++编译pthread_create函数的注意点

1.在c++中函数的参数是和函数一起进行编译的,参数列表为:int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*),因此回调函数定义时参数中一定要有个void*的参数 2.pt

冒大烟的羊肉串的专栏 5037

【100%通过率】华为od统一考试B卷【查找接口成功率最优时间段】Python 实现

服务之间交换的接口成功率作为服务调用关键质量特性,某个时间段内的接口失败率使用一个数组表示,数组中每个元素都是单位时间内失败率数值,数组中的数值为0~100的整数, 给定一个数值(minAverageLost)表示某个时间段内平均失败率容忍值,即平均失败率小于等于minAverageLost,找出数组中最长时间段,如果未找到则直接返回NULL。

MISAYAONE的博客 5875

多线程之pthread_create()函数

总述:pthread_create是(Unix、Linux、Mac OS X)等操作系统的创建线程的函数。它的功能是创建线程(实际上就是确定调用该线程函数的入口点),在线程创建以后,就开始运行相关的线程函数。 pthread_create的返回值表示成功,返回0;表示出错,返回表示-1。 pthread_create函数如何创造线程 函数原型声明: #include &lt;pth...

wushuomin的博客 16万+

Mathtype7.4 嵌入至 Office 365

将mathtype7.4导入word中

加斯顿工程师的博客 4871

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

与线程有关的几个函数pthread_create、pthread_detach、pthread_join、pthread_mutex_lock、pthread_cond_wait等用法

     线程,有时被称为轻量进程,在我们平常开发中经常会用到,关于线程的介绍网上有很多,在这里我就不再做介绍了。我主要介绍下载Linux我们经常用到的几个与线程有关的函数。 pthread_create     创建线程函数,函数声明为         int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *res...

haofeng_ma的博客 5275

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

C语言笔记 pthread_create pthread_join 和 pthread_detach

头文件 #include<pthread.h> pthread_create int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, (void*)(*start_rtn)(void*),void *arg); 第一个参数为指向线程标识符的指针。 第二个参数用来设置线程属性。 第三个参数是线程运行...

flyconley的博客 3090

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

linux创建线程之pthread_create()函数

创建线程是每个linux开发者都会用到的。在这里介绍一下pthread_create()函数。我们来看一看怎么创造一个线程。 首先查看pthread_create()函数的定义: PTHREAD_CREATE(3) Linux Programmer's Manual PTHREAD_CREATE(3) NAME top pthread_create - create a new thread SYNOPSIS top

StevenYang2008的博客 3368

Linux多线程学习(一)pthread_create

Linux系统下的多线程遵循POSIX线程接口,称为pthread。 #include int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void),  void *restrict arg); Returns: 0 if

笑也有泪的专栏 7613

pthread_jion() , pthread_create() 导致的segmentation fault

下午调试程序的“segmentation fault”,先上代码:int main() { printf("entering main() /n"); init_globals(); /*initialize global variable defined in main.c*/ init_iic(); init_spi(); if(init_sock() == -1){ exit(1); } sock_listen_tid = pth

DOS5GW的专栏 3828

多线程之pthread_create()

总述:pthread_create是(Unix、Linux、Mac OS X)等操作系统的创建线程的函数。它的功能是创建线程(实际上就是确定调用该线程函数的入口点),在线程创建以后,就开始运行相关的线程函数。 pthread_create的返回值表示成功,返回0;表示出错,返回表示-1。 pthread_create函数如何创造线程函数原型声明: #include <pthread.h> int pthread_create( pthread_t *res

CodeAllen嵌入式 3万+

线程控制原语之pthread_self和pthread_create函数

注意:使用线程库函数用gcc编译时,要加参数:-lpthread(libpthread.so),因为线程库函数属于第三方c库函数,不是标准库函数(/lib、/usr/lib或者/usr/local/lib)。 (1)pthread_self函数 获取线程ID。其作用对应进程中 getpid() 函数。 pthread_t pthread_self(void); 返回值:成功:调用该函数的...

luciusvorenus 4485

pthread_create()之前的基本设置

一.pthread_create()之前的属性设置 1.线程属性设置 我们用pthread_create函数创建一个线程,在这个线程中,我们使用默认参数,即将该函数的第二个参数设为NULL。的确,对大多数程序来说,使用默认属性就够了,但我们还是有必要来了解一下线程的有关属性。 属性结构为pthread_attr_t,它同样在头文件pthread.h中定义,属性值不能直接设置,须使用相关函数进

鱼思故渊的专栏 1711

linux多线程之pthread_create

Linux系统下的多线程遵循POSIX线程接口,称为pthread #include /*功能:创建线程参数: thread_id: 指向线程标识符的指针 attr: 设置线程属性,NULL为默认属性 start_routine: 指向线程运行函数的指

lingdxuyan的专栏 5465

pthread_create()详解

pthread_create()函数声明 #include <pthread.h> int pthread_create(pthread_t * thread ,const pthread_attr_t * attr , void *(* start_routine )(void *),void * arg ); ...

qq_36415932的博客 4420

线程创建之重要属性PTHREAD_CREATE_DETACHED

#include int pthread_join(pthread_t thread, void **value_ptr); int pthread_detach(pthread_t thread); int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); int pthread_attr_set

Vinco's special column 2万+
上一篇: XCode 4.2支持iOS 5.1.1真机调试的实现
下一篇: pthread 简要使用指南(二) joinable 与 detached
hslinux
博客等级 码龄25年 212粉丝 46原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址