Chinaunix首页 | 论坛 | 博客
  • 博客访问: 825966
  • 博文数量: 91
  • 博客积分: 2544
  • 博客等级: 少校
  • 技术积分: 1885
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-12 09:08
文章存档

2016年(10)

2014年(2)

2013年(4)

2012年(23)

2011年(23)

2010年(13)

2009年(14)

2007年(2)

分类: LINUX

2010-11-14 15:14:55

多线程编程-同步
5, 线程同步

5.1 互斥锁
   暂略。
5.2 条件变量
(1)记住一些使用条件变量时的规则:
 1), 在测试断言之前获得互斥锁
 2), 因为返回可能是由于某些不相关的事件或无法使断言成真额pthread_cond_signal引起,所以要在从pthread_cont_wait返回之后重新对断言进行测试。
 3), 在修改断言中出现的任一变脸前,要获得互斥量。
 4), 仅仅在较短的时间段中持有互斥锁--通常是在测试断言或者修改共享变量的时候。
 5), 显示的(调用pthread_mutex_unlock) 或隐式的(用pthread_cond_wait)释放互斥锁。
 
(2) 关键函数
pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mutex);
该函数以原子的方式释放mutex指向的互斥锁,并导致调用线程基于cv所指向的条件变量阻塞。
 
使用规范一般是:
pthread_mutex_lock();
   while (condition is false)
      pthread_cond_wait();
pthread_mutex_unlock();
 
例子:以下是一个使用条件变量的例子:
 
--------------------------start------------------------------
/*
 * 一个简单的有限缓冲区的消费者和生产者的实例。
 *
 * 注意若生产者有多个,且生产者的速率比消费者的快,有可能覆盖原来的数据而使的数据丢失。
 *
 */
#include  
#include  
#include  
#include  
#define TCOUNT 20 
 
// management queue
struct manage_queue {
 int qlen;  /* queue total lengthn */
 int count;  /* current item number */
 int ppos;  /* put pos */
 int gpos;  /* get pos */
};

static int gbuf[TCOUNT];
static struct manage_queue mq;
static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; 
static pthread_cond_t item_cond = PTHREAD_COND_INITIALIZER; 
static pthread_attr_t attr;
static pthread_once_t ponce = PTHREAD_ONCE_INIT;
static void reader();
static void writer();
static void thread_init(void);
static int get_rand(void);

int  main(void) 

    pthread_t threads[3]; 
 
 pthread_once(&ponce, thread_init);
    pthread_create(&threads[0],&attr,(void *)reader, NULL); 
 sleep(2);
    pthread_create(&threads[1],&attr,(void *)writer, NULL); 
    //pthread_create(&threads[2],&attr,(void *)writer, NULL); 
 #if 0
    for (i = 0; i < 3; i++) { 
     pthread_join(threads[i], NULL); 
    } 
 #endif
 pause();
    return 0; 

 
void reader(void)

 int data;
 for ( ; ; ) {
  pthread_mutex_lock(&count_mutex);
  while (mq.count <= 0) { 
   pthread_cond_wait(&item_cond, &count_mutex); 
   printf("watch_count(): get signal: Count is %d\n", mq.count); 
  }
  data = gbuf[mq.gpos];
  fprintf(stderr, "read item buf[%d]=%d\n", mq.gpos, data);
  mq.gpos++;
  if (mq.gpos >= mq.qlen) 
   mq.gpos = 0;
  mq.count--;
  fprintf(stderr, "reader count=[%d]\n", mq.count);
  pthread_mutex_unlock(&count_mutex); 
  sleep(1);
 }

 
void writer(void) 

 for ( ; ; ) { 
  pthread_mutex_lock(&count_mutex); 
  // put item
  gbuf[mq.ppos] = get_rand();
  fprintf(stderr, "write put %d in seat %d\n", gbuf[mq.ppos], mq.ppos);
  // circle queue
  mq.ppos++;
  if (mq.ppos == mq.qlen)
   mq.ppos = 0;
  mq.count++;
  fprintf(stderr, "writer count=[%d]\n", mq.count);
  pthread_cond_signal(&item_cond);
  pthread_mutex_unlock(&count_mutex); 
  sleep(2);
 } 
}

static void thread_init(void)
{
 pthread_attr_init(&attr); 
 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 
 mq.qlen = TCOUNT;
 mq.ppos = 0;
 mq.gpos = 0;
 mq.count = 0;
}

static int get_rand(void)
{
 return rand()%100;
}
 
--------------end-------------------------
 
该例子存在一定的缺陷,若生产者的速度大于消费者,就可能使得数据丢失。
若要避免这种情况一般是使用两个条件变量,一个记录空位置数,一个记录填满的元素的个数。
阅读(1533) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-15 19:47:38

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com