C++多线程信号量

fly2sky 2012-11-06 02:23:56
正在学习linux多线程,写了个小小程序,实现的是两个线程往缓冲区里写数据,两个从缓冲区里读数据,效果是达到了,想写写小博记录巩固一下,但是写之前心里看程序总是不踏实,觉得领会错了写了小博反而误人子弟,望高手给指出一下是不是有问题,或者隐含的bug,或者如何改进,或者提些建议,谢谢。

#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#define SIZE 4
struct product
{
sem_t sem_empty;\\定义两个信号量,防止下标访问越界,和线程同步问题
sem_t sem_full;
pthread_mutex_t mutexlock;
int pos;
int buf[SIZE];
};

void init(struct product *t)
{
pthread_mutex_init(&t->mutexlock,NULL);
sem_init(&t->sem_full,0,SIZE);
sem_init(&t->sem_empty,0,0);
t->pos =-1;
}

void put(struct product *t,int data)
{
sem_wait(&t->sem_full);
pthread_mutex_lock(&t->mutexlock);
t->pos = t->pos +1;
t->buf[t->pos] = data;
printf("Thread %d put a data:%d to pos%d\n",pthread_self(),data,t->pos);
sem_post(&t->sem_empty);
pthread_mutex_unlock(&t->mutexlock);
}

void get(struct product *t)
{
sem_wait(&t->sem_empty);
pthread_mutex_lock(&t->mutexlock);
printf("Thread %d get a data:%d from pos%d\n",pthread_self(),t->buf[t->pos],t->pos);
t->buf[t->pos] = -1;
t->pos = t->pos -1;
sem_post(&t->sem_full);
pthread_mutex_unlock(&t->mutexlock);
}

struct product pdt;
void *putter()
{
int n;
for(n=0;n<8;n++)
put(&pdt,n);
}
void *getter()
{
int n;
for(n=0;n<8;n++)
get(&pdt);
}


int main(void)
{
pthread_t pt1,pt2,gt1,gt2;
void *retval;
init(&pdt);
pthread_create(&pt1,NULL,putter,0);
pthread_create(&pt2,NULL,putter,0);
pthread_create(>1,NULL,getter,0);
pthread_create(>2,NULL,getter,0);

pthread_join(pt1,&retval);
pthread_join(pt2,&retval);
pthread_join(gt1,&retval);
pthread_join(gt2,&retval);
return 0;
}
...全文
363 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fly2sky 2012-11-06
  • 打赏
  • 举报
回复
gdb我也是刚学,刚刚在用gdb运行了一下,貌似有下标越界了,SIZE为4啊,为何出来4,5,6,7了啊 (gdb) r Starting program: /home/test/program/mt3 [Thread debugging using libthread_db enabled] [New Thread -1208256832 (LWP 21728)] [New Thread -1208259664 (LWP 21729)] Thread -1208259664 put a data:0 to pos0 [New Thread -1218749520 (LWP 21730)] Thread -1208259664 put a data:1 to pos1 Thread -1208259664 put a data:2 to pos2 Thread -1208259664 put a data:3 to pos3 [New Thread -1229243472 (LWP 21731)] Thread -1218749520 put a data:0 to pos4 Thread -1208259664 put a data:4 to pos5 Thread -1229243472 get a data:4 from pos5 Thread -1218749520 put a data:1 to pos5 Thread -1229243472 get a data:1 from pos5 Thread -1208259664 put a data:5 to pos5 Thread -1229243472 get a data:5 from pos5 Thread -1218749520 put a data:2 to pos5 Thread -1229243472 get a data:2 from pos5 Thread -1208259664 put a data:6 to pos5 Thread -1229243472 get a data:6 from pos5 Thread -1218749520 put a data:3 to pos5 Thread -1229243472 get a data:3 from pos5 Thread -1208259664 put a data:7 to pos5 Thread -1229243472 get a data:7 from pos5 Thread -1218749520 put a data:4 to pos5 Thread -1229243472 get a data:4 from pos5 Thread -1218749520 put a data:5 to pos5 [Thread -1229243472 (LWP 21731) exited] [Thread -1208259664 (LWP 21729) exited] Thread -1218749520 put a data:6 to pos6 [New Thread -1239733328 (LWP 21732)] Thread -1218749520 put a data:7 to pos7 [Thread -1218749520 (LWP 21730) exited] Thread -1239733328 get a data:7 from pos7 Thread -1239733328 get a data:6 from pos6 Thread -1239733328 get a data:5 from pos5 Thread -1239733328 get a data:0 from pos4 Thread -1239733328 get a data:3 from pos3 Thread -1239733328 get a data:2 from pos2 Thread -1239733328 get a data:1 from pos1 Thread -1239733328 get a data:0 from pos0 [Thread -1239733328 (LWP 21732) exited] Program exited normally.
HTJOY 2012-11-06
  • 打赏
  • 举报
回复
没啥问题,挺好的

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧