c++多线程队列 类对象
来源:互联网 发布:淘宝 海外 上传身份证 编辑:程序博客网 时间:2024/06/05 19:33
参考:http://blog.csdn.net/mymodian9612/article/details/53608084
个人感觉:队列里不能存指针,存指针
队列里不能存指针,队列中的对象也不能存指针
threadsafe_queue =new threadsafe_queue2<Point>();
#include <thread>
class Point
{
public:
Point() {
}
Point(int x,int y)//实现setPoint函数
{
xPos = x;
yPos = y;
}
void printPoint() //实现printPoint函数
{
cout << "x = " << xPos<< endl;
cout << "y = " << yPos<< endl;
}
int xPos;
int yPos;
};
threadsafe_queue2<Point> threadsafe_queue;
void t1() //普通的函数,用来执行线程
{
int i;
i = 0;
while (1) {
i++;
Point *point;
point= new Point(i, i);
threadsafe_queue.push(move(*point));
}
}
void t2()
{
while (1) {
//Point pInt = threadsafe_queue.wait_and_pop();
//int *pInt = *aaa->get();
std::shared_ptr<Point> aaa =threadsafe_queue.wait_and_pop();
Point *pInt = aaa.get();
cout << "Please enter " << pInt->xPos<< " b \"" << pInt->xPos<< endl;
}
}
int main()
{
upload_file();
thread th1(t1); //实例化一个线程对象th1,使用函数t1构造,然后该线程就开始执行了(t1())
thread th2(t2);
#ifndef __THREADSAFE_QUEUE2_H_
#define __THREADSAFE_QUEUE2_H_
#include <mutex>
#include <memory>
#include <functional>
#include <condition_variable>
template <typename T>
class threadsafe_queue2
{
public:
threadsafe_queue2()
: head(new node)
, tail(head.get())
{}
threadsafe_queue2(const threadsafe_queue2&) =delete;
threadsafe_queue2 operator=(const threadsafe_queue2&) =delete;
~threadsafe_queue2() = default;
std::shared_ptr<T> try_pop()
{
std::unique_lock<std::mutex> ulkh(head_mut);
if (head.get() == get_tail())
return nullptr;
auto old_head = std::move(head);
head = std::move(old_head->next);
return old_head->data;
}
//此版本存在最后一个元素加入后,可能一直阻塞在wait中,下面一个函数不存在此问题
/*std::shared_ptr<T> wait_and_pop()
{
std::unique_lock<std::mutex> ulkh(head_mut);
data_cond.wait(ulkh, [&]()
{
node *old_tail=get_tail();
return head.get() != old_tail;
});
auto old_head = std::move(head);
head = std::move(old_head->next);
return old_head->data;
}*/
std::shared_ptr<T> wait_and_pop()
{
std::unique_lock<std::mutex> ulkh(head_mut);
{
std::unique_lock<std::mutex> ulkt(tail_mut);
data_cond.wait(ulkt, [&]()
{
return head.get() != tail;
});
}
auto old_head = std::move(head);
head = std::move(old_head->next);
return old_head->data;
}
void push(T &&t)
{
std::shared_ptr<T> new_data(std::make_shared<T>(std::forward<T>(t)));
std::unique_ptr<node> new_tail(new node);
node *p = new_tail.get();
{
std::unique_lock<std::mutex> ulkt(tail_mut);
tail->data = new_data;
tail->next = std::move(new_tail);
tail = p;
}
data_cond.notify_one();
}
private:
struct node
{
std::shared_ptr<T> data;
std::unique_ptr<node> next;
};
std::mutex head_mut;
std::mutex tail_mut;
std::unique_ptr<node> head;
node *tail;
std::condition_variable data_cond;
private:
node* get_tail()
{
std::unique_lock<std::mutex> ulkt(tail_mut);
return tail;
}
};
template <typename T>
class threadsafe_queue2<T*>
{
public:
threadsafe_queue2()
: head(new node)
, tail(head)
{}
threadsafe_queue2(const threadsafe_queue2&) =delete;
threadsafe_queue2 operator=(const threadsafe_queue2&) =delete;
~threadsafe_queue2()
{
node *pre;
for (; head != tail;)
{
pre = head;
head = head->next;
delete pre;
}
delete tail;
}
T* try_pop()
{
node *old_head = nullptr;
{
std::unique_lock<std::mutex> ulkh(head_mut);
if (head == get_tail())
return nullptr;
old_head = head;
head = head->next;
}
T *data = old_head->data;
delete old_head;
return data;
}
//此版本存在最后一个元素加入后,可能一直阻塞在wait中,下面一个函数不存在此问题
/*T* wait_and_pop()
{
node *old_head = nullptr;
{
std::unique_lock<std::mutex> ulkh(head_mut);
data_cond.wait(ulkh, [&]()
{
node *old_tail = get_tail();
return head != old_tail;
});
old_head = head;
head = head->next;
}
T *data = old_head->data;
delete old_head;
return data;
}*/
T* wait_and_pop(){
node *old_head = nullptr;
{
std::unique_lock<std::mutex> ulkh(head_mut);
{
std::unique_lock<std::mutex> ulkt(tail_mut);
data_cond.wait(ulkt, [&]()
{
return head != tail;
});
}
old_head = head;
head = head->next;
}
T *data = old_head->data;
delete old_head;
return data;
}
void push(T *t)
{
node *new_tail = new node;
{
std::unique_lock<std::mutex> ulkt(tail_mut);
tail->data = t;
tail->next = new_tail;
tail = new_tail;
}
data_cond.notify_one();
}
private:
struct node
{
T *data;
node *next;
};
std::mutex head_mut;
std::mutex tail_mut;
node *head;
node *tail;
std::condition_variable data_cond;
private:
node* get_tail()
{
std::unique_lock<std::mutex> ulkt(tail_mut);
return tail;
}
};
#endif
下面是调用指针的例子:不能拿到当初存的值
#include <thread>
class Point
{
public:
Point() {
}
Point(int x,int y)//实现setPoint函数
{
xPos = x;
yPos = y;
}
void printPoint() //实现printPoint函数
{
cout << "x = " << xPos<< endl;
cout << "y = " << yPos<< endl;
}
int xPos;
int yPos;
};
threadsafe_queue2<int*> *threadsafe_queue;
void t1() //普通的函数,用来执行线程
{
int i;
i = 0;
while (1) {
i++;
Point *point;
point= new Point(i, i);
//threadsafe_queue->push(move(*point));
threadsafe_queue->push(&i);
}
}
void t2()
{
while (1) {
int *pInt = threadsafe_queue->wait_and_pop();
cout << "Please enter " << pInt<< " 1 \"" << &pInt<< " 2 \"" << pInt<< endl;
//int *pInt = *aaa->get();
/*std::shared_ptr<Point> aaa =threadsafe_queue->wait_and_pop();
Point *pInt = aaa.get();*/
//cout << "Please enter " << pInt->xPos << " b \"" << pInt->xPos << endl;
}
}
int main()
{
upload_file();
threadsafe_queue =new threadsafe_queue2<int*>();
thread th1(t1); //实例化一个线程对象th1,使用函数t1构造,然后该线程就开始执行了(t1())
thread th2(t2);
- c++多线程队列 类对象
- 多线程---队列
- 多线程队列
- linux多线程编程(C):信号量实现的线程安全队列
- linux多线程编程(C):互斥量实现的线程安全队列
- java多线程模拟M/M/C(马科洛夫队列)
- c语言实现多线程下的链表队列
- linux c多线程与消息队列实现字符串转化
- c语言数据结构应用-数组队列(无锁队列)在多线程中的使用
- 对象+队列
- 循环队列的面向对象实现(C++)
- 顺序队列类实现(C++)
- 【c++】环形队列类模板
- C++:队列的类实现
- C 队列
- 队列(C#)
- C 队列
- C+队列
- Wannafly挑战赛4 E 二次剩余
- matplotlib命令与格式:设置折线与点属性
- eclipse启动tomcat正常 ,没有出现错误,但是访问404
- 腾讯分布式机器学习系统sabaton和AnyEmbedding介绍
- 由电子领域延伸的技术视野
- c++多线程队列 类对象
- uboot启动
- Spring Boot启动依赖分析
- 初探GF-Complete(伽罗瓦运算库)
- gradle插件版本和gradle版本之间的对应
- 5.10 面向抽象编程(略)
- linux的基本命令
- FreeRtos 文件结构
- 指针的基本用法