61 lines
1.2 KiB
C
61 lines
1.2 KiB
C
|
|
#include <pthread.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <semaphore.h>
|
|
#include <stdatomic.h>
|
|
|
|
#include <waitgroup.h>
|
|
|
|
pthread_t thread;
|
|
atomic_bool thread_cancel;
|
|
wg_t wg;
|
|
|
|
|
|
void* thread_run(void*);
|
|
|
|
int server_init(void) {
|
|
int res = 0;
|
|
thread_cancel = false;
|
|
|
|
wg_init(&wg);
|
|
|
|
pthread_attr_t attr;
|
|
pthread_attr_init(&attr);
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
wg_add(&wg);
|
|
pthread_create(&thread, &attr, thread_run, NULL);
|
|
pthread_attr_destroy(&attr);
|
|
|
|
return res;
|
|
}
|
|
|
|
int server_run(void) {
|
|
int res = 0;
|
|
void *retval;
|
|
printf("server run\n");
|
|
sleep(4);
|
|
thread_cancel = true;
|
|
printf("thread cancel\n");
|
|
wg_wait(&wg);
|
|
printf("exit\n");
|
|
return res;
|
|
}
|
|
|
|
void* thread_run(void*) {
|
|
for(;;) {
|
|
if (thread_cancel == true) {
|
|
printf("thread canceled\n");
|
|
sleep(1);
|
|
wg_done(&wg);
|
|
return NULL;
|
|
|
|
}
|
|
printf("thread run\n");
|
|
sleep(1);
|
|
}
|
|
return NULL;
|
|
}
|
|
|