多缓冲区--生产者-消费者问题

//m个生产者-n个消费者-k个缓冲区
Buffer B[k];
Semaphore empty = k;
Semaphore full = 0;
Semaphore mutex = 1;  //生产者消费者互斥访问缓冲区

in = 0;
out = 0;

process producer()_i{
	while (true){
		produce();
		P(empty);
		P(mutex);
		append() to B[in];
		in = (in + 1) % k;
		V(mutex);
		V(full);
	}
}

process consumer()_j{
	while (true){
		P(full);
		P(mutex);
		take() from B[out];
		out = (out + 1) % k;
		V(mutex);
		V(empty);
		consume();
	}
}

你可能感兴趣的:(多线程)