For the moment, let's take the following example:
Class cuploadsongroad{
HANDLE Buffer_full;
HANDLE Buffer_empty;
int m_nroadnum;
Critical_section SC, OC;
int *buffer;
int buffer_p;
int buffer_c;
int p;
int C;
Public
Cuploadsongroad () {}
Cuploadsongroad (int n): M_nroadnum (N)
{
Buffer = new Int[m_nroadnum];
memset (buffer, 0, m_nroadnum*sizeof (int));
Buffer_full = CreateSemaphore (null, m_nroadnum, m_nroadnum, NULL);
Buffer_empty = CreateSemaphore (null, 0, m_nroadnum, NULL);
InitializeCriticalSection (&SC);
InitializeCriticalSection (&OC);
Buffer_c = c = 0;
for (int i=0; i<n; i++) {
Buffer[i] = i;
}
buffer_p = p = n;
}
~cuploadsongroad ()
{
delete []buffer;
DeleteCriticalSection (&SC);
DeleteCriticalSection (&OC);
CloseHandle (Buffer_full);
CloseHandle (Buffer_empty);
}
unsigned int produce () {
WaitForSingleObject (Buffer_empty, INFINITE);
EnterCriticalSection (&SC);
Buffer[buffer_p] = p;
printf ("producer produces \t%d\n", buffer[buffer_p]);
Buffer_p = (++p)% M_nroadnum;
LeaveCriticalSection (&SC);
ReleaseSemaphore (Buffer_full, 1, NULL);
return 0;
}
unsigned int consume () {
WaitForSingleObject (Buffer_full, 10*60*1000);
EnterCriticalSection (&SC);
printf ("T consumer consumes \t%d\n", buffer[buffer_c]);
Buffer_c = (++c)% M_nroadnum;
LeaveCriticalSection (&SC);
ReleaseSemaphore (Buffer_empty, 1, NULL);
return 0;
}
};