www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - protothreads

Hi, is there a way to build an implementation of protothreads in the D language

It's a very leightweight way to do event-driven programming.
Instead of forking threads, this library (which actually is only precompiler
code) helps to build switch statements around the codeblocks you want to be
executed asynchronous.

To show how easy it can handle complex blocking situations, I will show two
lines from the examples included in the library:
timer_set(&codelock_timer, 1000);
PT_WAIT_UNTIL(pt, key_pressed() || timer_expired(&codelock_timer));

Because D has no precompiler (and I am pretty new to it), I found no way to
replace something with incomplete codeblocks like:
#define x int a
#define y = 10;
a y

If you look at the source of protothreads, you will see that it's just about 40
lines of C code (ignoring the comments) !

So if an implementation of it can be done for D, wouldn't it be worth it ?

Btw, maybe it could be done better in D, for example by using staticif instead
of the switch statement (to avoid creating scopes) ?

Thanks in advance,
Dec 22 2005