added move support to pop queque
This commit is contained in:
parent
0cf139bf8a
commit
65e8349c60
@ -75,7 +75,7 @@ public:
|
|||||||
if (!item_pushed_cond_.wait_until(ul, clock::now() + timeout, [this]() { return !this->q_.empty(); }))
|
if (!item_pushed_cond_.wait_until(ul, clock::now() + timeout, [this]() { return !this->q_.empty(); }))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
item = q_.front();
|
item = std::move(q_.front());
|
||||||
q_.pop();
|
q_.pop();
|
||||||
if (q_.size() >= max_size_ - 1)
|
if (q_.size() >= max_size_ - 1)
|
||||||
{
|
{
|
||||||
|
57
src/test.cpp
57
src/test.cpp
@ -15,25 +15,61 @@
|
|||||||
std::atomic<uint64_t> push_count, pop_count;
|
std::atomic<uint64_t> push_count, pop_count;
|
||||||
std::atomic<bool> active;
|
std::atomic<bool> active;
|
||||||
|
|
||||||
using Q = c11log::details::blocking_queue<std::string>;
|
|
||||||
|
|
||||||
|
class A
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
A()
|
||||||
|
{
|
||||||
|
std::cout << "Regular ctor\n";
|
||||||
|
}
|
||||||
|
A(const A&)
|
||||||
|
{
|
||||||
|
std::cout << "Copy ctor\n";
|
||||||
|
}
|
||||||
|
A& operator=(const A&)
|
||||||
|
{
|
||||||
|
std::cout << "operator=\n";
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
A& operator=(A&&)
|
||||||
|
{
|
||||||
|
std::cout << "operator=&&\n";
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
A(A&& a)
|
||||||
|
{
|
||||||
|
std::cout << "Move ctor\n";
|
||||||
|
}
|
||||||
|
~A()
|
||||||
|
{
|
||||||
|
//std::cout << "Dtor\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
using std::string;
|
||||||
using std::chrono::seconds;
|
using std::chrono::seconds;
|
||||||
|
using Q = c11log::details::blocking_queue<string>;
|
||||||
|
|
||||||
void pusher(Q* q)
|
void pusher(Q* q)
|
||||||
{
|
{
|
||||||
|
string a = "Hello";
|
||||||
while(active)
|
while(active)
|
||||||
{
|
{
|
||||||
//if(q->push("Hello", seconds(10)))
|
q->push(a);
|
||||||
q->push("hello");
|
|
||||||
++push_count;
|
++push_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
void popper(Q* q)
|
void popper(Q* q)
|
||||||
{
|
{
|
||||||
std::string output;
|
string output;
|
||||||
while(active)
|
while(active)
|
||||||
{
|
{
|
||||||
//if(q->pop(output, seconds(10)))
|
|
||||||
q->pop(output);
|
q->pop(output);
|
||||||
++pop_count;
|
++pop_count;
|
||||||
}
|
}
|
||||||
@ -44,6 +80,13 @@ void testq(int size, int pushers, int poppers)
|
|||||||
|
|
||||||
active = true;
|
active = true;
|
||||||
Q q{static_cast<Q::size_type>(size)};
|
Q q{static_cast<Q::size_type>(size)};
|
||||||
|
/*
|
||||||
|
A a;
|
||||||
|
q.push(a);
|
||||||
|
std::cout << "Befor pop..\n";
|
||||||
|
q.pop(a);
|
||||||
|
return;
|
||||||
|
*/
|
||||||
|
|
||||||
for(int i = 0; i < poppers; i++)
|
for(int i = 0; i < poppers; i++)
|
||||||
new std::thread(std::bind(popper, &q));
|
new std::thread(std::bind(popper, &q));
|
||||||
@ -69,10 +112,12 @@ void testq(int size, int pushers, int poppers)
|
|||||||
cout << "---------------------------------------------------------------------" << endl;
|
cout << "---------------------------------------------------------------------" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
|
||||||
if(argc !=4)
|
if(argc !=4)
|
||||||
{
|
{
|
||||||
std::cerr << "Usage: " << argv[0] << " qsize, pushers, poppers" << std::endl;
|
std::cerr << "Usage: " << argv[0] << " qsize, pushers, poppers" << std::endl;
|
||||||
|
Loading…
Reference in New Issue
Block a user