TEST(LinkedBlockingQueue, MixedTest) {
		BlockingQueue *queue = new LinkedBlockingQueue(5);
		SuperObject *dummy1 = new DummyObject(1);
		ASSERT_TRUE(queue->offer(dummy1));
		ASSERT_TRUE(queue->offer(new DummyObject(2)));
		ASSERT_TRUE(queue->offer(new DummyObject(3)));
		ASSERT_TRUE(queue->offer(new DummyObject(4)));
		ASSERT_TRUE(queue->add(new DummyObject(5)));
		ASSERT_EQ(5, queue->size());
		SuperObject *tmp1 = queue->take();
		ASSERT_EQ(dummy1, tmp1);
		ASSERT_EQ(4, queue->size());
		delete tmp1;
		SuperObject *tmp2 = queue->peek();
		ASSERT_EQ(4, queue->size());
		ASSERT_TRUE(tmp2 != NULL);

		delete queue->take();
		ASSERT_EQ(3, queue->size());
		delete queue->take();
		ASSERT_EQ(2, queue->size());
		delete queue->take();
		ASSERT_EQ(1, queue->size());
		SuperObject *tmp3 = queue->poll(3);
		ASSERT_TRUE(tmp3 != NULL);
		if (tmp3 != NULL) {
			delete tmp3;
		}
		ASSERT_EQ(0, queue->size());
		ASSERT_EQ(NULL, queue->poll(3));
		ASSERT_EQ(NULL, queue->peek());

		delete queue;
	}
	TEST(LinkedBlockingQueue, OrderingTest) {
		BlockingQueue *queue = new LinkedBlockingQueue();
		ASSERT_TRUE(queue->offer(new DummyObject(1)));
		ASSERT_TRUE(queue->offer(new DummyObject(2)));
		ASSERT_TRUE(queue->offer(new DummyObject(3)));
		DummyObject *ret1 = dynamic_cast<DummyObject *>(queue->take());
		ASSERT_TRUE(ret1 != NULL);
		ASSERT_EQ(1, ret1->getData());
		delete ret1;
		DummyObject *ret2 = dynamic_cast<DummyObject *>(queue->take());
		ASSERT_TRUE(ret2 != NULL);
		ASSERT_EQ(2, ret2->getData());
		delete ret2;

		ASSERT_TRUE(queue->offer(new DummyObject(4)));

		DummyObject *ret3 = dynamic_cast<DummyObject *>(queue->take());
		ASSERT_TRUE(ret3 != NULL);
		ASSERT_EQ(3, ret3->getData());
		delete ret3;

		DummyObject *ret4 = dynamic_cast<DummyObject *>(queue->take());
		ASSERT_TRUE(ret4 != NULL);
		ASSERT_EQ(4, ret4->getData());
		delete ret4;
		ASSERT_EQ(0, queue->size());
		delete queue;
	}
Ejemplo n.º 3
0
void consumer_take(int id, BlockingQueue & queue) {
    for (int i = 0; i < 50; ++i) {
        int data = queue.take();
        cout << "Consumer " << id << " take " << data << endl;
        this_thread::sleep_for(chrono::milliseconds(250));
    }
}
Ejemplo n.º 4
0
void* thread_func(void *arg)
{
	while(running)
	{
		Functor task = taskQueue.take();
		task();
	}
	pthread_exit(NULL);
}
Ejemplo n.º 5
0
int main(int argc, char *argv[])
{
    BlockingQueue<std::string> queue;

    queue.put("Hello");
    queue.put("World");

    std::cout << queue.take() << std::endl;
    std::cout << queue.take() << std::endl;

    return 0;
}