这里实现一个基于数组的线程安全的循环队列

11 篇文章 0 订阅
10 篇文章 2 订阅

具体代码如下:

#include<pthread.h>
#include<iostream>
using namespace std;

#define QUEUESIZE 128

template<class object>
class ThreadSafeQueue
{
private:
	pthread_mutex_t m_lock;
	int m_front;
	int m_rear;
	object m_data[QUEUESIZE];
public:
	ThreadSafeQueue():m_front(0),m_rear(0)
	{
		pthread_mutex_init(&m_lock,NULL);
	}
	
	bool EnQueue(object data)
	{
		pthread_mutex_lock(&m_lock);
		if(isFull())
		{
			cout<<"The queue is full!"<<endl;
			pthread_mutex_unlock(&m_lock);
			return false;
		}
		m_data[m_rear] = data;
		m_rear = (m_rear+1)%QUEUESIZE;
		pthread_mutex_unlock(&m_lock);
		return true;
	}
	
	bool DeQueue(object& data)
	{
		pthread_mutex_lock(&m_lock);
		if(isEmpty())
		{
			cout<<"The queue is empty!"<<endl;
			pthread_mutex_unlock(&m_lock);
			return false;
		}
		data = m_data[m_front];
		m_front = (m_front+1)%QUEUESIZE;
		pthread_mutex_unlock(&m_lock);
		return true;
	}
	
	bool isFull()
	{
		if((m_rear+1)%QUEUESIZE == m_front)
			return true;
		return false;
	}
	
	bool isEmpty()
	{
		if(m_rear == m_front)
			return true;
		return false;
	}
	
	~ThreadSafeQueue()
	{
		pthread_mutex_destroy(&m_lock);
	}
};



int main(int argc, char* argv[])
{
	ThreadSafeQueue<int> testQueue;
	int out = 0;
	if(!testQueue.DeQueue(out))
		cout<<"DeQueue false!"<<endl;
	else
		cout<<"DeQueue true out="<<out<<endl;
	testQueue.EnQueue(12);
	testQueue.EnQueue(13);
	testQueue.EnQueue(14);
        if(!testQueue.DeQueue(out))
                cout<<"DeQueue false!"<<endl;
        else
                cout<<"DeQueue true out="<<out<<endl;

        if(!testQueue.DeQueue(out))
                cout<<"DeQueue false!"<<endl;
        else
                cout<<"DeQueue true out="<<out<<endl;

        if(!testQueue.DeQueue(out))
                cout<<"DeQueue false!"<<endl;
        else
                cout<<"DeQueue true out="<<out<<endl;

        if(!testQueue.DeQueue(out))
                cout<<"DeQueue false!"<<endl;
        else
                cout<<"DeQueue true out="<<out<<endl;
	return 0;
}

g++ queue.c -lpthread

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 你可以使用 Python 中的 Queue 模块中的 Queue 类来创建一个线程安全的队列。这个类在线程间提供了同步机制,使得你可以在多个线程之间安全地使用一个队列。 下面是一个例子,展示了如何使用 Queue 类来创建一个线程安全的队列: ``` import queue q = queue.Queue() # 在队列中添加一个元素 q.put('a') # 从队列中取出一个元素 item = q.get() print(item) # 输出: 'a' # 在队列中添加多个元素 q.put('b') q.put('c') # 使用循环来从队列中取出多个元素 while not q.empty(): print(q.get()) # 输出: 'b' 'c' ``` 需要注意的是,Queue 类是由 Python 中的 threading 模块提供的,所以你需要先导入这个模块才能使用。 ### 回答2: 线程安全的队列是一种能够在多个线程同时访问时,不会出现数据竞争或数据损坏的数据结构。以下是一个简单的线程安全的队列的实现: ```java public class ThreadSafeQueue<T> { private Queue<T> queue; public ThreadSafeQueue() { queue = new LinkedList<>(); } public synchronized void enqueue(T item) { queue.add(item); notifyAll(); } public synchronized T dequeue() throws InterruptedException { while (queue.isEmpty()) { wait(); } return queue.poll(); } public synchronized boolean isEmpty() { return queue.isEmpty(); } public synchronized int size() { return queue.size(); } } ``` 这个实现使用Java的内置队列数据结构,并使用`synchronized`关键字来确保在多线程环境下的安全访问。`enqueue`方法和`dequeue`方法都用`synchronized`关键字修饰,以确保同时只有一个线程可以对队列进行入队或出队操作。当一个线程调用`enqueue`方法时,它会将元素添加到队列并通过`notifyAll`方法通知其他等待线程。当一个线程调用`dequeue`方法时,如果队列为空,它将进入等待状态,直到有其他线程调用`enqueue`方法并通知它。这样可以避免在空队列上进行出队操作。另外,`isEmpty`和`size`方法也用`synchronized`关键字修饰,以确保在多线程环境下获取正确的队列状态。 这个线程安全的队列的实现保证了多个线程在并发访问时不会出现数据竞争或数据损坏的问题,从而可以安全地用于多线程的应用场景中。 ### 回答3: 线程安全的队列是一种多线程环境下能够保证数据操作安全的数据结构。在实现线程安全队列时,可以考虑使用互斥锁(mutex)或者读写锁(read-write lock)来保护队列的数据结构,以确保在多线程操作时数据的一致性和完整性。 在实现一个线程安全队列时,可以定义一个队列类,并在其内部维护一个数据结构(如数组或链表)来存储数据元素。以下为一个可能的实现: ```python import threading class ThreadSafeQueue: def __init__(self): self.queue = [] self.mutex = threading.Lock() def enqueue(self, item): with self.mutex: self.queue.append(item) def dequeue(self): with self.mutex: if self.is_empty(): return None return self.queue.pop(0) def is_empty(self): return len(self.queue) == 0 def size(self): return len(self.queue) ``` 在上述代码中,使用了互斥锁`mutex`来保证在队列的入队和出队操作时只有一个线程可以进行操作。通过`with self.mutex`语句块包裹队列操作,可以确保在操作期间其他线程无法同时访问队列,从而保证队列操作的原子性。 这里的队列是使用数组实现的,`enqueue()`方法通过`append()`将元素添加到队列的末尾,`dequeue()`方法使用`pop(0)`取出队列的头部元素并返回。`is_empty()`和`size()`方法分别返回队列是否为空和队列的大小。 通过以上的实现,可以确保在多线程环境下,对队列的操作和数据的访问是互不干扰的,从而保证了线程安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值