Go to file
Tristan B. Velloza Kildaire be76e09f12 Event
- Use `ptrdiff_t` in `wait(timeval*)`
2023-06-29 18:33:39 +02:00
.github/workflows Create d.yml 2023-03-25 22:44:01 +02:00
branding - Added logo 2023-02-23 15:27:49 +02:00
source/libsnooze Event 2023-06-29 18:33:39 +02:00
.gitignore Initial implementation 2023-02-23 15:16:24 +02:00
LICENSE - Added license 2023-02-23 15:47:46 +02:00
README.md - Update documentation link 2023-06-24 14:46:23 +02:00
dub.json Dub 2023-06-14 16:12:19 +02:00

README.md

libsnooze

A wait/notify mechanism for D


D DUB DUB DUB

API

To see the full documentation (which is always up-to-date) check it out on DUB.

Usage

Importing issues

Currently importing just with import libsnooze is broken, we recommend you import as follows:

import libsnooze.clib;
import libsnooze;

Which should build!

Example

Firstly we create an Event which is something that can be notified or awaited on. This is simply accomplished as follows:

Event myEvent = new Event();

Now let's create a thread which consumes myEvent and waits on it. You will see that we wrap some exception catching around the call to wait(). We have to catch an InterruptedException as a call to wait() can unblock due to a signal being received on the waiting thread, normally you would model yoru program looping back to call wait() again. Also, if there is a problem with the underlying eventing system then a FatalException will be thrown and you should handle this by exiting your program or something.

class TestThread : Thread
{
    private Event event;

    this(Event event)
    {
        super(&worker);
        this.event = event;
    }

    public void worker()
    {
        writeln("("~to!(string)(Thread.getThis().id())~") Thread is waiting...");

        try
        {
            /* Wait */
            event.wait();
            writeln("("~to!(string)(Thread.getThis().id())~") Thread is waiting... [done]");
        }
        catch(InterruptedException e)
        {
            // NOTE: You can maybe retry your wait here
            writeln("Had an interrupt");
        }
        catch(FatalException e)
        {
            // NOTE: This is a FATAL error in the underlying eventing system, do not continue
        }
    }
}

TestThread thread1 = new TestThread(event);
thread1.start();

Now on the main thread we can do the following to wakeup waiting threads:

/* Wake up all sleeping on this event */
event.notifyAll();