Skip to content

Instantly share code, notes, and snippets.

@op
Created May 24, 2013 05:29

Revisions

  1. op created this gist May 24, 2013.
    58 changes: 58 additions & 0 deletions thread_recv_block.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    #include "src/nn.h"
    #include "src/reqrep.h"

    #include "src/utils/err.c"
    #include "src/utils/thread.c"
    #include "src/utils/sleep.c"

    #include <stddef.h>
    #include <assert.h>
    #include <stdlib.h>
    #include <string.h>

    int rep;

    void worker (void *arg)
    {
    int rc;
    int i;
    int timeo;
    char buf[3];

    rc = nn_recv(rep, buf, 3, 0);
    }

    int main(int argc, char *argv[])
    {
    int rc;
    int req;
    int timeo;
    struct nn_thread thread;

    req = nn_socket(AF_SP, NN_REQ);
    assert(req != -1);
    rc = nn_connect(req, "inproc://thread_recv_close");
    assert(rc >= 0);

    rep = nn_socket(AF_SP, NN_REP);
    assert(rep != -1);
    rc = nn_bind(rep, "inproc://thread_recv_close");
    assert(rc >= 0);

    /* Wait a bit till the worker thread blocks in nn_recv(). */
    nn_thread_init(&thread, worker, NULL);
    nn_sleep(100);

    /* Calling nn_term() before close will unblock the thread. */
    /* nn_term(); */

    rc = nn_close(req);
    assert(rc == 0);

    rc = nn_close(rep);
    assert(rc == 0);

    nn_thread_term(&thread);

    return 0;
    }