linux 线程创建函数pthread_create的几个传参方式

2024-06-10 14:08

本文主要是介绍linux 线程创建函数pthread_create的几个传参方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近在linux中用到了多线程的一些知识,网上找了一些有关pthread_create函数应用的例子,感觉收获不少,在这里贴出来以供方便学习之。

[文件] test1.c ~ 719B     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *start_routine( void *arg);
int main( int argc, char **argv)
{
   pthread_t tid;
   int code;
   int i;
   for (i = 0; i < 5; i++)
   {
     code = pthread_create(&tid, NULL, start_routine, ( void *) i);
     if (code != 0)
     {
       fprintf (stderr, "Create new thread failed: %s\n" , strerror (code));
       exit (1);
     }
     fprintf (stdout, "New thread created.\n" );
   }
   pthread_exit(( void *) 0);
}
void *start_routine( void *arg)
{
   int id = ( int ) arg;
   fprintf (stdout, "%s:%d:%s() running ...\n" , __FILE__, __LINE__, __func__);
   fprintf (stdout, "id = %d\n" , id);
   return (( void *) 0);
}
// vim:tabstop=8

2. [文件] test2.c ~ 793B     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define THREADS_NUM 5
void *start_routine( void *arg);
int main( int argc, char **argv)
{
   pthread_t tid;
   int id[THREADS_NUM];
   int code;
   int i;
   for (i = 0; i < THREADS_NUM; i++)
   {
     id[i] = i;
     
     code = pthread_create(&tid, NULL, start_routine, &id[i]);
     if (code != 0)
     {
       fprintf (stderr, "Create new thread failed: %s\n" , strerror (code));
       exit (1);
     }
     fprintf (stdout, "New thread created.\n" );
   }
   
   pthread_exit(( void *) 0);
}
void *start_routine( void *arg)
{
   int id = *( int *) arg;
   fprintf (stdout, "%s:%d:%s() running ...\n" , __FILE__, __LINE__, __func__);
   fprintf (stdout, "id = %d\n" , id);
   return (( void *) 0);
}
// vim:tabstop=8

3. [文件] test3.c ~ 714B     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *start_routine( void *arg);
int main( int argc, char **argv)
{
   pthread_t tid;
   int code;
   int i;
   for (i = 0; i < 5; i++)
   {
     code = pthread_create(&tid, NULL, start_routine, &i);
     if (code != 0)
     {
       fprintf (stderr, "Create new thread failed: %s\n" , strerror (code));
       exit (1);
     }
     fprintf (stdout, "New thread created.\n" );
   }
   pthread_exit(( void *) 0);
}
void *start_routine( void *arg)
{
   int id = *( int *) arg;
   fprintf (stdout, "%s:%d:%s() running ...\n" , __FILE__, __LINE__, __func__);
   fprintf (stdout, "id = %d\n" , id);
   return (( void *) 0);
}
// vim:tabstop=8

这篇关于linux 线程创建函数pthread_create的几个传参方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048371

相关文章

linux生产者,消费者问题

pthread_cond_wait() :用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或pthread_cond_broadcast来唤醒它。 pthread_cond_wait() 必须与pthread_mutex 配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread

如何突破底层思维方式的牢笼

我始终认为,牛人和普通人的根本区别在于思维方式的不同,而非知识多少、阅历多少。 在这个世界上总有一帮神一样的人物存在。就像读到的那句话:“人类就像是一条历史长河中的鱼,只有某几条鱼跳出河面,看到世界的法则,但是却无法改变,当那几条鱼中有跳上岸,进化了,改变河道流向,那样才能改变法则。”  最近一段时间一直在不断寻在内心的东西,同时也在不断的去反省和否定自己的一些思维模式,尝试重

ESP32 esp-idf esp-adf环境安装及.a库创建与编译

简介 ESP32 功能丰富的 Wi-Fi & 蓝牙 MCU, 适用于多样的物联网应用。使用freertos操作系统。 ESP-IDF 官方物联网开发框架。 ESP-ADF 官方音频开发框架。 文档参照 https://espressif-docs.readthedocs-hosted.com/projects/esp-adf/zh-cn/latest/get-started/index

Linux 安装、配置Tomcat 的HTTPS

Linux 安装 、配置Tomcat的HTTPS 安装Tomcat 这里选择的是 tomcat 10.X ,需要Java 11及更高版本 Binary Distributions ->Core->选择 tar.gz包 下载、上传到内网服务器 /opt 目录tar -xzf 解压将解压的根目录改名为 tomat-10 并移动到 /opt 下, 形成个人习惯的路径 /opt/tomcat-10

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

【Linux进阶】UNIX体系结构分解——操作系统,内核,shell

1.什么是操作系统? 从严格意义上说,可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境。我们通常将这种软件称为内核(kerel),因为它相对较小,而且位于环境的核心。  从广义上说,操作系统包括了内核和一些其他软件,这些软件使得计算机能够发挥作用,并使计算机具有自己的特生。这里所说的其他软件包括系统实用程序(system utility)、应用程序、shell以及公用函数库等

大学湖北中医药大学法医学试题及答案,分享几个实用搜题和学习工具 #微信#学习方法#职场发展

今天分享拥有拍照搜题、文字搜题、语音搜题、多重搜题等搜题模式,可以快速查找问题解析,加深对题目答案的理解。 1.快练题 这是一个网站 找题的网站海量题库,在线搜题,快速刷题~为您提供百万优质题库,直接搜索题库名称,支持多种刷题模式:顺序练习、语音听题、本地搜题、顺序阅读、模拟考试、组卷考试、赶快下载吧! 2.彩虹搜题 这是个老公众号了 支持手写输入,截图搜题,详细步骤,解题必备

idea lanyu方式激活

访问http://idea.lanyus.com/这个地址。根据提示将0.0.0.0 account.jetbrains.com添加到hosts文件中,hosts文件在C:\Windows\System32\drivers\etc目录下。点击获得注册码即可。

【操作系统】信号Signal超详解|捕捉函数

🔥博客主页: 我要成为C++领域大神🎥系列专栏:【C++核心编程】 【计算机网络】 【Linux编程】 【操作系统】 ❤️感谢大家点赞👍收藏⭐评论✍️ 本博客致力于知识分享,与更多的人进行学习交流 ​ 如何触发信号 信号是Linux下的经典技术,一般操作系统利用信号杀死违规进程,典型进程干预手段,信号除了杀死进程外也可以挂起进程 kill -l 查看系统支持的信号

Windows/macOS/Linux 安装 Redis 和 Redis Desktop Manager 可视化工具

本文所有安装都在macOS High Sierra 10.13.4进行,Windows安装相对容易些,Linux安装与macOS类似,文中会做区分讲解 1. Redis安装 1.下载Redis https://redis.io/download 把下载的源码更名为redis-4.0.9-source,我喜欢跟maven、Tomcat放在一起,就放到/Users/zhan/Documents