专栏中心

EEPW首页 > 专栏 > linux下使用pthread进行多线程编程,出现内存分配问题,请各位指点

linux下使用pthread进行多线程编程,出现内存分配问题,请各位指点

发布人:yanqin 时间:2009-04-16 来源:工程师 发布文章
我在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财报,多项业务取得新进展

嵌入式系统 2025-04-30

EventOS Nano拯救嵌入式开发:事件驱动+1.5KB ROM,重新定义轻量化

嵌入式系统 2025-04-30

辟新路,中国半导体设备及零部件出海

EDA/PCB 2025-04-30

西门子与台积电合作推动半导体设计与集成创新

Qorvo公布2025财年Q4财报:营收超预期

纳米工程阳极可以提高锂离子电池的容量和使用寿命吗?

轴向磁通电机在柴油混合动力汽车中受到关注

台积电拟限制出口最先进工艺技术

EDA/PCB 2025-04-30

Dostal的设计:以安静的方式实现降压-升压电压转换

代码整洁如诗!Keil 插件上线,一键格式化代码,告别风格混乱!

更多 培训课堂
更多 焦点
更多 视频

技术专区