sul*_*man 2 c multithreading operating-system mutex pthreads
我们已经研究过,如果我们处理多线程问题,那么我们使用一种称为mutex的线程同步方法,它允许锁定关键部分,以便其他线程不会干扰并进入阻塞状态,直到互斥锁解锁关键部分.
但是我在我的程序中做了这个事情,但是这个程序的输出与mutex的概念不匹配.如果我错了,请纠正我.
码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#define MAX 10
pthread_mutex_t the_mutex;
pthread_cond_t condc, condp;
int toConsume=0;
int i;
void* consumer(void *ptr) {
pthread_mutex_lock(&the_mutex);
while(i<MAX)
{
/* protect buffer */
while (toConsume <= 0) /* If there is nothing in the buffer then wait */
{
printf("Waiting Thread id:%lu \n",pthread_self());
pthread_cond_wait(&condc, &the_mutex);
}
pthread_mutex_unlock(&the_mutex); /* release the buffer */
sleep(2);
pthread_mutex_lock(&the_mutex); /* protect buffer */
toConsume--;
i++;
}
pthread_mutex_unlock(&the_mutex); /* release the buffer */
pthread_exit(0);
}
int main(int argc, char **argv) {
pthread_t pro, con[3];
pthread_mutex_init(&the_mutex, NULL);
pthread_cond_init(&condc, NULL); /* Initialize consumer condition variable */
pthread_cond_init(&condp, NULL); /* Initialize producer condition variable */
// Create the threads
for(int i=0 ;i<3;i++)
pthread_create(&con[i], NULL, consumer, NULL);
for(int i=0 ;i<3;i++)
pthread_join(con[i], NULL);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT
$ ./ex
Waiting Thread id:140580582618880
Waiting Thread id:140580574226176
Waiting Thread id:140580565833472
Run Code Online (Sandbox Code Playgroud)
即使互斥锁保持锁定,所有线程也会进入关键部分.
该函数pthread_cond_wait将在线程等待时解锁保持的互斥锁.这允许另一个线程进入临界区.
使用的目的pthread_cond_wait是在真正执行临界区内的工作之前,线程需要等待某些条件变为真.要首先测试条件,需要锁定互斥锁.但是,如果条件为假,则必须等待某个事件才能使条件成立.如果等待锁定,则没有其他线程能够更新状态以使条件成为真,因为更新条件也需要锁定相同的互斥锁.
因此,在等待条件变量时,互斥锁被解锁以允许另一个线程获取锁以执行使条件成立的事情.
例如,考虑一个作业队列.线程将锁定互斥锁以从队列中获取作业.但是,如果队列为空,则必须等待作业出现在队列中.这是必须等待的条件,并且条件变量可用于此目的.当它调用pthread_cond_wait条件变量时,关联的互斥锁将被解锁.
另一个线程希望将作业放入队列中.该线程可以锁定互斥锁,将作业添加到队列,发出条件变量信号,然后解锁互斥锁.
当发出条件变量的信号时,等待的线程被唤醒并pthread_cond_wait返回并再次保持互斥锁上的锁定.它检测到队列非空,并且可以进入从队列中排队作业的关键部分.
| 归档时间: |
|
| 查看次数: |
869 次 |
| 最近记录: |