我想在Rust中构建一个单生产者多消费者示例,其中生产者注定有不超过10个未完成项目。我在C中使用互斥量和两个condvar建模了一个解决方案。一种condvar是在没有消耗的东西时等待消费者,而另一种condvar是在未消费的物品数大于10时等待生产者。C代码如下。
据我从Rust文档了解的那样,std::sync::Mutex
和std::sync::Condvar
之间必须存在1-1连接,因此我无法准确翻译我的C解决方案。
是否还有其他方法可以使用std::sync::Mutex
和std::sync::Condvar
在Rust中达到相同的目的(我看不到)。
#define _GNU_SOURCE
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
//
// This is a simple example of using a mutex and 2 condition variables to
// sync a single writer and multiple readers interacting with a bounded (fixed max size) queue
//
// in this toy example a queue is simulated by an int counter n_resource
//
int n_resource;
pthread_cond_t rdr_cvar;
pthread_cond_t wrtr_cvar;
pthread_mutex_t mutex;
void reader(void* data)
{
long id = (long)data;
for(;;) {
pthread_mutex_lock(&mutex);
while (n_resource <= 0) {
pthread_cond_wait(&rdr_cvar,&mutex);
}
printf("Reader %ld n_resource = %d\n",id,n_resource);
--n_resource;
// if there are still things to read - singla one reader
if(n_resource > 0) {
pthread_cond_signal(&rdr_cvar);
}
// if there is space for the writer to add another signal the writer
if(n_resource < 10) {
pthread_cond_signal(&wrtr_cvar);
}
pthread_mutex_unlock(&mutex);
}
}
void writer(void* data)
{
for(;;) {
pthread_mutex_lock(&mutex);
printf("Writer before while n_resource %d \n",n_resource);
while (n_resource > 10) {
pthread_cond_wait(&wrtr_cvar,&mutex);
}
printf("Writer after while n_resource %d \n",n_resource);
++n_resource;
// if there is something for a reader to read signal one of the readers.
if(n_resource > 0) {
pthread_cond_signal(&rdr_cvar);
}
pthread_mutex_unlock(&mutex);
}
}
int main()
{
pthread_t rdr_thread_1;
pthread_t rdr_thread_2;
pthread_t wrtr_thread;
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&rdr_cvar,NULL);
pthread_cond_init(&wrtr_cvar,NULL);
pthread_create(&rdr_thread_1,NULL,&reader,(void*)1L);
pthread_create(&rdr_thread_2,(void*)2L);
pthread_create(&wrtr_thread,&writer,NULL);
pthread_join(wrtr_thread,NULL);
pthread_join(rdr_thread_1,NULL);
pthread_join(rdr_thread_2,NULL);
}