Re: WaitForSingleObject
Larry wrote:
// Declare Procuder && Cosumer CS
CRITICAL_SECTION csProducer;
CRITICAL_SECTION csConsumer;
Okay. Nice. Now, the 1M$ question is: What data structures are those
critical sections supposed to guard? Defining this is crucial, because
whether you use them correctly depends on exactly that.
// Initialize the critical section
InitializeCriticalSection(&csProducer);
// Launch Producer Thread
unsigned int prodRet;
_beginthreadex(0,0,Producer,NULL,0,&prodRet);
if(prodRet)
cout << "Launched Producer Thread!" << endl;
// Release resources used by the critical section object.
DeleteCriticalSection(&csProducer);
Wrapped in init/delete calls.
// Initialize the critical section
InitializeCriticalSection(&csConsumer);
// Spawn a new Consumr Thread each
// time a client connects.
unsigned int sockRet;
_beginthreadex(0,0,Consumer,s,0,&sockRet);
if(sockRet)
cout << "Spawned a new thread!" << endl;
// Release resources used by the critical section object.
DeleteCriticalSection(&csConsumer);
Wrapped in init/delete calls again, this time for the other CS.
Sorry, but both wraps are wrong. You have to initialise the critical
sections on startup and delete them when you don't need them any more (here
before main() returns). In between, you can use them, e.g. you have to lock
one (EnterCriticalSection) whenever you want to access (read or write) the
shared data it guards. Then, when you're done, you unlock it again
(LeaveCriticalSection), so other threads can access the data.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932