Last active
November 12, 2017 05:47
-
-
Save fyzhr/b3c41624fbf8ccb5792ff37d415be621 to your computer and use it in GitHub Desktop.
OccurStarvation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <thread> | |
#include <mutex> | |
#include <iostream> | |
#include "BlockingQueue.h" | |
using BlockQueue::BlockQueue; | |
using std::cout; | |
using std::endl; | |
using std::thread; | |
BlockQueue<int> queue; | |
void test_push(int base) | |
{ | |
for(int i = 0; i < 2; ++i) { // 将2个entity加入阻塞队列 | |
cout << "enqueue" << endl; | |
queue.enqueue(base + i); | |
} | |
} | |
int test_pop() | |
{ | |
int ret = queue.dequeue(); | |
cout << "thread id : " << std::this_thread::get_id() << " return " << ret << endl; | |
return ret; | |
} | |
int main() | |
{ | |
printf("main thread id : %ld\n", std::this_thread::get_id()); | |
thread t2(test_pop); | |
thread t3(test_pop); | |
thread t4(test_pop); | |
thread t5(test_pop); | |
//睡眠2秒开始生产 | |
std::this_thread::sleep_for(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::seconds(2))); | |
thread t1(test_push, 12); | |
t1.join(); | |
t2.join(); | |
t3.join(); | |
t4.join(); | |
t5.join(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment