Producer consumer problem - producers and consumers

Producer Consumer: Consumer Producer

1. What is the producer, the consumer? Almost here and our daily lives. Shampoo factory is the producer, the people is his most loyal consumers; ATM producers, withdrawal of the consumer is his people; school canteens company is the producer, diners is the consumer ... ... sort of model dime a dozen.

2. Such a life model in how to use a computer? Modern computer is basically a multitasking operating system (either single core CPU or multi-core CPU), which is the basic unit of scheduling is the thread (the thread to understand how, lying below the reference name explanation). How to communicate (refer lying Glossary) between different threads? Producers, consumers mechanism for asynchronous communication is notified.

3. Refer to the following models:

void *producer(void *data)

{

   pthread_lock(mt);

   if ( list is full )

   {

      pthread_cond_wait( cond_not_full, mt ); //

   }

   add_queue( list, data );

   pthread_cond_signal( cond_not_empty );

   pthread_unlock(mt);

}

void *consumer(void * data)

{

   pthread_lock(mt);

   if ( list is empty )

   {

      pthread_cond_wait( cond_not_empty, mt ); //

   }

   delete_queue( list, data );

   pthread_cond_signal( cond_not_full );

   pthread_unlock(mt);

}

4. Knowledge series: the production and consumer queue can be combined with List N many things to do, specific usage scenarios, different Idea is not the same, is not listed. But basically the same code model.

 

Westward Glossary:

Thread: It has its own or shared resources, and in most cases run (according to PC address streaking) in an endless loop inside. For example: while (1) {// To do something;}; for (;;) {// To do something;}

Currently I use more is POSIX pthread thread. Of course, Windows, C ++, Android, etc. Their own package is good.

Between inter-thread communication, the communication process: in fact, two people (two threads) holding two phone call, you send a Data I, Data I send you. that is it.

Common mode: pipes, message queues, shared memory, semaphores, Socket like reference Ebook "Unix inter-process communication."

 

Icekirin.yuan - Kirin studio Addict

Q group: 147 565 042

 

Published 14 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/YXFLINUX/article/details/51911842