Re: Should I use mutex in this context?
Doug Harrison [MVP] wrote:
On Thu, 30 Oct 2008 14:03:21 -0400, Tommy <tommy767@gmail.com> wrote:
The OP asked very clearly, does he need a traditional synchronization
kernel object (event, he pointed out) for a simple 1 read thread, 1
write thread concept?
And the answer for this SIMPLE CASE, you don't.
And the simple answer is wrong. :)
Well, you will never be able to make this fail, and right or wrong,
you would have to admit it is more common place than not.
------ CUT HERE: -------------
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <process.h>
bool Abort = false;
void thread(void *p)
{
while (!Abort) {
printf(".");
Sleep(1000);
}
Sleep(5000);
printf("Done\n");
_endthread();
}
void main(char argc, char *argv[])
{
HANDLE h = (HANDLE)_beginthread(thread, 0, NULL );
printf("-MAIN: Thread Started h: %d\n",h);
printf("-MAIN: Press Escape To Exit\n");
while (1) {
if (_kbhit() && _getch() == 27) break;
Sleep(30);
}
Abort = true;
printf("-MAIN: Waiting for Thread Completion\n");
if (WaitForSingleObject(h,INFINITE) != WAIT_OBJECT_0) {
printf("! Unexpected error\n");
}
printf("-MAIN: Complete\n");
}
------ CUT HERE: -------------
> I gave a comprehensive, concise (as
> possible, given the complexity of the issue) reply to the OP
> in my first message in this thread.
Which is all good and undisputed. But IMV, goes beyond the overall
scope of the OP question.
I don't get the emphasis on "1 reader, 1 writer" thread.
There is no contention issue to deal with (or at least at levels the
OP should be concerned with).
--