信号槽使用mythread
This commit is contained in:
		| @@ -8,18 +8,19 @@ | ||||
| #include "stdio.h" | ||||
| #include "errno.h" | ||||
| #include <semaphore.h> | ||||
| #include "exception.h" | ||||
| #include "mythread.h" | ||||
| #include "debug.h" | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| static void* _thread(void* arg) { | ||||
| static int _thread(void* arg) { | ||||
|   mythread_t* self = arg; | ||||
|   slot_list_with_pars *slot_p; | ||||
|   while (1) { | ||||
|     slot_p = NULL; | ||||
|     sem_wait(&self->sem); | ||||
|     pthread_mutex_lock(&self->lock); | ||||
|     printf("sem take\n"); | ||||
|     DBG_INFO("sem take\n"); | ||||
|     // 把槽列表里的函数取出来 | ||||
|     if (self->slot_head) { | ||||
|       slot_p = self->slot_head; | ||||
| @@ -28,14 +29,14 @@ static void* _thread(void* arg) { | ||||
|     pthread_mutex_unlock(&self->lock); | ||||
|     if (slot_p) { | ||||
|       if (slot_p->func) { | ||||
|         printf("args_p=%p\n", slot_p); | ||||
|         DBG_INFO("args_p=%p\n", slot_p); | ||||
|         slot_p->func(slot_p); | ||||
|       } | ||||
|       // 槽函数执行完之后释放 | ||||
|       free(slot_p); | ||||
|     } | ||||
|   } | ||||
|   return NULL; | ||||
|   return 0; | ||||
| } | ||||
|  | ||||
|  | ||||
| @@ -52,7 +53,7 @@ void send_slot_fun(mythread_t *self, slot_list_with_pars* slot_p) { | ||||
|   } | ||||
|   pthread_mutex_unlock(&self->lock); | ||||
|   sem_post(&self->sem); | ||||
|   printf("add_slot_fun\n"); | ||||
|   DBG_INFO("add_slot_fun\n"); | ||||
| } | ||||
|  | ||||
|  | ||||
| @@ -79,11 +80,12 @@ void _connect(void* sig_obj, void* sig_fun, mythread_t* thread, void* slot_obj, | ||||
| } | ||||
|  | ||||
|  | ||||
| mythread_t *sigthread_init() { | ||||
| mythread_t *sigthread_init(const char *name) { | ||||
|   mythread_t* self = calloc(1, sizeof(mythread_t)); | ||||
|   pthread_mutex_init(&self->lock, NULL); | ||||
|   sem_init(&self->sem, 0, 0); | ||||
|   pthread_create(&self->tid, NULL, _thread, self); | ||||
|   // pthread_create(&self->tid, NULL, _thread, self); | ||||
|   self->tid = myth_create(_thread, self, name); | ||||
|   return self; | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user