免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2386 | 回复: 0
打印 上一主题 下一主题

pthread_mutex_lock() 和 pthread_cond_wait() [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-22 10:01 |只看该作者 |倒序浏览

               
请看下面代码和在FC9运行结果:
#include
#include
#include
struct msg {
struct msg *next;
int num;
};
struct msg *head;
pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *consumer(void *p)
{
struct msg *mp;
for (;;)
    {
        pthread_mutex_lock(&lock);
        printf("Consume pthread_mutex_lock\n");
            while (head == NULL)
        pthread_cond_wait(&has_product, &lock);
            printf("Consume pthread_cond_wait\n");
        mp = head;
        head = mp->next;
        pthread_mutex_unlock(&lock);
             printf("Consume pthread_mutex_unlock\n");
        printf("Consume %d\n", mp->num);
        free(mp);
        sleep(rand() % 5);
    }
}
void *producer(void *p)
{
struct msg *mp;
for (;;) {
    mp = malloc(sizeof(struct msg));
    mp->num = rand() % 1000 + 1;
    printf("Produce %d\n", mp->num);
    pthread_mutex_lock(&lock);
    printf("Produce pthread_mutex_lock\n");
    mp->next = head;
    head = mp;
   
    pthread_mutex_unlock(&lock);
        printf("Produce pthread_mutex_unlock\n");
    pthread_cond_signal(&has_product);
    printf("Produce pthread_cond_signal\n");
    sleep(rand() % 5);
}
}
int main(int argc, char *argv[])
{
pthread_t pid, cid;  
srand(time(NULL));
pthread_create(&pid, NULL, producer, NULL);
pthread_create(&cid, NULL, consumer, NULL);
pthread_join(pid, NULL);
pthread_join(cid, NULL);
return 0;
}
编译: gcc -o cond -lpthread  cond.c
运行: [root@dep4 test]# ./cond
Produce 198
Produce pthread_mutex_lock
Produce pthread_mutex_unlock
Produce pthread_cond_signal
Produce 753
Produce pthread_mutex_lock
Produce pthread_mutex_unlock
Produce pthread_cond_signal
Consume pthread_mutex_lock
Consume pthread_cond_wait
Consume pthread_mutex_unlock
Consume 753
Consume pthread_mutex_lock
Consume pthread_cond_wait
Consume pthread_mutex_unlock
Consume 198
Produce 69
Produce pthread_mutex_lock
Produce pthread_mutex_unlock
Produce pthread_cond_signal
Consume pthread_mutex_lock
Consume pthread_cond_wait
Consume pthread_mutex_unlock
Consume 69
Produce 934
Produce pthread_mutex_lock
Produce pthread_mutex_unlock
Produce pthread_cond_signal
Consume pthread_mutex_lock
Consume pthread_cond_wait
Consume pthread_mutex_unlock
Consume 934
Produce 598
Produce pthread_mutex_lock
Produce pthread_mutex_unlock
Produce pthread_cond_signal
Consume pthread_mutex_lock
Consume pthread_cond_wait
Consume pthread_mutex_unlock
Consume 598
Consume pthread_mutex_lock   //consume线程获取互斥量lockProduce 601Produce pthread_mutex_lock   //为何重复能获得互斥量?
Produce pthread_mutex_unlock
Produce pthread_cond_signal
Consume pthread_cond_wait
Consume pthread_mutex_unlock
Consume 601
Consume pthread_mutex_lock
^XProduce 159
Produce pthread_mutex_lock
Produce pthread_mutex_unlock
Produce pthread_cond_signal
Consume pthread_cond_wait
Consume pthread_mutex_unlock
Consume 159
Consume pthread_mutex_lock
问题: 上述红色部分:为何重复能获得互斥量?
解答: 下红色部分
[root@dep4 test]# man pthread_cond_wait  
PTHREAD_COND_TIMEDWAIT(P)  POSIX Programmer¡¯s Manual PTHREAD_COND_TIMEDWAIT(P)

NAME
       pthread_cond_timedwait, pthread_cond_wait - wait on a condition

SYNOPSIS
       #include

       int pthread_cond_timedwait(pthread_cond_t *restrict cond,
              pthread_mutex_t *restrict mutex,
              const struct timespec *restrict abstime);
       int pthread_cond_wait(pthread_cond_t *restrict cond,
              pthread_mutex_t *restrict mutex);

DESCRIPTION
       The  pthread_cond_timedwait()  and  pthread_cond_wait() functions shall block on a
       condition variable. They shall be called with mutex locked by the  calling  thread
       or undefined behavior results.

       These  functions atomically release mutex and cause the calling thread to block on       the condition variable cond; atomically here means  "atomically  with  respect  to
       access  by  another thread to the mutex and then the condition variable". That is,
       if another thread is able to acquire the mutex after the about-to-block thread has
       released   it,   then   a   subsequent   call   to   pthread_cond_broadcast()   or
       pthread_cond_signal() in that thread shall behave as if it were issued  after  the
       about-to-block thread has blocked.

       Upon successful return, the mutex shall have been locked and shall be owned by the
       calling thread.

       When using condition variables there  is  always  a  Boolean  predicate  involving
       shared  variables  associated  with each condition wait that is true if the thread

       
        文件:cond.rar
        大小:0KB
        下载:
下载
       
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/64117/showart_2157169.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP