in bidirs ,the addclient method works well.
my code is very similar to the bidirS's code
but my code can't work well as the example
the function 'addClient' will be stopped by the
IceUtil::Monitor<IceUtil::RecMutex>::Lock lock( *this );
i don't know the reason.
it looks like the addclient and run have a common recmutex.
but why the example can work well ?
these are my code ,
void NetControlI::run()
{
//if i delete the code "IceUtil::Monitor<IceUtil::RecMutex>::Lock lock( *this );"
// function addClient can get the recmutex
IceUtil::Monitor<IceUtil::RecMutex>::Lock lock( *this );
CMessage mesSendToClient;
while( !bDestroy )
{
mesSendToClient = refMQFromParse.getMessage();
if( mesSendToClient.iId == -1 )
{
if( mesSendToClient.sMessage == "quit" )
break;
}
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;
}
}
}
::Ice::Int NetControlI::addClient( const Ice::Identity &a , const Ice::Current &b )
{
//my code can't get the recmutex , if there is a same code in function run.
IceUtil::Monitor<IceUtil::RecMutex>::Lock lock(*this);
SendToClientPrx client = SendToClientPrx::uncheckedCast( b.con->createProxy(a)->ice_oneway()->ice_timeout(-1)->ice_secure(false));
for( int i = 0 ; i < iMaxLink ; ++i )
{
if( !isUsedLinkId[i] )
{
isUsedLinkId[i] = true;
ClientPrx[i] = client;
return i;
}
}
}
//refMQFromParse is a object of CMessageQueue
void CMessageQueue:utMessage( const CMessage &a )
{
IceUtil::Monitor< IceUtil::Mutex >::Lock lock( *this );
if(MessageQueue.empty() )
notify();
MessageQueue.push( a );
}
CMessage CMessageQueue::getMessage()
{
IceUtil::Monitor< IceUtil::Mutex >::Lock lock( *this );
while( MessageQueue.empty() )
wait();
CMessage sTemp = MessageQueue.front();
MessageQueue.pop();
return sTemp;
}

utMessage( const CMessage &a )
Reply With Quote