博客
关于我
pthread_create导致的程序崩溃
阅读量:795 次
发布时间:2023-03-04

本文共 605 字,大约阅读时间需要 2 分钟。

今天在使用pthread_create创建线程时,程序一运行就直接死掉了。经过对比,我发现自己在调用pthread_create时传递了NULL作为第一个参数,导致程序崩溃。错误信息显示程序因“Segmentation fault”而终止。根据pthread_create的man pages,第一个参数必须是线程标识符,必须使用-pointer类型,不能传递NULL。

为了修复这个问题,我修改了代码,正确使用了&tid作为第一个参数,并添加了错误检查。编译使用gcc加上-lstdc++和-lpthread。测试后,程序成功运行了。

以下是修复后的代码:

#include 
#include
void *ThreadTest(void *arg) { printf("Enter ThreadTest \r\n");}int main(void) { pthread_t tid; if (pthread_create(&tid, NULL, ThreadTest, NULL) != 0) { printf("Create thread error! \n"); } else { printf("Create thread success! \n"); } getchar(); return 0;}

转载地址:http://xwafk.baihongyu.com/

你可能感兴趣的文章
PROFINET 模拟器使用教程
查看>>
Program type already present: android.support.v4.widget.EdgeEffectCompat
查看>>
PyTorch中文版官方教程来啦(附下载)
查看>>
Progress Kemp LoadMaster 远程命令执行漏洞复现(CVE-2024-1212)
查看>>
Project configuration is not up-to-date with pom.xml. Run Maven->Update Project
查看>>
Project Euler 15 Lattice paths
查看>>
Project Euler 48 Self powers( 大数求余 )
查看>>
Project Euler Problem 12: Highly divisible triangular number
查看>>
ProjectEuler 2
查看>>
projection介绍及EPSG:4326和EPSG:3857的投射转换
查看>>
project打开文件时,显示无法识别此文件格式?
查看>>
Prometheus + Grafana on Kubernetes部署
查看>>
prometheus + grafana进行服务器资源监控
查看>>
Prometheus Alertmanager 告警配置详解
查看>>
Prometheus Grafana 展示平台
查看>>
Prometheus pushgateway使用详解
查看>>
Prometheus 云原生 - Prometheus 数据模型、Metrics 指标类型、Exporter 相关
查看>>
Prometheus 云原生 - 基于 file_sd、http_sd 实现 Service Discovery
查看>>
Prometheus 云原生 - 微服务监控报警系统 (Promethus、Grafana、Node_Exporter)部署、简单使用
查看>>
Prometheus 云原生 - 监控 Linux、MySQL、Redis、RabbitMQ、Docker、SpringBoot 3.x
查看>>