博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程读写链表 pthread
阅读量:6195 次
发布时间:2019-06-21

本文共 3288 字,大约阅读时间需要 10 分钟。

hot3.png

#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);  }

 

转载于:https://my.oschina.net/hanxiaodong/blog/1541982

你可能感兴趣的文章
CSS一些最佳实践
查看>>
8.Kubernetes Service(服务)
查看>>
iOS开发库的族“.NET研究”谱介绍
查看>>
图解DevExpress RichEditControl富文本的使用,附源码及官方API
查看>>
三级联动---城市地区选择
查看>>
剖析 Laravel 计划任务--避免重复
查看>>
公司框架遇到的问题
查看>>
详解 Discuz 的 PHP经典加密解密函数 authcode
查看>>
Mysql XX 天之内
查看>>
Oracle如何删除表中重复记录
查看>>
nginx 是如何处理访问请求的
查看>>
wget参数用法详解
查看>>
【COCOS2D-X(1.X 2.X)】Json(cpp版)以及添加自定义字体库教程
查看>>
使用curl命令查看访问url的时间
查看>>
whois
查看>>
python添加环境变量
查看>>
Linux 新手容易犯的 7 个错误
查看>>
DP-01背包 (题)
查看>>
WinForm中跨线程操作控件
查看>>
CODING 敏捷实践完全指南
查看>>