libdill icon indicating copy to clipboard operation
libdill copied to clipboard

A bug in structured-concurrency tutorial

Open suvarchal opened this issue 3 years ago • 1 comments

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.

suvarchal avatar Jan 03 '22 22:01 suvarchal

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;
}

plazer1 avatar Mar 09 '22 19:03 plazer1