Realize the producer consumer problem with VC

zhaozj2021-02-16  48

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 m_psemaphoreempty-> lock (); mutexlock.lock (); pdlg-> m_sbuf.format ("% 0.10d", i); Mutexlock.unlock ); Pdlg-> m_psemaphorefull-> unlock ();

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 m_psemaphoreful-> lock (); pdlg-> m_pmutex-> lock (); sprintf (pbuf, pdlg-> m_sbuf); bsleep = pdlg-> m_bslow; pdlg-> m_pmutex-> unlock () PBUF = 10; if (PBUF> M_Data CSM_BUF_COUNT-10) PBUF = m_data; m_data [csm_buf_count] = 0; PDLG-> m_pmutex-> lock (); sprintf (pdlg-> m_sbufcsm.getBuffer (csm_buf_count 10), M_data); PDLG-> m_pmutex-> unlock (); if (bsleep) Sleep (100); pdlg-> m_psemaphoreempty -> unlock ();} return cwinthread :: run ();} // 3 launch thread: // m_psemaphorefull = new csemaphore (1, 1); m_psemaphoreempty = new csemaphore (0,1); m_pmutex = new cmutex; this-> m_bupdateauto = false;

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 ();

转载请注明原文地址:https://www.9cbs.com/read-22506.html

New Post(0)