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


原创粉丝点击