Results 1 to 2 of 2

Thread: Timer and Mutex/Monitor Question

  1. #1
    billg is offline Registered User
    Name: Bill Gw
    Organization: WMS
    Project: Games
    Join Date
    Apr 2011
    Posts
    11

    Timer and Mutex/Monitor Question

    I have an IceUtil::TimerTask that runs every 5 milliseconds and protects a critical code region. I have other code (not running in the 5msec timer) that also needs access to the critical code region. I’ve implemented protecting the critical code region using the IceUtil::Mutex and IceUtil::Monitors, see a big difference in timing and was wondering if this is expected. Here’s what I’m seeing:

    // MUTEX --------------------------------------------------------------------------------
    runTimerTask () - every 5 msecs
    {
    IceUtil::Mutex::Lock local_lock( My_Mutex );

    do critical code
    }

    UpdateSomething() –
    {
    IceUtil::Mutex::Lock local_lock( My_Mutex );

    operations = 1000;
    for ( int I = 0; I < 1000; i++ )
    {
    do critical code
    local_lock.release();

    IceUtil::ThreadControl::yield();
    IceUtil::ThreadConrol::sleep( IceUtil::Time::milliseconds( 1 ) );
    }

    }

    When I run this Mutex code above, the function ‘UpdateSomething()’ takes ~2seconds.


    // Monitor (using .wait() )-----------------------------------------------------------------
    runTimerTask () - every 5 msecs
    {
    IceUtil::Monitor<IceUtil::Mutex>::Lock local_lock( My_Monitor );
    My_Monitor.notify()

    do critical code
    }


    UpdateSomething() –
    {
    IceUtil::Monitor<IceUtil::Mutex>::Lock local_lock( My_Monitor );

    operations = 1000;
    for ( int I = 0; I < 1000; i++ )
    {
    do critical code

    My_Monitor.wait();
    }

    }

    When I run this Monitor code above, the function ‘UpdateSomething()’ takes ~10.5seconds.



    // Monitor (using .timedWait() )---------------------------------------------------------
    runTimerTask () - every 5 msecs
    {
    IceUtil::Monitor<IceUtil::Mutex>::Lock local_lock( My_Monitor );
    My_Monitor.notify()

    do critical code
    }


    UpdateSomething() –
    {
    IceUtil::Monitor<IceUtil::Mutex>::Lock local_lock( My_Monitor );

    operations = 1000;
    for ( int I = 0; I < 1000; i++ )
    {
    do critical code

    finished = false;
    while ( finished == false )
    {
    finished = My_Monitor.timedWait(IceUtil::Time::milliseconds( 1 ) );
    }
    }
    }

    When I run this Monitor code above, the function ‘UpdateSomething()’ takes ~12seconds.


    I’m using an oscilloscope to capture timing. I’m looking for the most efficient way to lock the critical code and it looks like the ICE manual says to use Monitors but maybe I’m not using them correctly or missed something in the documentation. Any help is greatly appreciated.

    Thanks.
    -bill

  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
    Hi Bill,

    If you have just two threads with short critical sections, simply using a mutex is the most efficient solution (as your experiments show). Locking a mutex (and later releasing this mutex) is extremely fast where there is no or little contention. BTW a lock is missing in your first pseudo code sample--you want to reacquire the lock before re-entering the loop.

    Using a monitor makes sense if your critical sections take more time, and/or if you have many threads sharing the same data.

    Best regards,
    Bernard
    Bernard Normier
    ZeroC, Inc.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Exceptions can escape from IceUtil::Timer::run
    By andy_84 in forum Bug Reports
    Replies: 1
    Last Post: 11-18-2010, 03:33 AM
  2. Problem with IceUtil::Mutex
    By anhpnt in forum Help Center
    Replies: 3
    Last Post: 12-04-2009, 07:09 AM
  3. Monotonic Timer in ICE for .NET
    By kwaclaw in forum Comments
    Replies: 2
    Last Post: 04-03-2008, 10:26 PM
  4. IceUtil::Mutex for C#
    By amrufon in forum Help Center
    Replies: 1
    Last Post: 09-05-2006, 09:35 AM
  5. IceStorm/ Timer Service ideas
    By stephan in forum Help Center
    Replies: 5
    Last Post: 10-27-2003, 05:53 AM

Posting Permissions

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