当前位置: 首页 > ops >正文

C语言多线程

文章目录

    • 创建线程
    • 实战

创建线程

在C语言中创建线程方式如下

#include <pthread.h>
pthread_create (thread, attr, start_routine, arg)

其中,thread为线程指针;attr为线程属性,默认NULLstart_routine为线程运行函数起始地址;arg为运行函数的参数。无参数时使用NULL

如果线程成功创建,函数返回0,否则说明创建失败。

当线程完成工作后,若无需继续存在,则会调用pthread_exit函数。也就是说,这个函数可以显式地退出一个线程。

pthread_exit (status)

实战

下面做一个简单的示例,通过调用多线程来打印数字

//test1.c
#include<stdio.h>
#include<pthread.h>
#define NUM_THREADS 5void *PrintTh(void *th){int i = *((int*)th);printf("Hello, I'm thread %d\n", i);return 0;
}int main(){int i,ret;pthread_t p;for(i=0; i<NUM_THREADS; i++){printf("create th %d\n",i);ret = pthread_create(&p,NULL, PrintTh, (void*)&i);if(ret != 0)printf("th %d error, code = \n",i);}pthread_exit(NULL);return 0;
}

如果在Linux中使用pthread,需要使用库libpthread.a, 编译时要加-lpthread参数:

gcc test1.c -lpthread -o test1.o ./test1.

运行结果如下


create th 0
create th 1
Hello, I’m thread 1
Hello, I’m thread 2
create th 2
Hello, I’m thread 3
create th 3
create th 4
Hello, I’m thread 4
Hello, I’m thread 4


之所以main函数和printTh输出的值不同,是因为我们传入printTh的参数是一个空指针,这个空指针直接指向了用于循环的i的地址,所以printTh调用i的时候,有时i的取值刚好发生变化,有时尚未发生变化。

由于函数pthread_create接收的传输参数必须为void*,而且只有一个,故当需要传递多个参数时,需要用到结构体。

// test2.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define NUM_THREADS 5typedef struct PERSON{int id;char name;
}Person, *pPerson;void * PrintTh(void * p)
{pPerson tid = (pPerson)p;printf("I'm %c ,th %d\n", tid->name,tid->id);return 0;
}int main(void)
{pthread_t Pthread[NUM_THREADS];Person persons[NUM_THREADS];int i, ret;for (i = 0; i < NUM_THREADS; i++){printf("create th %d \n",i);persons[i].id = i;persons[i].name = 'A'+i%10;ret = pthread_create(&Pthread[i], NULL, PrintTh, (void *)&persons[i]);if (0 != ret){printf("Error: 创建线程失败!\n");exit(-1);}}pthread_exit(NULL);return 0;
}

输出结果为


create th 0
create th 1
I’m A ,th 0
I’m B ,th 1
create th 2
create th 3
I’m C ,th 2
I’m D ,th 3
create th 4
I’m E ,th 4


http://www.xdnf.cn/news/11258.html

相关文章:

  • iPhone1.1.1版简体中文汉化完全教程
  • 港台术语与内地术语之对照
  • 2024亲测可用最新PHP仿猪八戒任务威客网整站源码/在线接任务网站源码
  • Word技能-更改 Normal 模板 (Normal.dotm)
  • termux从入门到入坑
  • ORA-01157 cannot identify/lock data file n 故障一例
  • 2021年,用更现代的方法使用PGP(下)
  • 日志格式规范
  • Web服务器群集:LVS负载均衡群集
  • 层次分析法原理及实例(AHP)
  • 微信接入AI 机器人方法大全,全网最细教程
  • visual studio-wdk8.1+vs2013中使用winusb模版开发usb设备驱动
  • linux:services服务器配置
  • 23种PHP开发工具PHP IDE集合
  • CURL命令 : GET、POST请求、文件下载等常用命令
  • 我们的生日花
  • 【Linux】Ubuntu12.04的下载与安装
  • appfuse是什么
  • 将字符串切割成数组 componentsSeparatedByString
  • C语言程序设计:学生成绩管理系统(附代码以及编写逻辑)
  • SAP 金税接口介绍
  • 文件快速定位神器(C++小项目实战)
  • 【Maven学习】Nexus OSS私服仓库的安装和配置
  • 从osCommerce到Zen Cart,再到CubeCart
  • java环境变量
  • 区块链的介绍和应用场景以及发展趋势
  • TP-link路由器如何进行端口映射?
  • 程序设计模式
  • jdk 命令大全
  • iText输出中文的三种字体选择方式