Jump to ID:
Graphics

Project Home

Documents

Discussions

Wiki

Project Info
Forum Topic - Waiting on a thread: Page 1 of 10 (10 Items)
   
 
 
Waiting on a thread  
This might not really have anything to do with the microGUI, so if it is in the wrong forum, just let me know.

I am attempting to wait for a thread from a third party library to finish before my code continues to process.

The third party library (TCP library) basically starts a thread to send a message over a socket to another computer.

Basically, I provide the TCP library a callback function that it invokes when the response is received.

The callback function is always invoked after the wait period that I establish for the pthread_cond_timedwait().  No 
matter how long I make the wait period, the callback function seems to be on hold until after that is over.

Any ideas?

Here is my code so far:

int PatComSendMessageW(char* pszMessage, const int len, const long nTimeOut)
{
    int nWaitError = 0;
    struct timespec timeWait = {0};

    g_pComClient = TCPClientConstructor(Trp_IP,
                                        Trp_Port,
                                        FLAG_READ_TIMEOUT,
                                       PatComRecvMessage);  //PASSING CALLBACK

    //LOCK MUTEX
    pthread_mutex_lock(&g_ReadMutex);

    //SEND MESSAGE
    TCPWriteTimed(g_pComClient, pszMessage, len, nTimeOut);

    //SET AMOUNT OF TIME TO WAIT
    timeWait.tv_sec  = time(0) + 15;
    timeWait.tv_nsec = 0;

    //WAIT FOR MESSAGE
    nWaitError = pthread_cond_timedwait(&g_ReadEvent, &g_ReadMutex, &timeWait);
    pthread_mutex_unlock  (&g_ReadMutex);

    return 0;
}

void PatComRecvMessage(struct Socket* pComClient,
                       int   nComEvent,
                       char* pszMessage,
                       int   nMsgLen,
                       int   nComError)
{
    int nError = 0;
    PtEnter(NULL);
    WdgtAppendText(ABW_LgnTxtWarnMsg, "PatComRecvMessage\n");
    PtBkgdHandlerProcess();
    PtLeave(NULL);

    do
    {
        if(!pszMessage || nMsgLen < 1)
        {
            nError = -1;
            WdgtSetText(ABW_LgnTxtErrorMsg,"TRP: Received Empty Reply");
            break;
        }

        switch(nComEvent)
        {
            case TCP_EVENT_READ_DATA:
                PtEnter(NULL);
                WdgtSetText(ABW_LgnTxtErrorMsg,"TRP: Received Reply");
                PtLeave(NULL);
                break;

            case TCP_EVENT_READ_ERROR:
                PtEnter(NULL);
                WdgtSetText(ABW_LgnTxtErrorMsg,"TRP: Read Error");
                PtLeave(NULL);
                break;

            default:
                PtEnter(NULL);
                WdgtSetText(ABW_LgnTxtErrorMsg,"TRP: Unknown Event on Socket");
                PtLeave(NULL);
                break;
        }

    }while(0);

    pthread_cond_signal(&g_ReadEvent);

    return;
}