- 论坛徽章:
- 0
|
请看下面代码和在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 |
|