本文整理汇总了C++中MessageQueue::push_front方法的典型用法代码示例。如果您正苦于以下问题:C++ MessageQueue::push_front方法的具体用法?C++ MessageQueue::push_front怎么用?C++ MessageQueue::push_front使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MessageQueue
的用法示例。
在下文中一共展示了MessageQueue::push_front方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ut_MessageQueue_simple
/******************************* test functions *******************************/
int32_t ut_MessageQueue_simple(void)
{
TestCaseBegin();
std::mutex mMut;
std::atomic_int mInt;
mInt = 0;
std::atomic_char running;
running = 1;
MessageQueue < std::function < int(void) >> messages;
std::thread t1([&]
{
while(running)
{
const auto f = messages.receive();
if(f) mInt = f();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
);
CHECK(0 == mInt);
messages.push_back([ = ] {return 1; }
);
std::this_thread::sleep_for(std::chrono::milliseconds(20));
CHECK(1 == mInt);
messages.push_back([ = ] {return 2; }
);
std::this_thread::sleep_for(std::chrono::milliseconds(20));
CHECK(2 == mInt);
messages.push_back([ = ] {return 4; }
);
messages.push_front([ = ] {return 3; }
);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
CHECK(3 == mInt);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
CHECK(4 == mInt);
{
std::lock_guard<std::mutex> lg(mMut);
messages.push_back([ = ] {return 5; }
);
messages.push_back([ = ] {return 5; }
);
messages.push_back([ = ] {return 5; }
);
messages.clear_and_push_front([ = ] {return 6; }
);
}
std::this_thread::sleep_for(std::chrono::milliseconds(20));
CHECK(6 == mInt);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
CHECK(6 == mInt);
messages.push_back([ = ] {return 5; }
);
running = 0;
t1.join();
TestCaseEnd();
}
示例2: ut_MessageQueue_complex
int32_t ut_MessageQueue_complex(void)
{
TestCaseBegin();
std::mutex mMut;
std::atomic_int mInt;
mInt = 0;
MessageQueue < std::pair < std::function<int(void)>, bool >> messages;
std::thread t1([&]
{
while(true)
{
const auto f = messages.receive();
if(f.second) break;
if(f.first) mInt = f.first();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
);
CHECK(0 == mInt);
messages.push_back(std::make_pair([ = ] {return 1; }, false));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
CHECK(1 == mInt);
messages.push_back(std::make_pair([ = ] {return 2; }, false));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
CHECK(2 == mInt);
messages.push_back(std::make_pair([ = ] {return 4; }, false));
messages.push_back(std::make_pair([ = ] {return 5; }, false));
messages.push_front(std::make_pair([ = ] {return 3; }, false));
std::this_thread::sleep_for(std::chrono::milliseconds(40));
CHECK(3 == mInt);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(4 == mInt);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(5 == mInt);
{
std::lock_guard<std::mutex> lg(mMut);
messages.push_back(std::make_pair([ = ] {return 4; }, false));
messages.push_back(std::make_pair([ = ] {return 4; }, false));
messages.push_back(std::make_pair([ = ] {return 4; }, false));
messages.clear_and_push_front(std::make_pair([ = ] {return 6; }, false));
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(6 == mInt);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(6 == mInt);
messages.push_front(std::make_pair([ = ] {return 0xff; }, true));
std::this_thread::sleep_for(std::chrono::milliseconds(10));
t1.join();
TestCaseEnd();
}