core.sync.event

The event module provides a primitive for lightweight signaling of other threads (emulating Windows events on Posix)

License:
Distributed under the Boost Software License 1.0. (See accompanying file LICENSE)
Authors:
Rainer Schuetze
Source
core/sync/event.d
struct Event;

represents an event. Clients of an event are suspended while waiting for the event to be "signaled".

Implemented using pthread_mutex and pthread_condition on Posix and CreateEvent and SetEvent on Windows.

import core.sync.event, core.thread, std.file;

struct ProcessFile
{
    ThreadGroup group;
    Event event;
    void[] buffer;

    void doProcess()
    {
        event.wait();
        // process buffer
    }

    void process(string filename)
    {
        event.initialize(true, false);
        group = new ThreadGroup;
        for (int i = 0; i < 10; ++i)
            group.create(&doProcess);

        buffer = std.file.read(filename);
        event.set();
        group.joinAll();
        event.terminate();
    }
}

nothrow @nogc this(bool manualReset, bool initialState);

Creates an event object.

Parameters:
bool manualReset the state of the event is not reset automatically after resuming waiting clients
bool initialState initial state of the signal
nothrow @nogc void initialize(bool manualReset, bool initialState);

Initializes an event object. Does nothing if the event is already initialized.

Parameters:
bool manualReset the state of the event is not reset automatically after resuming waiting clients
bool initialState initial state of the signal
nothrow @nogc void terminate();

deinitialize event. Does nothing if the event is not initialized. There must not be threads currently waiting for the event to be signaled.

nothrow @nogc void set();

Set the event to "signaled", so that waiting clients are resumed

nothrow @nogc void reset();

Reset the event manually

nothrow @nogc bool wait();

Wait for the event to be signaled without timeout.

Returns:
true if the event is in signaled state, false if the event is uninitialized or another error occured
nothrow @nogc bool wait(Duration tmout);

Wait for the event to be signaled with timeout.

Parameters:
Duration tmout the maximum time to wait
Returns:
true if the event is in signaled state, false if the event was nonsignaled for the given time or the event is uninitialized or another error occured

© 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/core_sync_event.html