数据结构【三】:简单优先队列PriorityQueue
在数据结构【二】:简单阻塞队列BlockingQueue的基础上添加权限属性:priority,并控制enqueue时根据priority排序插入.
1.定义priority取值范围0~9
2.dequeue取出priority值最大的节点(最高进先出 largest-in,first-out ).
3.若priority相等,则继续遵循FIFO原则
注意 : 此代码未经生产环境检验,仅供学习参考.
PriorityQueue.h
#ifndef CUR_PRIORITYQUEUE_H
#define CUR_PRIORITYQUEUE_H
#include <stdlib.h>
#include <pthread.h> struct node{
int value;
int priority; //优先级,取值范围0~9
struct node * next;
}; typedef struct PriorityQueue_ST{
int capacity,remaining,closed;
struct node * head, *tail;
pthread_mutex_t queue_mutex;
pthread_cond_t cond_not_full;
pthread_cond_t cond_not_empty;
pthread_cond_t cond_empty;
}PriorityQueue; extern PriorityQueue* empty_queue(int _capacity);
extern int queue_free(PriorityQueue *q);
extern int is_empty(const PriorityQueue *q);
extern int is_full(const PriorityQueue *q);
extern int enqueue(struct node *item, PriorityQueue *q);
extern struct node* dequeue(PriorityQueue *q); #endif
PriorityQueue.c
#include "PriorityQueue.h"
#include <stdio.h> PriorityQueue* empty_queue(int _capacity)
{
PriorityQueue *q = malloc(sizeof(PriorityQueue));
q->head = q->tail = NULL;
q->capacity = q->remaining = _capacity;
q->closed = ;
pthread_mutex_init(&q->queue_mutex , NULL);
pthread_cond_init(&q->cond_not_full , NULL);
pthread_cond_init(&q->cond_not_empty , NULL);
pthread_cond_init(&q->cond_empty , NULL);
return q;
} int queue_free(PriorityQueue *q)
{
pthread_mutex_lock(&q->queue_mutex);
printf("close queue...\n");
q->closed = ;
//等待cond_empty
while(!is_empty(q))
{
pthread_cond_wait(&q->cond_empty, &q->queue_mutex);
}
free(q);
pthread_mutex_unlock(&q->queue_mutex);
printf("closed...\n");
} int is_empty(const PriorityQueue *q)
{
return q->capacity == q->remaining;
} int is_full(const PriorityQueue *q)
{
return q->remaining == ;
} int enqueue(struct node *item, PriorityQueue *q)
{ if(q->closed) goto err;
//lock
pthread_mutex_lock(&q->queue_mutex);
//等待cond_not_full
while(is_full(q))
{
pthread_cond_wait(&q->cond_not_full, &q->queue_mutex);
}
//设置最权限:大为9,最小为0,默认为0
item->priority = item->priority > ? : item->priority;
item->priority = item->priority < ? : item->priority; if(is_empty(q))
{
q->head = q->tail = item;
//通知所有等待cond_not_empty的线程
pthread_cond_broadcast(&q->cond_not_empty);
}
else
{
//插入时按优先级排序 : order by priority desc
struct node *big = NULL;
struct node *small = q->head;
while(small != NULL && small->priority >= item->priority)
{
big = small;
small = small->next;
} if(small == NULL)
{
//尾部插入 : big = q->tail
q->tail->next = item;
q->tail = item;
}else if(big == NULL)
{
//头部插入
q->head = item;
item->next = small;
}else
{
//中部插入
big->next = item;
item->next = small;
} }
q->remaining--;
//unlock
pthread_mutex_unlock(&q->queue_mutex); return ;
err :
return -;
} struct node* dequeue(PriorityQueue *q)
{ //已经关闭的空队列
if(q->closed && is_empty(q)) goto err;
//lock
pthread_mutex_lock(&q->queue_mutex);
//空队列,等待cond_not_empty
while(!q->closed && is_empty(q))
{
pthread_cond_wait(&q->cond_not_empty, &q->queue_mutex);
}
//take
struct node * temp = q->head;
q->head = q->head->next; //在未关闭队列的情况下,唤醒enqueue等待线程
if(!q->closed && is_full(q))
{
pthread_cond_broadcast(&q->cond_not_full); //TODO 1
}
q->remaining++;
//唤醒关闭队列线程
if(q->closed && is_empty(q))
{
pthread_cond_signal(&q->cond_empty);//TODO 2
} //注意:TODO 1和TODO 2其实是互斥的,不可能同时满足条件
//必须先判断是否激活cond_not_full然后remaining++
//最后再判断是否激活cond_empty
//unlock
pthread_mutex_unlock(&q->queue_mutex);
return temp;
err:
return NULL;
}
测试代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "PriorityQueue.h"
extern void* func_put(void* _q); PriorityQueue *q;
pthread_t thread1;
void main()
{
q = empty_queue();
pthread_create(&thread1,NULL,func_put,(void*)q);
srand((int) time()); int i;
for(i=; i<=; i++)
{
struct node * item = (struct node *)malloc(sizeof(struct node));
item->value = i;
item->next = NULL;
item->priority = ( + rand() % );
enqueue(item,q);
printf("enqueue -> value : %d, priority : %d, remaining : %d\n",i,item->priority,q->remaining);
sleep();
}
queue_free(q);
} void* func_put(void* _q)
{
PriorityQueue *q = (PriorityQueue*)_q;
struct node *item;
while((item = dequeue(q)) != NULL)
{
printf("dequeue -> value : %d, priority : %d, remaining : %d\n",item->value,item->priority,q->remaining);
free(item);
sleep();
}
}
测试结果:
数据结构【三】:简单优先队列PriorityQueue的更多相关文章
- 【Java源码】集合类-优先队列PriorityQueue
一.类继承关系 public class PriorityQueue<E> extends AbstractQueue<E> implements java.io.Serial ...
- [Swift]实现优先队列PriorityQueue
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Java优先队列PriorityQueue的各种打开方式以及一些你不知道的细节
目录 Java优先队列PriorityQueue的各种打开方式以及一些你不知道的细节 优先队列的默认用法-从小到大排序 对String类用优先队列从大到小排序 通过自定义比较器对自定义的类进行从小到大 ...
- .NET 6 优先队列 PriorityQueue 实现分析
在最近发布的 .NET 6 中,包含了一个新的数据结构,优先队列 PriorityQueue, 实际上这个数据结构在隔壁 Java中已经存在了很多年了, 那优先队列是怎么实现的呢? 让我们来一探究竟吧 ...
- golang实现的简单优先队列
下面是golang实现的简单优先队列,参考信息可以查看https://golang.org/pkg/container/heap/或者https://golang.google.cn/pkg/cont ...
- 堆数据结构(heapq)简单应用
## 堆数据结构(heapq)简单应用 # 堆数据结构 heapq # 常用方法:nlargest(),nsmallest(),heapify(),heappop() # 如果需要的个数较小,使用nl ...
- Java的优先队列PriorityQueue详解
一.优先队列概述 优先队列PriorityQueue是Queue接口的实现,可以对其中元素进行排序, 可以放基本数据类型的包装类(如:Integer,Long等)或自定义的类 对于基本数据类型的包装器 ...
- 学习javascript数据结构(三)——集合
前言 总括: 本文讲解了数据结构中的[集合]概念,并使用javascript实现了集合. 原文博客地址:学习javascript数据结构(三)--集合 知乎专栏&&简书专题:前端进击者 ...
- JSP学习笔记(三):简单的Tomcat Web服务器
注意:每次对Tomcat配置文件进行修改后,必须重启Tomcat 在E盘的DATA文件夹中创建TomcatDemo文件夹,并将Tomcat安装路径下的webapps/ROOT中的WEB-INF文件夹复 ...
随机推荐
- WebRtc VoiceEngine代码解析
WebRtc中VoiceEngine可以完成大部分的VOIP相关人物,包括采集.自动增益.噪声消除.回声抑制.编解码.RTP传输.下边我们通过代码来解析Voe中处理流程: 创建VoiceEngine和 ...
- 可迭代的集合类型使用foreach语句
在学习算法这本书图论那一部分的时候,接触到了几个类似for(int w:G.adj(v)),的语句,不是很理解,就去百度,发现这是一种叫做foreach的语法,在书的76页有讲到,但是之前没认真看书, ...
- LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)
http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- JDBC 常用驱动类及url格式
1. oracle <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</ ...
- C# 使用ping命令
方法一:调用cmd 的ping命令 private static string CmdPing(string strIp) { Process p = new Process(); p.StartIn ...
- kindeditor 上传图片 显示绝对 路径
在前台页面上 效果图 另外附上 urlType 属性的 参数说明: 改变站内本地URL,可设置空.relative.absolute.domain. 空为不修改URL,relative为相对路径,ab ...
- 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.3 Details用户详细信息]
3.3 Details用户详细信息 用户详细信息是通过objectId获取.代码如下 public async Task<ActionResult> Details(string obje ...
- dojo grid 分页
dojo很强大,也很方便,但是缺少文档,只能看源代码,也挺好的,就是费时间... 网上找了一段代码(找不到原出处了,不好意思),也看了dojo自带的demo,放一段可以执行的页面代码这里.把ip换成自 ...
- iOS开发-UIColor转UIIamge方法
只能说太神奇了,完美应用到我的毕业设计 - (UIImage*) createImageWithColor: (UIColor*) color { CGRect rect=CGRectMake(,,s ...