创建线程时使用临时变量容易出现的问题
来源:互联网 发布:淘宝开店身份验证 编辑:程序博客网 时间:2024/06/13 22:21
今天有点思维短路,看完一段有问题的代码,感觉还很对。代码模型如下:
#include <stdio.h>#include <pthread.h>#include <stdlib.h>#include <unistd.h>typedef struct POINT{ int type ; int x; int y;}POINT;POINT * p;void * p_han(void * arg){ int idx = *(int *)arg ; printf("p_han ------------------ type:%d(x= %d ,y = %d ) \n",p[idx].type,p[idx].x,p[idx].y);}void * p_han1(void * arg){ int idx = *(int *)arg ; printf("p_han1 ------------------ type:%d(x= %d ,y = %d ) \n",p[idx].type,p[idx].x,p[idx].y);}int hande(int idx){ pthread_t pid ; if(p[idx].type == 1 ) { pthread_create( &pid,NULL,p_han,(void *)&idx); } else if( p[idx].type == 0) { pthread_create( &pid,NULL,p_han1,(void *)&idx); }}int main(){ p = (POINT *)malloc(sizeof(POINT) * 10); for(int i = 0 ; i < 10 ;i ++) { p[i].type = i % 2 ; p[i].x = i + 1 ; p[i].y = i + 2 ; } for(int i = 0 ;i < 10 ;i ++) { hande(i); } sleep(10);}
gdb 结果如下:
#0 0x00000000004008dc in p_han (arg=0x7ffff74ad96c) at test1.cxx:1919 printf("p_han ------------------ type:%d(x= %d ,y = %d ) \n",p[idx].type,p[idx].x,p[idx].y);
看完这个,知道原因在哪块了:在函数hande里,循环创建线程时,使用了函数内的自动变量idx作为线程的入参,线程创建完成后,由于线程调度的无序性,该线程可能没有执行,而此时,idx可能已被操作系统回收了,值已发生变化。将上述的程序改为以下的版本进行验证:
#include <stdio.h>#include <pthread.h>#include <stdlib.h>#include <unistd.h>typedef struct POINT{ int type ; int x; int y;}POINT;POINT * p;void * p_han(void * arg){ int idx = *(int *)arg ; printf("p_han--------------------idx = %d ,&idx = %p \n",idx,arg); printf("p_han ------------------ type:%d(x= %d ,y = %d ) \n",p[idx].type,p[idx].x,p[idx].y);}void * p_han1(void * arg){ int idx = *(int *)arg ; printf("p_han1--------------------idx = %d ,&idx = %p \n",idx,arg); printf("p_han1 ------------------ type:%d(x= %d ,y = %d ) \n",p[idx].type,p[idx].x,p[idx].y);}int hande(int idx){ pthread_t pid ; printf("hande --------------------idx = %d ,&idx = %p \n",idx,&idx); if(p[idx].type == 1 ) { pthread_create( &pid,NULL,p_han,(void *)&idx); } else if( p[idx].type == 0) { pthread_create( &pid,NULL,p_han1,(void *)&idx); }}int main(){ p = (POINT *)malloc(sizeof(POINT) * 10); for(int i = 0 ; i < 10 ;i ++) { p[i].type = i % 2 ; p[i].x = i + 1 ; p[i].y = i + 2 ; } for(int i = 0 ;i < 10 ;i ++) { hande(i); } sleep(10);}
hande --------------------idx = 0 ,&idx = 0x7fff7139fffc hande --------------------idx = 1 ,&idx = 0x7fff7139fffc hande --------------------idx = 2 ,&idx = 0x7fff7139fffc hande --------------------idx = 3 ,&idx = 0x7fff7139fffc hande --------------------idx = 4 ,&idx = 0x7fff7139fffc hande --------------------idx = 5 ,&idx = 0x7fff7139fffc hande --------------------idx = 6 ,&idx = 0x7fff7139fffc hande --------------------idx = 7 ,&idx = 0x7fff7139fffc hande --------------------idx = 8 ,&idx = 0x7fff7139fffc hande --------------------idx = 9 ,&idx = 0x7fff7139fffc p_han--------------------idx = 32762 ,&idx = 0x7fff7139fffc Segmentation fault (core dumped)
即在hande 执行完毕之后,p_han 线程才开始执行,此时,idx值已被修改,导致程序coredump 。
解决方案是 为每个线程用malloc/new开辟一段内存块,由线程函数内部去释放。
0 0
- 创建线程时使用临时变量容易出现的问题
- 使用remoteIO获取mic数据时容易出现的问题
- 使用git容易出现的一些问题
- 小谈线程容易出现的问题(菜鸟篇)
- 主函数中的局部变量定义时容易出现的问题
- 临时表和表变量的重复创建问题
- 使用scanf时容易出现的错误
- return临时变量的问题
- 使用注解@Resoure的时候容易出现的问题
- 使用selenium的时候firefox容易出现的问题
- 创建矩阵输出最大值最小值时容易出现的错误
- 主程序访问线程的临时变量
- 关于图形化编程软件sikuli 初学者使用时可能容易出现的一些问题
- Adapter Adapter.notifyDataSetChanged()使用时容易出现的问题 跑马灯效果Demo
- 用户使用时的权限问题 && 网卡图标出现“device not managed” && 设置临时IP
- 关于sql server 在存储过程中创建临时表,并往临时表中插入数据时出现乱码的问题
- 不使用临时变量的情况下,两个变量之间交换值的问题
- 在配置JDK时容易出现的问题
- 各大网站CSS代码初始化集合
- Java类加载器深入讲解
- NYOJ 845 无主之地1
- C语言中谁不认识指针?
- Java线程(4)线程池
- 创建线程时使用临时变量容易出现的问题
- Linux内核配置与编译
- UIImagePickerController详解
- linux 2.4调度系统分析
- hdu 5249 KPI set与其迭代器的运用
- 动态绑定之属性检测
- STATIC关键字
- php面向对象
- BroadcastReceiver