#define HB_PACKET_FAIL -1#define HB_PACKET_EMPTY 0#define HB_PACKET_SUCCESS 1struct hb_packet{ char *data; int size;};struct hb_packet_list{ struct hb_packet packet; struct hb_packet_list *next;};struct hb_packet_queue{ struct hb_packet_list *first_packet; struct hb_packet_list *last_packet; pthread_mutex_t mutex; pthread_cond_t cond; int count; unsigned int total_size; bool abort;};bool packet_queue_init(struct hb_packet_queue *q){ memset(q, 0, sizeof(struct hb_packet_queue)); if (pthread_mutex_init(&q->mutex, NULL) != 0) goto fail; if (pthread_cond_init(&q->cond, NULL) != 0) goto fail1; return true;fail1: pthread_mutex_destroy(&q->mutex);fail: return false;}void packet_queue_free(struct hb_packet_queue *q){ pthread_mutex_destroy(&q->mutex); pthread_cond_destroy(&q->cond); }int packet_queue_put(struct hb_packet_queue *q, struct hb_packet *packet){ struct hb_packet_list *new_packet; new_packet = (struct hb_packet_list *)malloc(sizeof(struct hb_packet_list)); if (new_packet == NULL) return HB_PACKET_FAIL; new_packet->packet = *packet; new_packet->next = NULL; pthread_mutex_lock(&q->mutex); if (q->last_packet == NULL) q->first_packet = new_packet; else q->last_packet->next = new_packet; q->last_packet = new_packet; q->count++; q->total_size += new_packet->packet.size; pthread_cond_signal(&q->cond); pthread_mutex_unlock(&q->mutex); return HB_PACKET_SUCCESS;}int packet_queue_get(struct hb_packet_queue *q, struct hb_packet *packet,bool block){ struct hb_packet_list *potential_packet; int return_status; pthread_mutex_lock(&q->mutex); while (true) { potential_packet = q->first_packet; if (potential_packet != NULL) { q->first_packet = potential_packet->next; if (q->first_packet == NULL) q->last_packet = NULL; q->count--; q->total_size -= potential_packet->packet.size; *packet = potential_packet->packet; free(potential_packet); return_status = HB_PACKET_SUCCESS; break; } else if (!block) { return_status = HB_PACKET_EMPTY; break; } else { pthread_cond_wait(&q->cond, &q->mutex); if (q->abort) { return_status = HB_PACKET_FAIL; break; } } } pthread_mutex_unlock(&q->mutex); return return_status;}DWORD WINAPI ThreadFun1(LPVOID pM) { TRACE("×ÓÏ̵߳ÄÏß³ÌIDºÅΪ:%d\n×ÓÏß³ÌÊä³öHello World\n",GetCurrentThreadId()); hb_packet_queue *q = (hb_packet_queue *)pM; while(1) { struct hb_packet packet; int i = packet_queue_get(q,&packet,false); TRACE("get result =%d \n",i); if (i == 1) { TRACE("get result =%s \n",packet.data); } Sleep(1111); } return 0; } DWORD WINAPI ThreadFun2(LPVOID pM) { TRACE("×ÓÏ̵߳ÄÏß³ÌIDºÅΪ:%d\n×ÓÏß³ÌÊä³öHello World\n",GetCurrentThreadId()); hb_packet_queue *q = (hb_packet_queue *)pM; while(1) { struct hb_packet packet; packet.data = "111111"; packet.size = sizeof("111111"); int i = packet_queue_put(q, &packet); TRACE("put result =%d \n",i); Sleep(1234); } return 0; } void CMy123444Dlg::OnCancel2() { // TODO: Add your control notification handler code here hb_packet_queue *queue = new hb_packet_queue; packet_queue_init(queue); HANDLE handle1 =CreateThread(NULL,0,ThreadFun1,queue,0,NULL); HANDLE handle2 =CreateThread(NULL,0,ThreadFun2,queue,0,NULL); }