Results 1 to 4 of 4

Thread: i puzzle about the example bidirS!

  1. #1
    fw_csha is offline Registered User
    Join Date
    Sep 2004
    Posts
    22

    i puzzle about the example bidirS!

    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;
    }
    Last edited by fw_csha; 05-17-2005 at 11:12 AM.

  2. #2
    bernard's Avatar
    bernard is offline ZeroC Staff
    Name: Bernard Normier
    Organization: ZeroC, Inc.
    Project: Ice
    Join Date
    Feb 2003
    Location
    Palm Beach Gardens, FL
    Posts
    1,294
    IceUtil::Monitor<IceUtil::RecMutex>::Lock lock( *this );

    This line means "acquire a Monitor/RecMutex named *this, and release it in the destructor of lock".

    Obviously, if one thread acquires this mutex and enters an infinite loop ( while( !bDestroy ) ), no other thread will be able to acquire this mutex.

    In the bidir demo (CallbackI.cpp), the answer is the
    timedWait(IceUtil::Time::seconds(2));
    in the loop.
    This releases the *this mutex for 2 seconds (or until notify is called).

    Cheers,
    Bernard

  3. #3
    fw_csha is offline Registered User
    Join Date
    Sep 2004
    Posts
    22
    if i let "run" wait 1 second , enabling "addclient" to get recmutex,
    but "addclient" can't finish in 1 second .
    "run" will wait until " addclient " release the recmutex ?

  4. #4
    matthew's Avatar
    matthew is offline ZeroC Staff
    Name: Matthew Newhook
    Organization: ZeroC, Inc.
    Project: Internet Communications Engine
    Join Date
    Feb 2003
    Location
    NL, Canada
    Posts
    1,458
    Quote Originally Posted by fw_csha
    if i let "run" wait 1 second , enabling "addclient" to get recmutex,
    but "addclient" can't finish in 1 second .
    "run" will wait until " addclient " release the recmutex ?
    Yes, being a mutex it cannot be acquired by two threads at the same time.

    As I previously recommended to you you should read a good book or tutorial on monitors and MT programming. Here is a link to one that is provided by Sun on the Java monitor which is the same concept as the IceUtil Monitor class. http://java.sun.com/docs/books/tutor.../monitors.html.

    Regards, Matthew

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Ice.Storm puzzle me
    By paul in forum Help Center
    Replies: 0
    Last Post: 04-28-2009, 01:57 AM
  2. puzzle about how to install ice on linux
    By snowman in forum Help Center
    Replies: 2
    Last Post: 01-30-2008, 06:04 AM
  3. a puzzle
    By rellik78 in forum Help Center
    Replies: 1
    Last Post: 09-20-2006, 05:54 AM
  4. puzzle in C++ demo
    By ChenQingQing in forum Help Center
    Replies: 1
    Last Post: 05-24-2006, 03:18 AM
  5. Puzzle on smart pointer
    By soloman817 in forum Help Center
    Replies: 1
    Last Post: 07-13-2004, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •