sustrik / libdill

Structured concurrency in C
MIT License
1.68k stars 155 forks source link

A bug in structured-concurrency tutorial #213

Open suvarchal opened 2 years ago

suvarchal commented 2 years ago

In section
What are the use cases? > Parent coroutine closes child coroutine http://libdill.org/structured-concurrency.html#parent-coroutine-closes-child-coroutine

In the code example shown,i think, does not fit to the description, it was a bit of confusion for me until to realize later it might be a bug in the example. Given example

coroutine void worker(void) {
    int rc = msleep(now() + 2000);
    if(rc < 0 && errno == ECANCELED) return; /* 4. */
    /* 2. */
}

int main(void) {
    int cr = go(worker()); /* 1. */
    msleep(now() + (random() % 1000));
    hclose(cr); /* 3. */
    return 0;
}

should be with random deadline in the worker

coroutine void worker(void) {
    int rc = msleep(now() + (random() % 1000));
    if(rc < 0 && errno == ECANCELED) return; /* 4. */
    /* 2. */
}

int main(void) {
    int cr = go(worker()); /* 1. */
    msleep(now() + 2000);
    hclose(cr); /* 3. */
    return 0;
}

btw thanks for the wonderful library and well thought examples in tutorial.

plazer1 commented 2 years ago

I noticed this as well. Funnily enough, your correction is incorrect too. It should be like this:

coroutine void worker(void) {
    int rc = msleep(now() + (random() % 2000));
    if(rc < 0 && errno == ECANCELED) return; /* 4. */
    /* 2. */
}

int main(void) {
    int cr = go(worker()); /* 1. */
    msleep(now() + 1000);
    hclose(cr); /* 3. */
    return 0;
}