Threads[ConditionVariable] - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Multithreaded Programming : ConditionVariable : Threads/ConditionVariable/Wait

Threads[ConditionVariable]

  

Wait

  

wait for a condition variable to be signaled

 

Calling Sequence

Parameters

Description

Examples

Calling Sequence

Wait( condId, mutexId )

Parameters

condId

-

(integer) condition variable identifier

mutexId

-

(integer) mutex identifier

Description

• 

The Wait command is called to pause the current thread until it is signaled by the condition variable condId.  The Wait command accepts a mutex identifier as mutexId.  The current thread must have obtained the lock on mutexId by calling the Threads[Mutex][Lock] command. When Wait is called, the thread is paused and mutexId is unlocked atomically.  When the thread is started again, it will re-obtain the lock on mutexId before the thread returns from Wait. Another thread can use the Signal or Broadcast commands with condId as the argument to restart a waiting thread. The Signal and Broadcast commands do not require having mutexId locked, however it is often a good idea to do so.  By locking mutexId while calling Signal or Broadcast you can guarantee that no threads are entering Wait at the same time.

• 

For more information on using condition variables, see the Condition Variable help page.

Examples

This is an example of a Producer/Consumer pattern.

The Producer creates jobs (here represented by integers in the global table tab).  The Consumers remove a job, process it, then insert the result back into the table.

We have two condition variables and one mutex.  One condition variable (cp) is used for the Producer thread.  The Producer thread will pause on the producer condition variable if enough jobs are available for processing. The other condition variable (cc) is used for the Consumer threads.  When no jobs are available, the Consumer threads will pause on that condition variable until the Producer creates more jobs.

The mutex is used to protect access to the global variable tab.

Producer := proc( m, cp, cc, max, mindiff )
    global tab, e;
    local i,j,n;

    Threads[Mutex][Lock]( m );
    j := 0;

    tab[ "maxjob" ] := mindiff;
    tab[ "curjob" ] := 1;

    for j from 1 to mindiff
    do
        tab[ j ] := 2*j;
    end do;

    Threads[ConditionVariable][Signal]( cp );

    n := false;
    while ( e )
    do
        j := tab[ "maxjob" ];
        if ( j - tab[ "curjob" ] > mindiff/2 ) then
            n := true;
            Threads[ConditionVariable][Wait]( cp, m );
        end if;

        for i from j to tab[ "curjob" ] + mindiff
        do
            tab[ i ] := 2*i;
        end do;

        tab[ "maxjob" ] := tab[ "curjob" ] + mindiff;

        if ( n ) then
            Threads[ConditionVariable][Broadcast]( cc );
            n := false;
        end if;

    end do;
    Threads[Mutex][Unlock]( m );

end proc:
Consumer := proc( m, cp, cc, max )
    global tab, e;
    local n, i, j, num;

    num := 0;
    Threads[Mutex][Lock]( m );
    while ( num < max )
    do
        while ( tab[ "curjob" ] = tab[ "maxjob" ] )
        do
            Threads[ConditionVariable][Signal]( cp );
            Threads[ConditionVariable][Wait]( cc, m );
        end do;

        n := tab[ "curjob" ];
        j := tab[ n ];
        tab[ "curjob" ] := n + 1;

        Threads[Mutex][Unlock]( m );

        j := add( i, i=1..j );
        num := num+1;

        Threads[Mutex][Lock]( m );
        tab[ n ] := j;
    end do;
    Threads[Mutex][Unlock]( m );
end proc:

tabtable&colon;

mThreads&lsqb;Mutex&rsqb;&lsqb;Create&rsqb;

m:=2

(1)

ccThreads&lsqb;ConditionVariable&rsqb;&lsqb;Create&rsqb;

cc:=1

(2)

cpThreads&lsqb;ConditionVariable&rsqb;&lsqb;Create&rsqb;

cp:=2

(3)

etrue&colon;

Start the Producer thread. We wait on cp until the Producer thread has started.

Threads&lsqb;Mutex&rsqb;&lsqb;Lock&rsqb;m

id1Threads&lsqb;Create&rsqb;Producerm&comma;cp&comma;cc&comma;31&comma;10

id1:=1

(4)

Threads&lsqb;ConditionVariable&rsqb;&lsqb;Wait&rsqb;cp&comma;m

Threads&lsqb;Mutex&rsqb;&lsqb;Unlock&rsqb;m

Start the Consumer threads.  They will each consume 100 jobs and there are 5 threads so we should process 500 jobs.

id2seqThreads&lsqb;Create&rsqb;Consumerm&comma;cp&comma;cc&comma;100&comma;i&equals;1..5

id2:=2&comma;3&comma;4&comma;5&comma;6

(5)

Wait for the Consumer threads to finish.

Threads&lsqb;Wait&rsqb;opid2

Threads&lsqb;Mutex&rsqb;&lsqb;Lock&rsqb;m

Shutdown the Producer thread.

efalse&colon;

Threads&lsqb;ConditionVariable&rsqb;&lsqb;Signal&rsqb;cp

Threads&lsqb;Mutex&rsqb;&lsqb;Unlock&rsqb;m

Threads&lsqb;Wait&rsqb;id1

Check the number of processed jobs.

printtabcurjob

501

(6)

Check the results of one job.

printtab233

108811

(7)

printaddi&comma;i&equals;1..2233

108811

(8)

See Also

Threads

Threads[ConditionVariable][Broadcast]

Threads[ConditionVariable][Create]

Threads[ConditionVariable][Destroy]

Threads[ConditionVariable][Signal]

Threads[ConditionVariable][Wait]

Threads[Mutex]

 


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam