Into

Modules

Documentation

classPiiWaitCondition

#include <PiiWaitCondition.h>

Provides waiting/waking conditions between two threads.

Description

The difference between this class and QWaitCondition is in that this class ensures that the wake signals are not missed. If no thread is waiting, the wakeOne() call sets a flag that causes a subsequent wait() call to terminate immediately.

The PiiWaitCondition class works in two modes. By default, wakeOne() calls are queued so that the same number of subsequent wait() calls immediately terminate. In the other mode, only the first wait() call after one or more wakeOne() calls with no threads waiting will terminate immediately.

The following example is ripped off and modified from QT docs. In this example, thread 1 may be in doSomething() while thread 2 signals more characters read. The signals build up a queue which causes the same number of subsequent wait() calls in thread 1 to return immediately.

 PiiWaitCondition keyPressed;

 //thread 1
 for (;;)
   {
     keyPressed.wait(); // This is a PiiWaitCondition global variable
     // Key was pressed, do something interesting
     doSomething();
   }
  

 //thread 2
 for (;;)
   {
     getchar();
     // Causes any thread in keyPressed.wait() to return from
     // that function and continue processing
     keyPressed.wakeOne();
   }

Public types

enum
{ NoQueue, Queue }

Signalling modes.

Constructors and destructor

( )

Construct a new wait condition object.

Public member functions

unsigned int

Get the number of wakeOne() signals currently in queue.

Get the queuing mode.

bool
(
  • unsigned long time = ULONG_MAX
)

Wait for a wakeOne() call from another thread.

unsigned int

Get the number of threads currently waiting on the condition.

void

Wake all threads currently waiting on this condition.

void

Wake one of the threads currently waiting on this condition.

Enumeration details

  • enum QueueMode

    Signalling modes.

    • NoQueue - the first wait() call after many wakeOne() calls terminates immediately.

    • Queue - wakeOne() signals are queued when no thread is waiting so that the same number of subsequent wait() calls immediately terminate

Function details

  • PiiWaitCondition

    ( )

    Construct a new wait condition object.

    Parameters
    mode

    the signalling mode

  • unsigned int queueLength

    ()
    [inline]

    Get the number of wakeOne() signals currently in queue.

  • QueueMode queueMode

    ()
    [inline]

    Get the queuing mode.

  • bool wait

    (
    • unsigned long time = ULONG_MAX
    )

    Wait for a wakeOne() call from another thread.

    The thread calling this will block if there is no pending wake event until another thread calls wake or time milliseconds has elapsed. If time is ULONG_MAX (the default), then the wait will never timeout (the event must be signalled).

    Parameters
    time

    the time to wait for a wake event

    Returns

    true if wake was called and false if the wait timed out

  • unsigned int waiterCount

    ()
    [inline]

    Get the number of threads currently waiting on the condition.

  • void wakeAll

    ()

    Wake all threads currently waiting on this condition.

    This function ensures that all waiting threads are released, but does not build the signal queue. If there are any pending signals, they will be cleared.

  • void wakeOne

    ()

    Wake one of the threads currently waiting on this condition.

    If there are no waiting threads, a flag will be set that causes the next wait call to return immediately. If multiple wakeOne calls occur when no thread is waiting, the calls will either build up a queue or set a flag that causes the next wait call to terminate immediately.

Notes (0)

Add a note

Not a single note added yet. Be the first, add yours.