Click here to Skip to main content
15,897,704 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: SetEvent and WaitForSiongleObject Pin
Munchies_Matt24-Feb-15 5:13
Munchies_Matt24-Feb-15 5:13 
GeneralRe: SetEvent and WaitForSiongleObject Pin
Munchies_Matt23-Feb-15 22:25
Munchies_Matt23-Feb-15 22:25 
GeneralRe: SetEvent and WaitForSiongleObject Pin
Albert Holguin24-Feb-15 4:00
professionalAlbert Holguin24-Feb-15 4:00 
GeneralRe: SetEvent and WaitForSiongleObject Pin
Munchies_Matt24-Feb-15 4:12
Munchies_Matt24-Feb-15 4:12 
GeneralRe: SetEvent and WaitForSiongleObject Pin
Frankie-C24-Feb-15 5:19
Frankie-C24-Feb-15 5:19 
GeneralRe: SetEvent and WaitForSiongleObject Pin
Munchies_Matt24-Feb-15 6:46
Munchies_Matt24-Feb-15 6:46 
GeneralRe: SetEvent and WaitForSiongleObject Pin
Frankie-C24-Feb-15 21:46
Frankie-C24-Feb-15 21:46 
GeneralRe: SetEvent and WaitForSiongleObject Pin
Munchies_Matt24-Feb-15 22:12
Munchies_Matt24-Feb-15 22:12 
I understood, lets go back to the code:



As you say is the write thread getting stuck in the wait, or in the write.
The thing is, in either case, since both threads are of equal priority, the read thread Write thread:
C++
while(pDlg->Go)
{
    WriteFile(gh, buf, towrite, &written, 0);
    WaitForSingleObject(pDlg->WriteTrigger, INFINITE);
}



Read thread:
C++
while(pDlg->Go)
{
    ReadFile(gh, buf, 1024 - readTot, &read, 0);

            ... process data, snipped

    WriteFile(gh, &ACK, 1, &written, 0);
    SetEvent(pDlg->WriteTrigger);
    }

should get descheduled and the write thread scheduled in an even way, they should both get CPU time equally.

What looks to be happening is that the read thread is having a lot of CPU time (its a multi core CPU) and blocking the handle at an IO level thus stalling the write. What I find really surprising is that the read thread can run for up to 8 seconds.

The solution would be for the read thread to wait on the event immediately after it sets it, and then for the write thread to set it and wait immediately:

Write thread:
C++
while(pDlg->Go)
{
    WriteFile(gh, buf, towrite, &written, 0);
            SetEvent(pDlg->WriteTrigger);
    WaitForSingleObject(pDlg->WriteTrigger, INFINITE);
}



Read thread:
C++
while(pDlg->Go)
{
    ReadFile(gh, buf, 1024 - readTot, &read, 0);

            ... process data, snipped

    WriteFile(gh, &ACK, 1, &written, 0);
    SetEvent(pDlg->WriteTrigger);
            WaitForSingleObject(pDlg->WriteTrigger, INFINITE);

    }




But that is an ugly piece of code to look at! Smile | :)
GeneralRe: SetEvent and WaitForSiongleObject Pin
Frankie-C24-Feb-15 22:49
Frankie-C24-Feb-15 22:49 
GeneralRe: SetEvent and WaitForSiongleObject Pin
Munchies_Matt24-Feb-15 22:57
Munchies_Matt24-Feb-15 22:57 
GeneralRe: SetEvent and WaitForSiongleObject Pin
Frankie-C24-Feb-15 23:02
Frankie-C24-Feb-15 23:02 
GeneralRe: SetEvent and WaitForSiongleObject Pin
Munchies_Matt24-Feb-15 23:06
Munchies_Matt24-Feb-15 23:06 
QuestionDeviceIoControl for drive information Pin
john563223-Feb-15 3:20
john563223-Feb-15 3:20 
QuestionQuicksort slower than mergesort... Pin
gautgaut20-Feb-15 13:22
gautgaut20-Feb-15 13:22 
AnswerRe: Quicksort slower than mergesort... Pin
David Crow20-Feb-15 15:43
David Crow20-Feb-15 15:43 
GeneralRe: Quicksort slower than mergesort... Pin
gautgaut21-Feb-15 0:56
gautgaut21-Feb-15 0:56 
GeneralRe: Quicksort slower than mergesort... Pin
Stefan_Lang23-Feb-15 20:50
Stefan_Lang23-Feb-15 20:50 
GeneralRe: Quicksort slower than mergesort... Pin
gautgaut26-Feb-15 6:46
gautgaut26-Feb-15 6:46 
Questiondecision tree Pin
Member 1146482519-Feb-15 7:21
Member 1146482519-Feb-15 7:21 
AnswerRe: decision tree Pin
jeron119-Feb-15 7:34
jeron119-Feb-15 7:34 
QuestionRe: decision tree Pin
David Crow19-Feb-15 8:52
David Crow19-Feb-15 8:52 
QuestionSimple AES Pin
Member 1065708319-Feb-15 3:46
Member 1065708319-Feb-15 3:46 
AnswerRe: Simple AES Pin
jeron119-Feb-15 4:22
jeron119-Feb-15 4:22 
GeneralRe: Simple AES Pin
Member 1065708319-Feb-15 14:37
Member 1065708319-Feb-15 14:37 
GeneralRe: Simple AES Pin
enhzflep19-Feb-15 17:53
enhzflep19-Feb-15 17:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.