多线程段错误-多线程挂掉----哪个大神可以解决下,多谢啦!

问题描述

多线程挂掉----哪个大神可以解决下,多谢啦!

1 #include
2 #include
3 #include
4 #include
5 #define PAI 3.14159
6 void* area(void* arg){
7 double r = (double)arg;
8 double* s=malloc(sizeof(double));
9 s = PAI * r * r;
10 return s;
11 }
12 int main(void) {
13 printf("r=");
14 double r;
15 scanf("%lf", &r);
16 pthread_t tid;
17 int error=pthread_create(&tid, NULL, area, &r);
18 if(error){
19 errno=error;
20 printf("%mn");
21 }else {
22 printf("pthread_create successn");
23 }
24 #if 0
25 double
a;
26 pthread_join(tid, (void**)&a);
27 printf("s=%gn", a);
28 free(a);
29 a=NULL;
30 #endif
31 #if 0
32 double
* a = (double**)malloc(sizeof(double));
33 pthread_join(tid, (void**)a);
34 printf("s=%gn", (double)a);
35 free(*a);
36 a=NULL;
37 free(a);
38 a=NULL;
39 #endif
40 #if 0
40 #if 0
41 double
b=(double*)malloc(sizeof(double));
42 double** a=&b;
43 int error1=pthread_join(tid, (void**)a);
44 if(error1){
45 errno=error1;
46 printf("%mn");
47 }
48 printf("s=%gn", (double)a);
49 free(*a);
50 a=NULL;
51 free(b);
52 b=NULL;
53 #endif
54 #if 1
55 double
* a;
56 int error1=pthread_join(tid, (void**)a);
57 if(error1){
58 errno=error1;
59 printf("%mn");
60 }
61 printf("s=%gn", (double)a);
62 free(*a);
63 *a=NULL;
64 #endif
65 return 0;
66 }

执行结果 :

r=4
pthread_create success
段错误 (核心已转储)

gdb跟踪调试结果:

54 #if 1
55 double** a;
56 int error1=pthread_join(tid, (void**)a);
57 if(error1){
58 errno=error1;
59 printf("%mn");
60 }
61 printf("s=%gn", (double)a);
62 free(*a);
63 *a=NULL;
(gdb) l
64 #endif
65 return 0;
66 }
(gdb) b 54
Breakpoint 1 at 0x8048689: file ret.c, line 54.
(gdb) r
Starting program: /home/liushiwei/liushiwei/unix/14/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
r=3
[New Thread 0xb7dffb40 (LWP 7045)]
pthread_create success
[Thread 0xb7dffb40 (LWP 7045) exited]

Breakpoint 1, main () at ret.c:56
56 int error1=pthread_join(tid, (void**)a);
(gdb) p a
$1 = (double **) 0x8048709
(gdb) p *a
$2 = (double *) 0x18ebc381
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0xb7fb2e59 in pthread_join () from /lib/i386-linux-gnu/libpthread.so.0
(gdb) c
Continuing.

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb)

注意:前面三个#if 0 到#endif 都可以执行成功,但最后一个#if 1 到#endif,段错误,请大神帮帮小弟,已经纠结几天了,不知道怎么解决。谢谢!

解决方案

一级指针也要捆绑存储区呀

解决方案二:

double* a;
56 int error1=pthread_join(tid, (void**)a);

指针值为空...没有存储空间...

解决方案三:

你这代码真够乱的,报段错误多为数组越界,指针使用错误,指针未分配存储空间,你好好看看你出问题的代码

解决方案四:

55 double* a
应该是这里的问题,下面代码运行之前没有给申请空间。
给个内存空间试试吧。

解决方案五:

ps:觉得你的代码中好多用法都没有见过,你是不是传说中的大神、高手?

解决方案六:

sorry,我没仔细看代码,抄错好多,下面是整理后的代码,不必看前三个#if 0到对应的#endif之间的代码 (这三段代码都是调试用的,不会执行),
只看最后一个 #if 1 到最后一个 #endif 之间的代码,为什么在第56行报段错误?
pthread_join()函数的第二个参数是线程过程函数的返回值的地址---输出参数。
就是说,我如果声明一个二级指针,如果我直接用malloc()分配堆内存,给它初始化,不会有问题,但如果不初始化,就不可以(一级指针不用初始化也行)。

1 #include
2 #include
3 #include
4 #include
5 #define PAI 3.14159
6 void* area(void* arg){
7 double r = (double)arg;
8 double* s=malloc(sizeof(double));
9 s = PAI * r * r;
10 return s;
11 }
12 int main(void) {
13 printf("r=");
14 double r;
15 scanf("%lf", &r);
16 pthread_t tid;
17 int error=pthread_create(&tid, NULL, area, &r);
18 if(error){
19 errno=error;
20 printf("%mn");
21 }else {
22 printf("pthread_create successn");
23 }
24 #if 0
25 double
a;
26 pthread_join(tid, (void**)&a);
27 printf("s=%gn", a);
28 free(a);
29 a=NULL;
30 #endif
31 #if 0
32 double
* a = (double**)malloc(sizeof(double));
33 pthread_join(tid, (void**)a);
34 printf("s=%gn", (double)a);
35 free(*a);
36 a=NULL;
37 free(a);
38 a=NULL;
39 #endif
40 #if 0
41 double
b=(double*)malloc(sizeof(double));
42 double** a=&b;
43 int error1=pthread_join(tid, (void**)a);
44 if(error1){
45 errno=error1;
46 printf("%mn");
47 }
48 printf("s=%gn", (double)a);
49 free(*a);
50 a=NULL;
51 free(b);
52 b=NULL;
53 #endif
54 #if 1
55 double
* a;
56 int error1=pthread_join(tid, (void**)a);
57 if(error1){
58 errno=error1;
59 printf("%mn");
60 }
61 printf("s=%gn", (double)a);
62 free(*a);
63 *a=NULL;
64 #endif
65 return 0;
66 }

GDB调试没有变化中,还是找走到第56行, 系统发SIGSEGV信号(段错误)。哪个大神,帮一下,谢谢!。

解决方案七:

妹的!!!!!,重新整理过后的代码还是有错误,粘贴时,粘贴板会把** 粘贴成* ,再次重新整理一次,以下面的代码为准。

1 #include
2 #include
3 #include
4 #include
5 #define PAI 3.14159
6 void* area(void* arg){
7 double r = (double)arg;
8 double* s=malloc(sizeof(double));
9 s = PAI * r * r;
10 return s;
11 }
12 int main(void) {
13 printf("r=");
14 double r;
15 scanf("%lf", &r);
16 pthread_t tid;
17 int error=pthread_create(&tid, NULL, area, &r);
18 if(error){
19 errno=error;
20 printf("%mn");
21 }else {
22 printf("pthread_create successn");
23 }
24 #if 0
25 double
a;
26 pthread_join(tid, (void**)&a);
27 printf("s=%gn", a);
28 free(a);
29 a=NULL;
30 #endif
31 #if 0
32 double
* a = (double**)malloc(sizeof(double));
33 pthread_join(tid, (void**)a);
34 printf("s=%gn", (double)a);
35 free(*a);
36 a=NULL;
37 free(a);
38 a=NULL;
39 #endif
40 #if 0
41 double
b=(double*)malloc(sizeof(double));
42 double** a=&b;
43 int error1=pthread_join(tid, (void**)a);
44 if(error1){
45 errno=error1;
46 printf("%mn");
47 }
48 printf("s=%gn", (double)a);
49 free(*a);
50 a=NULL;
51 free(b);
52 b=NULL;
53 #endif
54 #if 1
55 double
* a;
56 int error1=pthread_join(tid, (void**)a);
57 if(error1){
58 errno=error1;
59 printf("%mn");
60 }
61 printf("s=%gn", (double)a);
62 free(*a);
63 *a=NULL;
64 #endif
65 return 0;
66 }

解决方案八:

#include
#include
#include
#include
#define PAI 3.14159
void* area(void* arg){
double r = (double)arg;
double* s=malloc(sizeof(double));
s = PAI * r * r;
return s;
}
int main(void) {
printf("r=");
double r;
scanf("%lf", &r);
pthread_t tid;
int error=pthread_create(&tid, NULL, area, &r);
if(error){
errno=error;
printf("%mn");
}else {
printf("pthread_create successn");
}
#if 0
double
a;
pthread_join(tid, (void**)&a);
printf("s=%gn", a);
free(a);
a=NULL;
#endif
#if 0
double
* a = (double**)malloc(sizeof(double));
pthread_join(tid, (void**)a);
printf("s=%gn", (double)a);
free(*a);
a=NULL;
free(a);
a=NULL;
#endif
#if 0
double
b=(double*)malloc(sizeof(double));
double** a=&b;
int error1=pthread_join(tid, (void**)a);
if(error1){
errno=error1;
printf("%mn");
}
printf("s=%gn", (double)a);
free(*a);
a=NULL;
free(b);
b=NULL;
#endif
#if 1
double
* a;
int error1=pthread_join(tid, (void**)a);
if(error1){
errno=error1;
printf("%mn");
}
printf("s=%gn", (double)a);
free(*a);
*a=NULL;
#endif
return 0;
}

我再试一次 ,看行不行,粘贴板会不会把 ** 粘成 *.

解决方案九:



妹的!!!!!,上面两个代码粘贴时,粘贴板还是会把** 粘贴成* ,请以下面图片里的代码为准,pthread_join1(1行----31行);pthread_join2(31行----61行);pthread_join2(37行----67行)。谢谢。

解决方案十:

。。。。。。。
。。。。。。。。。。
你先试一试,55行double** a ;之后申请个内存空间,然后再调用pthread_join()函数。
因为你声明了double** a 的指针,但并没有让它指向一个有效的内存空间,所以在调用pthread_join()函数是给你返回数据时发生错误。

你先试试吧。

时间: 2024-09-08 11:34:20

多线程段错误-多线程挂掉----哪个大神可以解决下,多谢啦!的相关文章

spring 注入 多线程-spring多线程注入报错,求大神帮忙解决一下。。谢谢!!

问题描述 spring多线程注入报错,求大神帮忙解决一下..谢谢!! Error creating bean with name 'transactionManager': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implement

php开发遇到错误 Creating default object from empty value 求大神帮忙解决下

问题描述 php开发遇到错误 Creating default object from empty value 求大神帮忙解决下 代码: 14 define('IN_ECS', true); 15 $smarty->left_delimiter = '<!--{'; 16 $smarty->right_delimiter = '}-->'; 17 require(dirname(__FILE__) . '/includes/init.php'); 18 if ((DEBUG_MOD

hibernate 根据持久化类 创建数据库表的时候出现的错误如下,请大神帮忙解决?

问题描述 hibernate 根据持久化类 创建数据库表的时候出现的错误如下,请大神帮忙解决? 十二月 09, 2015 1:49:52 下午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata 信息: table not found: DIC_TAB 十二月 09, 2015 1:49:52 下午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata 信息:

javase-Java连接derby报错,求大神帮解决下~

问题描述 Java连接derby报错,求大神帮解决下~ 解决方案 http://tieba.baidu.com/p/1415330675 用SYS登录时不可以进行建表操作,看样子derby的确有权限控制存在.哼哼哼哼--- 另,用户名中不能存在字符,刚才在用户名中包含了一个;号,结果出现了 错误 58009:网络协议异常:在 DSS 链中的下一个 DSS 段与当前段具有相同的标识号.连接已终止.错误 08004:发生连接认证失败.原因:用户无权访问数据库. 这样的错误 解决方案二: http:/

adb-[求救向] 请各位大神帮忙解决下吧,有关于安卓开发运行写好的项目的时候出现问题

问题描述 [求救向] 请各位大神帮忙解决下吧,有关于安卓开发运行写好的项目的时候出现问题 错误输出[2015-05-27 17:20:55 - Bluetooth] ------------------------------[2015-05-27 17:20:55 - Bluetooth] Android Launch![2015-05-27 17:20:55 - Bluetooth] The connection to adb is down and a severe error has o

求帮助-求大神帮忙解决下这个问题

问题描述 求大神帮忙解决下这个问题 Exception in thread "Thread-15" java.lang.NullPointerException at com.amtch.akka.slave.impl.SlaveServiceImpl.process(SlaveServiceImpl.java:51) at com.amtch.akka.communication.protocol.socket.SocketClient.run(SocketClient.java:5

一个空指针异常问题,求大神帮忙解决下,在线等

问题描述 一个空指针异常问题,求大神帮忙解决下,在线等 public void getQaList(String type, String qid, String qid_min, String cat_id, String tag_id, String page, String qid_top, boolean isShowLoadingDialog) { List<RequestParameter> parameter = new ArrayList<RequestParameter

请哪位大神帮忙解决下这个问题-内存泄露

问题描述 请哪位大神帮忙解决下这个问题-内存泄露 请哪位大神帮忙解决下这个问题,实在是找不到哪个地方内存泄露,凡是代码中用到new的,我都delete了... Detected memory leaks! Dumping objects -> {107} normal block at 0x00206188, 21 bytes long. Data: < l| > A8 6C 7C 00 04 00 00 00 04 00 00 00 01 00 00 00 {106} normal b

c#问题看下多谢了啊-急求大神帮忙看下多谢了啊

问题描述 急求大神帮忙看下多谢了啊 C.MOUSEDOWN事件怎么都不触发求大神帮忙看下谢谢啊看看谢谢搞了几天--看下看看谢谢搞了几天--看下 解决方案 你在跟灵魂对话吗,知道怎么问问题吗 解决方案二: 先检查有没有绑定事件 解决方案三: 是不是没连上数据库啊?......答案就在这里:求大神帮忙,急!!---------------------- 解决方案四: 解决方案五: 我想红色里面那个最小的窗体触发鼠标按下事件 但是不触发 帮忙看 也就是c.mouseDown 解决方案六: 你看看ds的