Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

core.sync.condition

The condition module provides a primitive for synchronized condition checking.
License:
Boost License 1.0
Authors:
Sean Kelly

Sourcecore/sync/condition.d

classCondition;
This class represents a condition variable as conceived by C.A.R. Hoare. As per Mesa type monitors however, "signal" has been replaced with "notify" to indicate that control is not transferred to the waiter when a notification is sent.
Examples:
import core.sync.mutex;import core.sync.semaphore;import core.thread;void testNotify(){auto mutex      =new Mutex;auto condReady  =newCondition( mutex );auto semDone    =new Semaphore;auto synLoop    =new Object;int  numWaiters = 10;int  numTries   = 10;int  numReady   = 0;int  numTotal   = 0;int  numDone    = 0;int  numPost    = 0;void waiter()    {for (int i = 0; i < numTries; ++i )        {synchronized( mutex )            {while ( numReady < 1 )                {                    condReady.wait();                }                --numReady;                ++numTotal;            }synchronized( synLoop )            {                ++numDone;            }            semDone.wait();        }    }auto group =new ThreadGroup;for (int i = 0; i < numWaiters; ++i )        group.create( &waiter );for (int i = 0; i < numTries; ++i )    {for (int j = 0; j < numWaiters; ++j )        {synchronized( mutex )            {                ++numReady;                condReady.notify();            }        }while (true )        {synchronized( synLoop )            {if ( numDone >= numWaiters )break;            }            Thread.yield();        }for (int j = 0; j < numWaiters; ++j )        {            semDone.notify();        }    }    group.joinAll();assert( numTotal == numWaiters * numTries );}void testNotifyAll(){auto mutex      =new Mutex;auto condReady  =newCondition( mutex );int  numWaiters = 10;int  numReady   = 0;int  numDone    = 0;bool alert      =false;void waiter()    {synchronized( mutex )        {            ++numReady;while ( !alert )                condReady.wait();            ++numDone;        }    }auto group =new ThreadGroup;for (int i = 0; i < numWaiters; ++i )        group.create( &waiter );while (true )    {synchronized( mutex )        {if ( numReady >= numWaiters )            {                alert =true;                condReady.notifyAll();break;            }        }        Thread.yield();    }    group.joinAll();assert( numReady == numWaiters && numDone == numWaiters );}void testWaitTimeout(){auto mutex      =new Mutex;auto condReady  =newCondition( mutex );bool waiting    =false;bool alertedOne =true;bool alertedTwo =true;void waiter()    {synchronized( mutex )        {            waiting    =true;// we never want to miss the notification (30s)            alertedOne = condReady.wait( dur!"seconds"(30) );// but we don't want to wait long for the timeout (10ms)            alertedTwo = condReady.wait( dur!"msecs"(10) );        }    }auto thread =new Thread( &waiter );    thread.start();while (true )    {synchronized( mutex )        {if ( waiting )            {                condReady.notify();break;            }        }        Thread.yield();    }    thread.join();assert( waiting );assert( alertedOne );assert( !alertedTwo );}testNotify();testNotifyAll();testWaitTimeout();
nothrow @nogc @safe this(Mutexm);

nothrow @nogc @safe this(shared Mutexm) shared;
Initializes a condition object which is associated with the supplied mutex object.
Parameters:
MutexmThe mutex with which this condition will be associated.
Throws:
SyncError on error.
@property Mutexmutex();

@property shared(Mutex)mutex() shared;
Gets the mutex associated with this condition.
Returns:
The mutex associated with this condition.
voidwait();

voidwait() shared;

voidwait(this Q)(bool_unused_)
if (is(Q == Condition) || is(Q == shared(Condition)));
Wait until notified.
Throws:
SyncError on error.
boolwait(Durationval);

boolwait(Durationval) shared;

boolwait(this Q)(Durationval, bool_unused_)
if (is(Q == Condition) || is(Q == shared(Condition)));
Suspends the calling thread until a notification occurs or until the supplied time period has elapsed.
Parameters:
DurationvalThe time to wait.

Inval must be non-negative.

Throws:
SyncError on error.
Returns:
true if notified before the timeout and false if not.
voidnotify();

voidnotify() shared;

voidnotify(this Q)(bool_unused_)
if (is(Q == Condition) || is(Q == shared(Condition)));
Notifies one waiter.
Throws:
SyncError on error.
voidnotifyAll();

voidnotifyAll() shared;

voidnotifyAll(this Q)(bool_unused_)
if (is(Q == Condition) || is(Q == shared(Condition)));
Notifies all waiters.
Throws:
SyncError on error.
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Sat Feb 21 04:06:29 2026

[8]ページ先頭

©2009-2026 Movatter.jp