当前位置:网站首页>7.4 线程退出
7.4 线程退出
2022-04-21 23:31:00 【大狮熊】
实验代码
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
int sum = 0;
void *thread_start (void *arg)
{
int i;
int len = *(int*)arg;
for (i = 0; i < len; i++)
{
sum += i;
sleep (1);
printf ("sub thread: sum = %d\n", sum);
}
printf ("sub thread: exit\n");
//pthread_exit (NULL);
exit (0);
}
int main (void)
{
pthread_t tid;
int arg = 10;
pthread_create (&tid, NULL, thread_start, (void *) &arg);
sleep (15);
pthread_cancel (tid);
printf ("main thread: sum = %d\n", sum);
printf ("main thread: exit\n");
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <stdlib.h>
void * thread_start (void *arg)
{
int arg_data = *((int *)arg);
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 50000; j++);
printf ("received arg%d = %d\n", arg_data, arg_data);
pthread_exit (NULL);
}
}
int main (void)
{
pthread_t tid;
int arg1 = 1;
int arg2 = 2;
int ret = pthread_create (&tid, NULL, thread_start, (void *) &arg1);
if (ret)
{
perror ("pthread_create");
exit (EXIT_FAILURE);
}
thread_start ((void *) &arg2);
return 0;
}
版权声明
本文为[大狮熊]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38105227/article/details/124325158
边栏推荐
- 2022/4/21
- 【接口测试基础】第三篇 | 传统风格接口与RESTful风格接口区别
- 多表创建视图问题:修改视图数据时报1062
- (三)瑞芯微rk3568 ssh 替换 dropbear
- Golang force buckle leetcode 479 Maximum palindrome product
- The pattern should be large and the vision should be broad, and the humanitarian spirit should be upheld [continuous updating, do not delete]
- Apache failed to start, MySQL login permission problem, troubleshooting method
- 云原生架构下的微服务选型和演进
- 【MYSQL优化1】mysql优化思路
- 【接口测试基础】第四篇 | 接口测试流程及接口文档解析
猜你喜欢

.100滚轮事件

Buuctf Netherlands broadband data disclosure

新独立版抖音口红机全修复版本附视频教程

瑞芯微芯片AI部分开发记录 第一节 《PC端环境搭建1》

BUUCTF 你(竟)然赶我走

Sélection et évolution des microservices dans l'architecture native du cloud

【接口测试基础】第五篇 | 接口用例设计详解

经典题目刷一刷

Ruffian Heng embedded: talk about the application and influence of system watchdog wdog1 in the startup of i.mxrt1xxx system

Amazing, 4 high-quality software full of surprises, feel more comfortable to use
随机推荐
【转载】Postman-Omysql连接数据库
golang力扣leetcode 386.字典序排数
selenium点击的元素被遮挡无法操作的解决办法
如何构建一个可“持续演进”的可观测体系?| QCon
File operation and IO
Simple and easy to collect navigation website source code
[H.264] simple encoder and SPS
(7) Ruixin micro rk3568 builderoot adds compiled scripts and binary program files
经典题目刷一刷
继华为仓颉后,再现4款国产编程语言,形式多样,有一款0代码
Apache failed to start, MySQL login permission problem, troubleshooting method
QT custom control 01 simple timer
多表创建视图问题:修改视图数据时报1062
. 101 keyboard events
【MYSQL优化3】观察mysql进程状态
A collection of large factories and face classics. Do you know these knowledge points
Leetcode:443 Compressed string
信噪比和信干噪比
GO语言学习——Go语言基础之流程控制二
Golang force buckle leetcode 479 Maximum palindrome product