// 基于线程的并发编程,向线程传递参数2(传递结构体)
// Test_3_createThread
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define NUM_Threads 5
typedef struct thread_data{
int threadid;
char message;
}THDATA,*PTHDATA;
void * PrintHello(void * pthreadid)
{
while(1)
{
PTHDATA tid = (PTHDATA)pthreadid;
printf("This is Pthread : %d ;info : %c \n",tid->threadid, tid->message);
}
return 0;
}
int main(void)
{
pthread_t Pthread[NUM_Threads];
THDATA index[NUM_Threads];
int i, ret;
for (i = 0; i < NUM_Threads; i++)
{
printf("main() : create thread %d \n",i);
index[i].threadid = i;
index[i].message = 'A'+i%10;
ret = pthread_create(&Pthread[i], NULL, PrintHello, (void *)&index[i]);
if (0 != ret)
{
printf("Error: create thread failed\n");
exit(-1);
}
}
pthread_exit(NULL);
return 0;
}
/*
* 在CLion(Ubuntu)中输出结果是
main() : 创建线程 0
main() : 创建线程 1
This is Pthread : 0 ;info : A
main() : 创建线程 2
main() : 创建线程 3
This is Pthread : 2 ;info : C
main() : 创建线程 4
This is Pthread : 3 ;info : D
This is Pthread : 4 ;info : E
This is Pthread : 1 ;info : B
* */
没有合适的资源?快使用搜索试试~ 我知道了~
pthread-test
共4个文件
makefile:1个
o:1个
dev_diag:1个
需积分: 0 0 下载量 26 浏览量
2022-11-18
15:52:57
上传
评论
收藏 9KB RAR 举报
温馨提示
c语言多线程 测试demo 诊断工具小插件
资源推荐
资源详情
资源评论
收起资源包目录
pthread_test.rar (4个子文件)
pthread_test
main.c 1KB
dev_diag 12KB
Makefile 719B
main.o 7KB
共 4 条
- 1
资源评论
LJ-SEU
- 粉丝: 110
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功