新闻  |   论坛  |   博客  |   在线研讨会
linux下使用pthread进行多线程编程,出现内存分配问题,请各位指点
yanqin | 2009-04-16 13:37:39    阅读:4727   发布文章

我在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

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客