- Added example usage

This commit is contained in:
Tristan B. Velloza Kildaire 2023-02-23 15:38:55 +02:00
parent 7579b9bd42
commit e73cb325b6
1 changed files with 39 additions and 2 deletions

View File

@ -3,7 +3,44 @@
<h1>libsnooze</h1>
</center>
<hr>
<!-- <hr> -->
## Usage
TODO:
Firstly we create an `Event` which is something that can be notified or awaited on. This is simply accomplished as follows:
```d
Event myEvent = new Event();
```
Now let's create a thread which consumes `myEvent` and waits on it:
```d
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...");
event.wait();
writeln("("~to!(string)(Thread.getThis().id())~") Thread is waiting... [done]");
}
}
TestThread thread1 = new TestThread(event);
thread1.start();
```
Now on the main thread we can do the following to wakeup waiting threads:
```d
/* Wake up all sleeping on this event */
event.notify();
```