Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple threads in a DLL

Status
Not open for further replies.

HyperEngineer

Programmer
May 8, 2002
190
US
I have a DLL that has one class that instantiates two other classes that use BeginThread(). It seems that the two new threads just stop processing. Here is some of the code.


The calling class:

This instantiates the classes:
Code:
    msg = (char*) malloc(128);
    sprintf(msg, "Socket = %d\n", m_hSocket);
    WriteToLogFile(msg);

    // Start the Rx & Tx threads[b]
    CSocketRx m_SocketRx((void*) this);
    CSocketTx m_SocketTx((void*) this);[/b]
	
    sprintf(msg, "Rx and Tx sockets created\n");
    WriteToLogFile(msg);
    free(msg);
This sets the m_bStartSession flag monitored in the first class:
Code:
    msg = (char*) malloc(128);
    sprintf(msg, "Set start session flag = %d\n", g_TelnetPipe->m_bStartSession);
    WriteToLogFile(msg);
[b]
    g_TelnetPipe->m_bStartSession = true;
[/b]
    sprintf(msg, "Wait for log in\nSession flag = %d\n", g_TelnetPipe->m_bStartSession);
    WriteToLogFile(msg);
First called class:

This is in the constructor:
Code:
    sprintf(msg, "Start receive thread\n");
    WriteToLogFileRx(msg, hLogFileRx);[b]
    m_hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) RdTh,(LPVOID)this,0,&dwRet);[/b]
    Sleep(100);

    sprintf(msg, "Rx Thread = %d\n", m_hThread);
    WriteToLogFileRx(msg, hLogFileRx);


    sprintf(msg, "Start receive thread\n");
    WriteToLogFileRx(msg, hLogFileRx);
    m_hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) RdTh,(LPVOID)this,0,&dwRet);
    Sleep(100);

    sprintf(msg, "Rx Thread = %d\n", m_hThread);
    WriteToLogFileRx(msg, hLogFileRx);
This is in the new thread RdTh():
Code:
    sprintf(msg, "Waiting for start session flag for %s\n", g_TelnetPipe->m_cIP);
    WriteToLogFileRx(msg, pSocketRx->m_hLogFileRx);

    // Can't go anywhere until the main thread is ready.[b][COLOR=red]
    while(!(g_TelnetPipe->m_bStartSession))      // This loop executes once then.....
    {
      iRet = IdleFunctionRx();
      Sleep(200);
      WriteToLogFileRx("Rx waiting", pSocketRx->m_hLogFileRx);
    }[/color][/b]
    //  Program never gets this far
    sprintf(msg, "Start session\n");
    WriteToLogFileRx(msg, pSocketRx->m_hLogFileRx);
Second called class:

This is in the constructor:
Code:
    sprintf(msg, "Start transmit thread\n");
    WriteToLogFileTx(msg, hLogFileTx);[b]
    m_hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) SendTh,(LPVOID)this,0,&dwRet);[/b]
    Sleep(100);

    sprintf(msg, "Tx Thead - %ld\n", m_hThread);
    WriteToLogFileTx(msg, hLogFileTx);
This is in the new thread SendTh();
Code:
    sprintf(msg, "Waiting for send request\n");
    WriteToLogFileTx(msg, pSocketTx->m_hLogFileTx);[b][COLOR=red]
    while(!g_TelnetPipe->m_bSendRequest)  // executes once..
    {
      nRet = IdleFunctionTx();
      Sleep(100);
      WriteToLogFileTx("Tx waiting", pSocketTx->m_hLogFileTx);
    }[/color][/b]
    // Program does not get this far.
    g_TelnetPipe->m_bSendRequest = false;
    sprintf(msg, "Send command = %s\n", g_TelnetPipe->m_cSendCommand);
    WriteToLogFileTx(msg, pSocketTx->m_hLogFileTx);
The two while loops in RED execute once. Where do they go after that? They don't continue with the routine and they don't execute the loop any more.

Thanks for the help.

HyperEngineer
If it ain't broke, it probably needs improvement.
 
Hi HyperEngineer,

I'm not an expert on thread synchronization, but an interested amateur. Instead of using the global member variable
Code:
g_TelnetPipe->m_bStartSession = true;
and the loop
Code:
// Can't go anywhere until the main thread is ready.
    while(!(g_TelnetPipe->m_bStartSession))      // This loop executes once then.....
    {
      iRet = IdleFunctionRx();
      Sleep(200);
      WriteToLogFileRx("Rx waiting", pSocketRx->m_hLogFileRx);
    }
to inhibit thread execution until the caller is ready, could you create the threads with the CREATE_SUSPENDED flag, then use
Code:
ResumeThread(m_hThread)
to allow the threads to continue execution? Also, could having two threads manipulating the global without enclosing the assignment statements within a Critical Section or something potentially cause erratic behavior?

Good Luck,

Greg Hansen

 
Greg,

Thanks for the reply. The problem is that the calling function exited. When it did the CSocketRx and CSocketTx went out of scope. So, anything they referenced was now unreachable. I put a loop in there that monitored their status and exited the function when both threads closed out.

With the CREATE_SUSPENDED and ResumeThread() I would have the same problem. The thread would not know if the main thread was ready. I could put in a time delay that would guarantee that the main thread was ready, but the monitoring of the global flag works. Now they want something else. If I can't find something on it, I will have to ask another question.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top