hi i am trying to develop a program
that can coordinate the access of a resource...
my resource is an array of strings.. my producers will
put strings into the array, my consumers will retrieve
one string at a time then delete that one string from the array
once they got it.
i would like the consumers and producers to take turns
in accessing the string array.
eg.. only one consumer can be
accessing the string array or only one producer can be
adding into the string array at any given time.
anyways, right now i am thinking of this strategy ...(after reading
some pseudocodes and producer/consumer semaphore
solutions online)
Code:
HANDLE hsem1; // gate to restrict access to string array.
HANDLE hsem2; // signal when string array full
HANDLE hsem3; // signal when string array empty
DWORD dwProdWait;
DWORD dwConsWait;
TCHAR sem1Name[] = TEXT("String array resource");
// create the gate for restricting access to the string array
// initial count 0 to allow time for setting up string array
hsem1 = CreateSemaphore(NULL, 0, 1, sem1Name);
// increment semaphore so that string array can now be accessed
ReleaseSemaphore(hSemaphore, 1, NULL);
// producer try to enter the gate
dwProdWait = WaitForSingleObject(hsem1, 0L);
switch (dwProdWait)
{
case WAIT_OBJECT_0: // Semaphore object signaled
break;
case WAIT_TIMEOUT: // Semaphore object non-signaled.
break;
}
i am dumbfounded right now.. how i can make
the producer wait on the boxempty to be nonsignaled
and how i can make the consumer wait
for boxfull to be nonsignaled..
is this even a valid strategy right now ? please help thanks.