Question

while(1) continues in event loop even if event is restet. below is my code. I have mentioned the actual question in comments inside the code where problem lies. Thank you in advance for any help :)

 DWORD WINAPI workerThreadProcedure(LPVOID lparam)
    {
       printf("===Worker Thread===\n");     
            // Initialize the critical section one time only.
        if (!InitializeCriticalSectionAndSpinCount(&cs, 
            0x00000400) ) 
            return  0;
       int num =  LOWORD (lparam);
       struct node* cur;
       struct node* disp = NULL;
           EnterCriticalSection(&cs);
           printf("Worker Thread in critical section\n");cur =cread();
            cur->rollno = num;
                cur->n=NULL;
            push(cur);
            SetEvent(data_available_event); // signal sender thread that data is available
            LeaveCriticalSection(&cs);  
        return 0;
    }
    DWORD WINAPI senderThreadProcedure(LPVOID lparam)
    {
        printf("===Sender Thread===\n");        
           while(1)
        {
            printf("Inside While\n");   
            WaitForSingleObject(data_available_event,0);
            EnterCriticalSection(&cs);
            printf("Sender Thread in critical section\n");  
            sendBuff = pop();
            if(sendBuff == NULL)
               {
                   ResetEvent(data_available_event); 
                   printf("Data not available anymore");
                   continue;
                              /*Here what I want is if sendBuff is NULL, 
    it resets the event and again go back to while(1) and again waits for 
    data_available_event. But what actually happens it when sendBuff becomes NUll, it 
    prints "Data not available anymore", goes to while(1) and even there is no 
    data_avaialble_event, it again enters critical section, again prints "Data not 
    available anymore" again go to while and again enters critical section and this cycle 
    runs infinietly :(*/

               }
            itoa(sendBuff->rollno,sendBuffer,10);
            printf("%s\n",sendBuffer);
            //printf("%d\n",disp->rollno);
            LeaveCriticalSection(&cs);
            printf("Sender Thread LEFT critical section\n");

            send(AH_glb_socketIdentifier,sendBuffer,strlen(sendBuffer),0);
            Sleep(1);
        }
        return 0;
    }
Was it helpful?

Solution

The event must be created with the parameter bManualReset TRUE to make the ResetEvent() working.

OTHER TIPS

Actually, you are forgetting a LeaveCriticalSection call in the if (sendBuff==NULL) check. That is probably blocking your Writer thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top