Sha256: 32945bbdf7dcc64d946d8913b47f9538eb3fac1e498a0ebf658ac5e69735f861

Contents?: true

Size: 1.4 KB

Versions: 15

Compression:

Stored size: 1.4 KB

Contents

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <uv.h>

uv_loop_t* create_loop()
{
    uv_loop_t *loop = malloc(sizeof(uv_loop_t));
    if (loop) {
      uv_loop_init(loop);
    }
    return loop;
}

void signal_handler(uv_signal_t *handle, int signum)
{
    printf("Signal received: %d\n", signum);
    uv_signal_stop(handle);
}

// two signal handlers in one loop
void thread1_worker(void *userp)
{
    uv_loop_t *loop1 = create_loop();

    uv_signal_t sig1a, sig1b;
    uv_signal_init(loop1, &sig1a);
    uv_signal_start(&sig1a, signal_handler, SIGUSR1);

    uv_signal_init(loop1, &sig1b);
    uv_signal_start(&sig1b, signal_handler, SIGUSR1);

    uv_run(loop1, UV_RUN_DEFAULT);
}

// two signal handlers, each in its own loop
void thread2_worker(void *userp)
{
    uv_loop_t *loop2 = create_loop();
    uv_loop_t *loop3 = create_loop();

    uv_signal_t sig2;
    uv_signal_init(loop2, &sig2);
    uv_signal_start(&sig2, signal_handler, SIGUSR1);

    uv_signal_t sig3;
    uv_signal_init(loop3, &sig3);
    uv_signal_start(&sig3, signal_handler, SIGUSR1);

    while (uv_run(loop2, UV_RUN_NOWAIT) || uv_run(loop3, UV_RUN_NOWAIT)) {
    }
}

int main()
{
    printf("PID %d\n", getpid());

    uv_thread_t thread1, thread2;

    uv_thread_create(&thread1, thread1_worker, 0);
    uv_thread_create(&thread2, thread2_worker, 0);

    uv_thread_join(&thread1);
    uv_thread_join(&thread2);
    return 0;
}

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
mt-libuv-4.1.04 ext/libuv/docs/code/signal/main.c
mt-libuv-4.1.03 ext/libuv/docs/code/signal/main.c
mt-libuv-4.1.02 ext/libuv/docs/code/signal/main.c
libuv-4.0.9 ext/libuv/docs/code/signal/main.c
libuv-4.0.2 ext/libuv/docs/code/signal/main.c
libuv-4.0.1 ext/libuv/docs/code/signal/main.c
libuv-4.0.0 ext/libuv/docs/code/signal/main.c
libuv-3.3.0 ext/libuv/docs/code/signal/main.c
libuv-3.2.4 ext/libuv/docs/code/signal/main.c
libuv-3.2.3 ext/libuv/docs/code/signal/main.c
libuv-3.2.2 ext/libuv/docs/code/signal/main.c
libuv-3.2.1 ext/libuv/docs/code/signal/main.c
libuv-3.2.0 ext/libuv/docs/code/signal/main.c
libuv-3.1.9 ext/libuv/docs/code/signal/main.c
libuv-3.1.8 ext/libuv/docs/code/signal/main.c