0

while(1) 在事件循环中继续,即使事件被重新设置。下面是我的代码。我已经在问题所在的代码中的注释中提到了实际问题。预先感谢您的任何帮助 :)

 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;
    }
4

2 回答 2

1

实际上,您在 if (sendBuff==NULL) 检查中忘记了 LeaveCriticalSection 调用。这可能会阻止您的 Writer 线程。

于 2013-12-04T22:48:56.537 回答
1

必须使用参数 bManualReset TRUE 创建事件才能使 ResetEvent() 工作。

于 2013-06-01T07:39:37.230 回答