linux下使用pthread进行多线程编程,出现内存分配问题,请各位指点
我在linux下使用pthread进行多线程编程,刚刚开始涉及到,所以很简单,但是我遇到了问题,请各位高手给看看,谢谢了。源代码如下:
%A #include<pthread.h>
%A #include<stdio.h>
%A #define THREAD_NUM 50
%A
%A pthread_mutex_t count_lock;
%A pthread_cond_t count_nonzero;
%A unsigned count=0;
%A
%A void * decrement_count(void * arg)
%A {
%A //printf("decrement\n");
%A pthread_mutex_lock(&count_lock);
%A sleep(10);
%A while(count==0)
%A pthread_cond_wait(&count_nonzero,&count_lock);
%A count=count-1;
%A printf("This is inside the decrement function: %d\n",count);
%A pthread_mutex_unlock(&count_lock);
%A return NULL;
%A }
%A
%A void * increment_count(void * arg)
%A {
%A sleep(10);
%A //printf("increment\n");
%A pthread_mutex_lock(&count_lock);
%A if(count==0)
%A pthread_cond_signal(&count_nonzero);
%A count=count+1;
%A printf("This is inside the increment function: %d\n",count);
%A pthread_mutex_unlock(&count_lock);
%A return NULL;
%A }
%A
%A int main(void)
%A {
%A pthread_t thread[THREAD_NUM];
%A //void * exit_status;
%A
%A pthread_mutex_init(&count_lock,NULL);
%A pthread_cond_init(&count_nonzero,NULL);
%A int i=0;
%A for(i=0;i<THREAD_NUM;i++)
%A {
%A if(i%2==0)
%A if(pthread_create(&thread[i],NULL,decrement_count,NULL)!=0)
%A perror("decrement ");
%A else
%A if(pthread_create(&thread[i],NULL,increment_count,NULL)!=0)
%A perror("increment");
%A }
%A for(i=0;i<THREAD_NUM;i++)
%A pthread_join(thread[i],NULL);//&exit_status);
%A pthread_cond_destroy(&count_nonzero);
%A return 0;
%A }
%A 使用gcc test.c -o test -lpthread进行编译以后,运行的时间发现结果如下:
%A This is inside the increment function: 1
%A This is inside the increment function: 2
%A This is inside the increment function: 3
%A This is inside the increment function: 4
%A This is inside the increment function: 5
%A This is inside the increment function: 6
%A This is inside the increment function: 7
%A This is inside the increment function: 8
%A This is inside the increment function: 9
%A This is inside the increment function: 10
%A This is inside the increment function: 11
%A This is inside the increment function: 12
%A This is inside the increment function: 13
%A This is inside the increment function: 14
%A This is inside the increment function: 15
%A This is inside the increment function: 16
%A This is inside the increment function: 17
%A This is inside the increment function: 18
%A This is inside the increment function: 19
%A This is inside the increment function: 20
%A This is inside the increment function: 21
%A This is inside the increment function: 22
%A This is inside the increment function: 23
%A This is inside the increment function: 24
%A This is inside the increment function: 25
%A 段错误
%A 然后就退出来了(注意:上面的“段错误”也是程序的输出)。
%A 我如果将程序开始的THREAD_NUM 赋值为500,就会在运行时间输入如下信息:
%A decrement : Cannot allocate memory
%A decrement : Cannot allocate memory
%A decrement : Cannot allocate memory
%A decrement : Cannot allocate memory
%A decrement : Cannot allocate memory
%A decrement : Cannot allocate memory
%A decrement : Cannot allocate memory
%A decrement : Cannot allocate memory
%A 此后一直不停的输出上述信息。但是我没有检查出是什么地方的问题。还请高手指点,谢谢。
%A%A
%A
专栏文章内容及配图由作者撰写发布,仅供工程师学习之用,如有侵权或者其他违规问题,请联系本站处理。 联系我们
相关推荐
NXP发布Q1财报,多项业务取得新进展
MP3硬件电路原理图03
MP3硬件电路原理图07
MP3硬件电路原理图05
三菱FX系列PLC教程 33 —— FX系列的传送指令
EventOS Nano拯救嵌入式开发:事件驱动+1.5KB ROM,重新定义轻量化
VHDL设计中电路简化问题的探讨
辟新路,中国半导体设备及零部件出海
VXI总线的接口芯片及其应用
加快vxworks在s3c4510b上启动速度的一个方法
机器人实验室网址
西门子与台积电合作推动半导体设计与集成创新
USB软件狗的设计及反破解技术
Qorvo公布2025财年Q4财报:营收超预期
MP3硬件电路原理图06
MP3硬件电路原理图04
上海移动怎么了?
USB接口的数字摄像系统设计
纳米工程阳极可以提高锂离子电池的容量和使用寿命吗?
三菱FX系列PLC教程 36 —— FX系列的取反指令
轴向磁通电机在柴油混合动力汽车中受到关注
vb环境下实现pci设备底层访问的两种方法
台积电拟限制出口最先进工艺技术
Dostal的设计:以安静的方式实现降压-升压电压转换
小尺寸显示器件市场升温
代码整洁如诗!Keil 插件上线,一键格式化代码,告别风格混乱!
三菱FX系列PLC教程 32 —— FX系列的程序流向控制指令
三菱FX系列PLC教程 34 —— FX系列的块传送指令及多点传送指令
三菱FX系列PLC教程 35 —— FX系列的区间比较指令
情感机器人缓解人类焦虑的情绪