import
This commit is contained in:
60
works/server.c
Normal file
60
works/server.c
Normal file
@@ -0,0 +1,60 @@
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
9
works/server.h
Normal file
9
works/server.h
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
#ifndef SERVER_H_QWERTY
|
||||
#define SERVER_H_QWERTY
|
||||
|
||||
int server_init(void);
|
||||
int server_run(void);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user