Archived

This forum has been archived. Please start a new discussion on GitHub.

a thread problem

my code is like the bidir demo,
the difference is that i use message queue .
if the message queue is empty ,the function will be in wait state, the programe will stay in the "run" function.
when i call the destroy function , i can't get the recmutex,so the destroy function will not be finished.
i don't know how to kill the thread "run" which in wait state.


CMessage CMessageQueue::getMessage()
{
IceUtil::Monitor< IceUtil::Mutex >::Lock lock( *this );
//if it is empty , will wait
while( MessageQueue.empty() )
wait();

CMessage sTemp = MessageQueue.front();
MessageQueue.pop();

return sTemp;
}


void NetControlI::run()
{
IceUtil::Monitor<IceUtil::RecMutex>::Lock lock( *this );

CMessage mesSendToClient;
while( !bDestroy )
{
//if the queue is empty , it will stop there
//

mesSendToClient = refMQFromParse.getMessage();
try
{
if( isUsedLinkId[ mesSendToClient.iId ] )
ClientPrx[ mesSendToClient.iId ] -> sendMessage( mesSendToClient.sMessage );
}
catch( const Exception& ex )
{
isUsedLinkId[ mesSendToClient.iId ] = false;
ClientPrx[ mesSendToClient.iId ] = NULL;
cerr << ex << endl;
}
}
}
void NetControlI::destroy()
{
IceUtil::ThreadPtr ptrTempThread;
cout << "destroying server1" << endl;

{
// i can't get the recmutexc IceUtil::Monitor<IceUtil::RecMutex>::Lock lock( *this );

cout << "destroying server2" << endl;
bDestroy = true;

notify();

ptrTempThread = ptrNetControlIThread;
ptrNetControlIThread = 0; // Resolve cyclic dependency.
}

ptrTempThread -> getThreadControl().join();
}

Comments

  • matthew
    matthew NL, Canada
    fw_csha wrote:
    my code is like the bidir demo,
    the difference is that i use message queue .
    if the message queue is empty ,the function will be in wait state, the programe will stay in the "run" function.
    when i call the destroy function , i can't get the recmutex,so the destroy function will not be finished.
    i don't know how to kill the thread "run" which in wait state.
    ...

    You can't get the mutex because you never release it. You are trying to solve a typical work queue problem. For an example of how to do this I would look at demo/IceUtil/workqueue. I would also recommend that you get a good book on MT programming such as "Concurrent Programming in Java" by Doug Lea.

    Best Regards, Matthew
  • thanks very much~
    i will do it!