Use VC to realize the producer consumer problem (reproduced)

xiaoxiao2021-03-06  71

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, using a wide range of search engines, I only found very limited information about CSemaphore.

, Producer consumer problem

Producers should continue to put data into shared buffering, consumers should continue to extract data from buffering. Consumers must wait for the producer

After taking the data, you can reproduce the new data (not overwritten data), and the consumer must wait after the producer will be removed (not repeated).

, Method of using semaphore

When the thread uses the specified number of shared resources, first call the Lock method "I can use resources". When there is an idle

When shared resource (at this time the counter value> 0) thread continues to execute and reducing the number of counters telling other threads "I use × × resources". 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."

,achieve

1. Create a dialog-based program. Add the following members:

// Slowly display the result of thread

BOOL M_BSLOW;

// producer thread

CPRoduCERTHREAD * M_PPRODUCERTHREAD;

// Consumer thread

Cconsumerthread * m_pconsumerthread;

// Buffet empty sign

CSemaphore * m_psemaphoreempty;

// Square flag

CSemaphore * m_psemaphoreful1;

Cmutex * m_pmutex; // mutually exclusive quantity

Add two editing boxes with class wizards, related add members

// Used to display the data taken by the consumer

CString m_sbufcsm;

// Show the producer insert buffer data

CSTRING M_SBUF; 2, create user interface threads, producers, and consumer threads.

Cproducerthread :: CProducerthread (void * hparent): m_pparentdlg (hparent)

{

}

Int cproducerthread :: run ()

{

CP_CDLG * PDLG;

PDLG = (CP_CDLG *) m_pparentdlg;

CSILOCK MUTEXLOCK (PDLG-> M_PMUTEX);

For (int i = 0; i

{

PDLG-> 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

{

PDLG-> m_psemaphorefull-> lock ();

PDLG-> M_PMUTEX-> LOCK ();

Sprintf (PBUF, PDLG-> M_SBUF); BSLEP = 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_CONT 10), M_DATA);

PDLG-> M_PMUTEX-> UNLOCK ();

IF (BSLEP)

Sleep (100);

PDLG-> M_PSemaphoreempty -> unlock ();

}

Return cwinthread :: run ();

} 3, start the 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_pproducethread-> 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 (); related source code download: http://www.vckbase.com/code/winsys/mtask/producerconsumer.rar

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

New Post(0)