In many cases we need multiple threads to assist each other to complete the same task. However, the thread is difficult to control from the outside. Thread synchronization techniques can make threads interact with each other, thereby avoiding external control pairs of time and resources. In the actual work process I encountered a similar problem, you need to operate the shared buffer. There is a read thread that is inserted, which makes me suddenly think of producers and consumers. I found 9CBS from MSDN to use a wide range of search engines, I only found a very popular information about CSemaphore. / / / Producer Consumer Problem // Producer should continue to put data into shared buffering, consumers should continue to extract data from buffering. Consumers must wait for the producer to take the data (not overwriting data), and consumers must wait after the producer will be put (not repeated).
/// Use the method of signal // When the thread uses the specified number of shared resources, first call the amount of the signal amount "I can use resources". When there is an idle share resource (at this time the counter value> 0) thread continues to perform and reducing the number of counter, telling other threads "I use × × resource". Otherwise he hangs yourself until there is enough resources available. When the resource is used, the thread calls the Unlock method tells other threads "I don't have this resource."
/// / Implementation //
// 1 Create a dialog-based program. Add the following members. // bool m_bslow; // Slowly display the result of thread
CPRoducerThread * m_pproducethread; // Producer thread cconsumerthread * m_pconusumerthread; // Consumer thread
CSemaphore * m_psemaphoreempty; // buffer empty flag CSEMAPHORE * m_psemaphorefull; // Crouch full sign
CMutex * m_pmutex; // Mutually exclusive semantime Add Two Edit Box Use the class wizard, the associated add member cstring m_sbufcsm; // is used to display the data cstring m_sbuf taken by the consumer; // Display the producer insert buffer data // 2 Create user interface threads, producers and consumer threads. // cproducethread :: CProduceThread (void * hparent): m_pparentdlg (hparent) {}
INT CPRoducesRead :: Run () {cp_cdlg * PDLG; PDLG = (CP_CDLG *) m_pparentdlg; csinglelock mutexlock (PDLG-> m_pmutex);
For (int i = 0; i
RETURN CWINTHREAD :: Run ();} cconsumerthread :: cconsumerthread (void * pparent): m_pparent (pparent) {
} int cconsumerthread :: run () {cp_cdlg * PDLG; PDLG = (cp_cdlg *) this-> m_pparent; char * PBUF; PBUF = this-> m_data; bool bsleep; for (int i = 0; i
this-> m_pProducerThread = new CProducerThread (this); this-> m_pConsumerThread = new CConsumerThread (this); this-> m_sBuf.Format ( "1234567890"); this-> UpdateData (false); this-> m_pProducerThread-> CreateThread ( CREATE_SUSPENDED); VERIFY (m_pProducerThread-> SetThreadPriority (THREAD_PRIORITY_IDLE)); this-> m_pConsumerThread-> CreateThread (CREATE_SUSPENDED); VERIFY (m_pConsumerThread-> SetThreadPriority (THREAD_PRIORITY_IDLE)); this-> m_pProducerThread-> ResumeThread (); this-> m_pConsumerThread -> ResumeThread ();