libsnooze/README.md

74 lines
1.5 KiB
Markdown
Raw Permalink Normal View History

2023-02-23 13:41:51 +00:00
<p align="center">
<img src="branding/logo.png" width=220>
</p>
2023-02-23 13:29:56 +00:00
2023-02-23 13:41:51 +00:00
<br>
<h1 align="center">libsnooze</h1>
<h3 align="center"><i><b>A wait/notify mechanism for D</i></b></h3>
---
<br>
<br>
2023-02-23 13:29:56 +00:00
2023-03-25 20:45:32 +00:00
[![D](https://github.com/deavmi/libsnooze/actions/workflows/d.yml/badge.svg)](https://github.com/deavmi/libsnooze/actions/workflows/d.yml)
2023-02-26 15:51:46 +00:00
## API
To see the full documentation (which is always up-to-date) check it out on [DUB](https://libsnooze.dpldocs.info/).
2023-02-23 13:38:55 +00:00
## Usage
2023-02-23 13:29:56 +00:00
### Importing issues
Currently importing just with `import libsnooze` is broken, we recommend you import as follows:
```d
import libsnooze.clib;
import libsnooze;
```
Which should build!
### Example
2023-02-23 13:38:55 +00:00
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.notifyAll();
2023-02-26 15:51:46 +00:00
```