void* dispatch_worker(void* arg) { cli_queue* q = arg; q->size = 0; q->head = q->tail = NULL; pthread_mutex_init(&q->lock, NULL); while (1) { pthread_mutex_lock(&q->lock); if (q->size > 0) { client* cli = cq_front(q); cq_pop_front(q); pthread_mutex_unlock(&q->lock); if (cli->conn == -1) { free(cli); pthread_mutex_destroy(&q->lock); return NULL; } else { pool.slave(cli->conn, cli->evt, cli->arg); free(cli); } } else { pthread_mutex_unlock(&q->lock); usleep(THREAD_POLL_TIME); } } return NULL; }
void print_levelbylevel(ptr_rbnode root) { CircularQueue *cq; cq_init(cq, ELEMENT_MAX); cq_push(cq, root); int depth_cnt = -1; while ((depth_cnt = cq->capacity)) { while (depth_cnt--) { cq_element_t current_node = cq_front(cq); cq_pop(cq); if (current_node->nil) { printf("N "); } else { printf("%d%s ", current_node->val, current_node->color ? "(B)" : "(R)"); cq_push(cq, current_node->left); cq_push(cq, current_node->right); } } printf("\n"); } cq_destroy(cq); }