使用互斥量实现生产者/消费者 [英] Implementing producers/consumers using mutex

查看:108
本文介绍了使用互斥量实现生产者/消费者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#define WORK_SIZE 1024
pthread_mutex_t work_mutex;
char work_area[WORK_SIZE];
void *thread_start(void *);
int main() {
pthread_t a_thread;
pthread_mutex_init(&work_mutex,NULL);
pthread_create(&a_thread,NULL,thread_start,NULL);
while(1)
{
pthread_mutex_lock(&work_mutex);
printf("Enter some text\n");
fgets(work_area, WORK_SIZE, stdin);
pthread_mutex_unlock(&work_mutex);
}
return 0;
}

void *thread_start(void *arg)
{
sleep(1);
while(1)
{
pthread_mutex_lock(&work_mutex);
printf("You enetered %d char",strlen(work_area));
pthread_mutex_unlock(&work_mutex);
}
}

当我执行程序时,在释放主线程中的互斥锁之后,每次在第二个线程可以获取该锁之前,它都会再次获取该锁.我期望一旦主线程释放锁,已经被阻塞的第二个线程将获取该锁并在主线程之前开始执行.

When I execute the program, after releasing of the mutex lock in main thread, it again aquires the lock, everytime, before the second thread could aquire the lock. I was expecting that once the main thread would release the lock, the second thread which is already blocked would aquire the lock and start execution before the main.

更清楚地说,我得到的是这样的输出:-

To be more clear, I am getting such type of output :-

Enter some text
qwerty
Enter some text
asdaf
Enter some text
jkdf 
Enter some text

推荐答案

不考虑使用信号量的建议,我认为您正在观察的行为的原因如下.

Putting asside the suggestion to use a semaphore, I think that the reason for the behaviour that you are observing is as follows.

  1. 直到thread_start中的pthread_mutex_lock被调用,main中的循环才不会被阻塞.
  2. 因此,thread_start的循环唯一有机会调用pthread_mutex_lock的时间是执行main的线程的时间片到期时
  3. 在释放锁定时发生时间片到期的可能性很小.这是因为在等待ENTER键时,主线程从阻塞状态唤醒时可能会有一个新的时间片.
  1. Until the pthread_mutex_lock in thread_start is called, the loop in main won't be blocked.
  2. Therefore, the only time that thread_start's loop will get a chance to call pthread_mutex_lock is when the time slice of the thread executing main expires
  3. The chances of that time slice expiry occuring while the lock is released is miniscule. This is because the main thread will probably have a fresh time slice when it wakes from the blocked state while it waited for the ENTER key.

请注意,此说明假设一个内核.但是即使在多核系统上,也仅在另一个线程的时间片用完时才对thread_start线程进行调度.在main线程未持有锁的情况下发生这种情况的可能性很小.

Note, this explanation assumes a single core. But even on a multicore system the thread_start thread is only going to be scheduled when another thread's time slice runs out. The chances of that happening while main's thread isn't holding the lock is small.

我上述假设的一种可能的检验方法是调用 pthread_yield .您可能要在两个线程中都这样做.即使那样,我也不认为每次都会保证线程切换.

One possible test for my above hypothesis would be to call pthread_yield after releasing the lock. You'll probably want to do that in both threads. Even then I don't think it will GUARANTEE a thread switch every time.

这篇关于使用互斥量实现生产者/消费者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆