关于pthread_create创建线程, 产生segment错误。

本文介绍了一种在Linux环境中遇到的特殊问题:编译正常但在运行时出现段错误的情况,并提供了一个解决方案——在链接.o文件时加入-lpthread参数。
在linux中 编译时没问题, 但是运行时 再创建的时候会产生个segment错误。 如果在连接.o文件时 在命令最后+上 -lpthread 就可以了。 原因还没有找到。 
valgrind --tool=helgrind ./webserver ==3344== Helgrind, a thread error detector ==3344== Copyright (C) 2007-2015, and GNU GPL'd, by OpenWorks LLP et al. ==3344== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info ==3344== Command: ./webserver ==3344== 服务器已启动,监听端口 8080 等待客户端连接... 新客户端连接: 127.0.0.1 ------ 收到请求 ------ GET / HTTP/1.0 Host: 127.0.0.1:8080 User-Agent: ApacheBench/2.3 Accept: */* ---------------------- 请求文件: ./web/Index.html 客户端断开连接: 127.0.0.1 等待客户端连接... 内容类型: text/html, 文件大小: 2348 字节 新客户端连接: 127.0.0.1 ------ 收到请求 ------ GET / HTTP/1.0 Host: 127.0.0.1:8080 User-Agent: ApacheBench/2.3 Accept: */* ---------------------- 请求文件: ./web/Index.html 内容类型: text/html, 文件大小: 2348 字节 客户端断开连接: 127.0.0.1 等待客户端连接... 新客户端连接: 127.0.0.1 ------ 收到请求 ------ GET / HTTP/1.0 Host: 127.0.0.1:8080 User-Agent: ApacheBench/2.3 Accept: */* ---------------------- 请求文件: ./web/Index.html 内容类型: text/html, 文件大小: 2348 字节 客户端断开连接: 127.0.0.1 等待客户端连接... 文件发送完成: 2348/2348 字节 文件发送完成: 2348/2348 字节 新客户端连接: 127.0.0.1 ==3344== ---Thread-Announcement------------------------------------------ ==3344== ==3344== Thread #1 is the program's root thread ==3344== ==3344== ---Thread-Announcement------------------------------------------ ==3344== ==3344== Thread #3 was created ==3344== at 0x51644DE: clone (clone.S:74) ==3344== by 0x4E46149: create_thread (createthread.c:102) ==3344== by 0x4E47E83: pthread_create@@GLIBC_2.2.5 (pthread_create.c:679) ==3344== by 0x4C34BB7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) ==3344== by 0x401868: main (webserver.c:239) ==3344== ==3344== ---------------------------------------------------------------- ==3344== ==3344== Possible data race during write of size 1 at 0x6828697 by thread #1 ==3344== Locks held: none ==3344== at 0x4C3A3CC: mempcpy (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) ==3344== by 0x401378F: _dl_allocate_tls_init (dl-tls.c:515) ==3344== by 0x4E47CED: get_cached_stack (allocatestack.c:254) ==3344== by 0x4E47CED: allocate_stack (allocatestack.c:501) ==3344== by 0x4E47CED: pthread_create@@GLIBC_2.2.5 (pthread_create.c:539) ==3344== by 0x4C34BB7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) ==3344== by 0x401868: main (webserver.c:239) ==3344== ==3344== This conflicts with a previous write of size 1 by thread #3 ==3344== Locks held: none ==3344== at 0x4E47622: start_thread (pthread_create.c:265) ==3344== Address 0x6828697 is in a rw- anonymous segment ==3344== 文件发送完成: 2348/2348 字节 接收请求失败: Bad file descriptor 客户端断开连接: 127.0.0.1 等待客户端连接... 新客户端连接: 127.0.0.1 ------ 收到请求 ------ GET / HTTP/1.0 Host: 127.0.0.1:8080 User-Agent: ApacheBench/2.3 Accept: */* ---------------------- 请求文件: ./web/Index.html 内容类型: text/html, 文件大小: 2348 字节 ==3344== ==3344== Process terminating with default action of signal 13 (SIGPIPE) ==3344== at 0x4E509FF: send (send.c:26) ==3344== by 0x401587: handle_request (webserver.c:158) ==3344== by 0x401185: handle_request_wrapper (webserver.c:56) ==3344== by 0x4C34DB6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) ==3344== by 0x4E476B9: start_thread (pthread_create.c:333) ==3344== ==3344== For counts of detected and suppressed errors, rerun with: -v ==3344== Use --history-level=approx or =none to gain increased speed, at ==3344== the cost of reduced accuracy of conflicting-access information ==3344== ERROR SUMMARY: 32 errors from 1 contexts (suppressed: 1435 from 240) 已杀死
最新发布
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值