including unique_ptr in a threadsafe_queue
Sep 10, 2017 at 1:43pm UTC
I'm trying to construct a small queue that only pushes and pops unique pointers.
I keep getting the following error:
error: no matching function for call to ‘threadsafe_queue<std::unique_ptr<tcp_session> >::push_unique(std::remove_reference<std::unique_ptr<tcp_session>&>::type)’
I've tried the following combinations of calls to push_unique:
1 2
// q.push_unique(tcp_sess);
q.push_unique(std::move(tcp_sess));
but both function calls result in the same error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <queue>
#include <memory>
#include <mutex>
#include <condition_variable>
class tcp_session { // dummy session
private :
int socket;
public :
void set_x(int i) { socket = i;}
int get_x() { return socket;}
};
template <typename T>
class threadsafe_queue {
private :
mutable std::mutex mut;
std::queue<std::unique_ptr<T>> unique_ptr_q;
std::condition_variable cv;
public :
threadsafe_queue() {}
threadsafe_queue(const threadsafe_queue&) = delete ;
threadsafe_queue& operator =(const threadsafe_queue&) = delete ;
//void push_unique(std::unique_ptr<T> ptr) {
void push_unique(std::unique_ptr<T>&& ptr) {
std::lock_guard<std::mutex> lk(mut);
unique_ptr_q.push(std::move<ptr>);
cv.notify_one();
}
std::unique_ptr<T> pop_unique() {
std::unique_lock<std::mutex> lk(mut);
cv.wait(lk,[this ]{return !unique_ptr_q.empty();});
auto res = std::make_unique<T>(unique_ptr_q.front()) ;
unique_ptr_q.pop();
return res;
}
bool empty() const {
std::lock_guard<std::mutex> lk(mut);
return unique_ptr_q.empty();
}
};
int main() {
std::unique_ptr<tcp_session> tcp_sess;
threadsafe_queue<std::unique_ptr<tcp_session>> q;
tcp_sess->set_x(10);
// q.push_unique(tcp_sess);
q.push_unique(std::move(tcp_sess));
auto tcp_sess2 = q.pop_unique();
std::cout << "x " << tcp_sess2->get_x() "\n" ;
return 0;
}
Can anybody suggest how to modify q.push_unique(std::move(tcp_sess)); or does the function declaration/definition need to change?
Sep 10, 2017 at 2:48pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <queue>
#include <memory>
#include <mutex>
#include <condition_variable>
#include <iostream> // **************
class tcp_session { // dummy session
private :
int socket;
public :
void set_x( int i ) { socket = i; }
int get_x() { return socket; }
};
template <typename T>
class threadsafe_queue {
private :
mutable std::mutex mut;
std::queue<std::unique_ptr<T>> unique_ptr_q;
std::condition_variable cv;
public :
threadsafe_queue() {}
threadsafe_queue( const threadsafe_queue& ) = delete ;
threadsafe_queue& operator =( const threadsafe_queue& ) = delete ;
//void push_unique(std::unique_ptr<T> ptr) {
void push_unique( std::unique_ptr<T>&& ptr ) {
std::lock_guard<std::mutex> lk( mut );
unique_ptr_q.push( std::move( ptr ) ); // **************
cv.notify_one();
}
std::unique_ptr<T> pop_unique() {
std::unique_lock<std::mutex> lk( mut );
cv.wait( lk, [this ] {return !unique_ptr_q.empty(); } );
auto res = std::move( unique_ptr_q.front() ) ; // **************
unique_ptr_q.pop();
return res;
}
bool empty() const {
std::lock_guard<std::mutex> lk( mut );
return unique_ptr_q.empty();
}
};
int main() {
std::unique_ptr<tcp_session> tcp_sess = std::make_unique<tcp_session>() ; // **************
threadsafe_queue<tcp_session> q; // **************
tcp_sess->set_x(10);
// q.push_unique(tcp_sess);
q.push_unique( std::move( tcp_sess ) );
auto tcp_sess2 = q.pop_unique();
std::cout << "x " << tcp_sess2->get_x() << "\n" ; // **************
return 0;
}
http://coliru.stacked-crooked.com/a/c5022221a3f74126
Sep 10, 2017 at 2:59pm UTC
many thanks
Sep 10, 2017 at 3:28pm UTC
I have another query. When I run the example you posted, it works fine, but when I tried to integrate the above into the project I have, I came up with the following error:
// error: invalid application of ‘sizeof’ to incomplete type ‘tcp_session'
when using either of the following 2 lines:
1 2
auto tcp_sess = tcp_sess_q.pop_unique();
//std::unique_ptr<tcp_session> tcp_sess = std::move(tcp_sess_q.pop_unique()); // blocks using a conditional variable with a unique_lock
Any ideas why ?
Sep 10, 2017 at 3:46pm UTC
#include the header which contains the definition of class tcp_session
If you had already done that, check the #include guards; make sure that they are correct.
Topic archived. No new replies allowed.