Can't exitThread with waitForMultipleObjects

126 views Asked by At

I have 2 threads, the first is doing:`

while(1){

    if(recv(conn_s, (char*) &i, sizeof(i),0)>0){

        if(i==28){
            SetEvent(event);
            //printf("nell if di 28\n");
            HANDLE t;
            sleep(1);
            //TerminateThread(scegli, 0);
            //if (CloseHandle(scegli)==0) printf("error close\n");
            t=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) avvia_conversazioneCl, conn, SYNCHRONIZE, 0);
            WaitForSingleObject(t, INFINITE);

the second one:

       const HANDLE handles[2]={event, stdinHandle};

       while(1){

            switch(WaitForMultipleObjects(num, handles, FALSE, 0)){
                case 0: 

                        ExitThread(-1);
                        break;

                case 1:
                                    r=ReadConsole(stdinHandle, &interlocutore, nLength, &charsRead, NULL);
                                    if (r==0){
                                        printf("%d\n", GetLastError());
                                    }
                                    //global_value_to_exit=1;
                                    int size=strlen(interlocutore);
                                     interlocutore[size-2]='\0';

                                    int verifica=verifica_interlocutore(interlocutore); 
                                     if (verifica==0){
                                         printf("interlocutore non disponibile, riprova: \n");
                                         continue;
                                    }



                                    int rrr=send(socket, interlocutore, size, 0);
                                    if (rrr<=0){
                                        printf("send error\n");
                                    }
                                    sleep(1);
                                    if(recv(socket, (char*) &ack, sizeof(int), 0)==SOCKET_ERROR){
                                        printf("errore: %d\n", WSAGetLastError());
                                    }
                                    printf("ho ricevuto %d\n", (int) ack);
                                    if(ack==999){

                                        //sleep(1);
                                         HANDLE t;
                                        DWORD tID;
                                        t=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) avvia_conversazioneCl, conn_s, 0, &tID);
                                        WaitForSingleObject(t, INFINITE);
                                        CloseHandle(t);
                                        //global_value_to_exit=0;
                                        ack=0;
                                        vis=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) visualizza_utenti, conn_s, 0, &visID);
                                     }
                                     //ResetEvent(stdinHandle);
                                     break;

                case WAIT_FAILED: 
                                perror("failed: \n");

                                break;
    }

    memset(interlocutore, 0, sizeof(interlocutore));
}

    return NULL;

}

also:

  const HANDLE handles[2]={event, stdinHandle};

   event=CreateEvent(NULL, FALSE, FALSE, NULL);

   stdinHandle=GetStdHandle(STD_INPUT_HANDLE);`

The point is: if i write something in console i do things with the second thread, if i get 28 with the receive i would like to make the second thread exit so i sett to signaled event. at this point with the waitformultipleobjects i should get into case 0 and exit the thread. instead after i've already recv 28 and changed the status of event, if i write something in console the second thread enters case 1. after writing something else it seems to be dead

any advice?

0

There are 0 answers