|
Packit |
b5b901 |
#include <stdio.h>
|
|
Packit |
b5b901 |
#include <stdlib.h>
|
|
Packit |
b5b901 |
#include <unistd.h>
|
|
Packit |
b5b901 |
#include <uv.h>
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_loop_t* create_loop()
|
|
Packit |
b5b901 |
{
|
|
Packit |
b5b901 |
uv_loop_t *loop = malloc(sizeof(uv_loop_t));
|
|
Packit |
b5b901 |
if (loop) {
|
|
Packit |
b5b901 |
uv_loop_init(loop);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
return loop;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
void signal_handler(uv_signal_t *handle, int signum)
|
|
Packit |
b5b901 |
{
|
|
Packit |
b5b901 |
printf("Signal received: %d\n", signum);
|
|
Packit |
b5b901 |
uv_signal_stop(handle);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
// two signal handlers in one loop
|
|
Packit |
b5b901 |
void thread1_worker(void *userp)
|
|
Packit |
b5b901 |
{
|
|
Packit |
b5b901 |
uv_loop_t *loop1 = create_loop();
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_signal_t sig1a, sig1b;
|
|
Packit |
b5b901 |
uv_signal_init(loop1, &sig1a);
|
|
Packit |
b5b901 |
uv_signal_start(&sig1a, signal_handler, SIGUSR1);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_signal_init(loop1, &sig1b);
|
|
Packit |
b5b901 |
uv_signal_start(&sig1b, signal_handler, SIGUSR1);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_run(loop1, UV_RUN_DEFAULT);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
// two signal handlers, each in its own loop
|
|
Packit |
b5b901 |
void thread2_worker(void *userp)
|
|
Packit |
b5b901 |
{
|
|
Packit |
b5b901 |
uv_loop_t *loop2 = create_loop();
|
|
Packit |
b5b901 |
uv_loop_t *loop3 = create_loop();
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_signal_t sig2;
|
|
Packit |
b5b901 |
uv_signal_init(loop2, &sig2;;
|
|
Packit |
b5b901 |
uv_signal_start(&sig2, signal_handler, SIGUSR1);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_signal_t sig3;
|
|
Packit |
b5b901 |
uv_signal_init(loop3, &sig3;;
|
|
Packit |
b5b901 |
uv_signal_start(&sig3, signal_handler, SIGUSR1);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
while (uv_run(loop2, UV_RUN_NOWAIT) || uv_run(loop3, UV_RUN_NOWAIT)) {
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
int main()
|
|
Packit |
b5b901 |
{
|
|
Packit |
b5b901 |
printf("PID %d\n", getpid());
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_thread_t thread1, thread2;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_thread_create(&thread1, thread1_worker, 0);
|
|
Packit |
b5b901 |
uv_thread_create(&thread2, thread2_worker, 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_thread_join(&thread1);
|
|
Packit |
b5b901 |
uv_thread_join(&thread2);
|
|
Packit |
b5b901 |
return 0;
|
|
Packit |
b5b901 |
}
|