photon v0.9.0 with Go-style D-flavored channels!
Dmitry Olshansky
dmitry.olsh at gmail.com
Fri May 3 17:12:40 UTC 2024
On Monday, 29 April 2024 at 20:50:59 UTC, Dmitry Olshansky wrote:
> On Monday, 29 April 2024 at 20:50:24 UTC, Dmitry Olshansky
> wrote:
>> Photon is a minimalistic multi-threaded fiber scheduler and
>> event loop that works transparently with traditional blocking
>> I/O C/C++/D/Rust libraries w/o degrading performance.
>>
And now we have Channels, gentelmen. The only missing bit is
`select` function to multiplex on a bunch of channels.
So here is example of Go-style, D-flavored channels:
```d
import std.algorithm, std.datetime, std.range, std.stdio;
import photon;
void first(shared Channel!string work, shared Channel!int
completion) {
delay(2.msecs);
work.put("first #1");
delay(2.msecs);
work.put("first #2");
delay(2.msecs);
work.put("first #3");
completion.put(1);
}
void second(shared Channel!string work, shared Channel!int
completion) {
delay(3.msecs);
work.put("second #1");
delay(3.msecs);
work.put("second #2");
completion.put(2);
}
void main() {
startloop();
auto jobQueue = channel!string(2);
auto finishQueue = channel!int(1);
go({
first(jobQueue, finishQueue);
});
go({ // producer # 2
second(jobQueue, finishQueue);
});
go({ // consumer
foreach (item; jobQueue) {
delay(1.seconds);
writeln(item);
}
});
go({ // closer
auto completions = finishQueue.take(2).array;
assert(completions.length == 2);
jobQueue.close(); // all producers are done
});
runFibers();
}
```
>
> Obligatory link:
> https://github.com/DmitryOlshansky/photon
>
---
Dmitry Olshansky
CEO @ [Glow labs](https://glow-labs.pro)
https://olshansky.me/about/
More information about the Digitalmars-d-announce
mailing list