当前位置:网站首页>[learning notes] thread creation
[learning notes] thread creation
2022-07-28 06:56:00 【Step on a meteor】
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
struct class {
char str[20];
int num;
};
struct class *do_func1(struct class *data)
{
memset(data, 0, sizeof(struct class));
strcpy(data->str,"liuxing");
data->num = 23;
return data;
}
struct class *do_func2(struct class *data)
{
memset(data, 0, sizeof(struct class));
strcpy(data->str,"star");
data->num = 25;
return data;
}
void *thread_func1(void *arg)
{
pthread_t pthread;
struct class *data = (struct class*)arg;
//struct class *redata = (struct class*)malloc(sizeof(struct class));
printf("child1 thread, ID is %ld\n", pthread_self());
do_func1(data);
return arg;
}
void *thread_func2(void *arg)
{
pthread_t pthread;
struct class *data = (struct class*) arg;
printf("child2 thread, ID is %ld\n", pthread_self());
do_func2(data);
return arg;
}
int main()
{
pthread_t pthread1;
pthread_t pthread2;
struct class *data = (struct class*)malloc(sizeof(struct class));
printf("main thread, ID is %lu\n", pthread_self());
if (pthread_create(&pthread1, NULL, (void*)thread_func1, (void*)data) != 0) {
printf("thread creation failed!\n");
return -1;
}
pthread_join(pthread1, &data);
printf("class str = %s, num = %d\n", data->str, data->num);
if (pthread_create(&pthread2, NULL, (void*)thread_func2, data) != 0) {
printf("thread creation failed!\n");
return -1;
}
pthread_join(pthread2, &data);
printf("class str = %s, num = %d\n", data->str, data->num);
return 0;
}
边栏推荐
- MySQL master master
- MySQL主从
- Dynamic memory management function of C language
- Technology sharing | do you know the functions of the server interface automated testing and requests library?
- Tcp/ip five layer model
- 测试人生 | 二线城市年薪超40W?疫情之下涨薪100% + 是怎么做到的?
- SSH服务配置
- Yapi vulnerability hanging horse program chongfu.sh processing
- DNS域名解析服务
- PKU-2739-Sum of Consecutive Prime Numbers(筛素数法打表)
猜你喜欢

Technology sharing | interface testing value and system

DNS forward resolution experiment

技术分享 | 使用 cURL 发送请求

Detailed explanation of LNMP construction process

Principle and configuration of NAT and pat

DNS域名解析服务

Technology sharing | how to simulate real use scenarios? Mock technology to help you

MySQL主主

浅谈Cookie和Session

iptables防火墙
随机推荐
raid磁盘阵列
Cocos2d-x learning notes Tile Map tiledmap
测试面试题集锦(二)| 测试工具篇(附答案)
Question brushing record - linked list
FTP服务
HDU-2036-改革春风吹满地(多边形面积模板)
HDU-5806-NanoApeLovesSequenceⅡ(尺取法)
What kind of air conduction Bluetooth headset with good configuration is recommended
Hdu-5805-nanoape loves sequence (thinking questions)
链表中结点的插入和删除
Technology sharing | do you know the functions of the server interface automated testing and requests library?
防火墙——iptables防火墙(四表五链、防火墙配置方法、匹配规则详解)
技术分享 | 使用postman发送请求
软件开发中常见模型
测试面试题集锦(三)| 计算机网络和数据库篇(附答案)
LNMP搭建过程详解
技术分享 | 接口测试常用代理工具
Common models in software development
MySQL常用命令
iptables防火墙