本文整理汇总了C++中ThreadPtr::join方法的典型用法代码示例。如果您正苦于以下问题:C++ ThreadPtr::join方法的具体用法?C++ ThreadPtr::join怎么用?C++ ThreadPtr::join使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ThreadPtr
的用法示例。
在下文中一共展示了ThreadPtr::join方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: setw
TEST(ThreadedSerializer, CustomSerializationFunction)
{
ThreadedSerializer<std::string, CustomSerializationFunction> ser;
ser.verbose_ = true;
ThreadPtr thread = ser.launch(); // Run in a different thread.
string dir = "custom_threaded_serializer_test";
if(!bfs::exists(dir))
bfs::create_directory(dir);
int num = 100;
for(int i = 0; i < num; ++i) {
ostringstream oss;
oss << dir << "/string" << setw(3) << setfill('0') << i;
string path = oss.str();
oss.str("");
oss << "The number is " << i << endl;
string str = oss.str();
ser.push(str, path);
}
ser.quit(); // Finish serializing everything in your queue and shut down your thread.
thread->join();
cout << "Done." << endl;
}
示例2: SignalDispatcher
SignalDispatcher(const Args&... args)
: _signal_set(_io_service, SIGINT, SIGTERM)
, _thread(new std::thread(std::bind((IoServiceRunFunc)&IoService::run, &_io_service)))
{
std::size_t size = sizeof...(args);
StopHandle res[sizeof...(args)] = {args...};
auto stop_func = [size, res] () {
for (std::uint32_t i = 0; i < size; ++i) {
res[i]();
}
LOG(INFO) << "Stop modules.";
};
_signal_set.async_wait(std::bind(stop_func));
_thread->join();
}
示例3: _start
static void _start(int thread)
{
monitor m;
m.count = thread;
m.sleep = 0;
m.m = new SNMonitor*[thread];
for (int i = 0; i < thread; i++) {
m.m[i] = new SNMonitor();
}
g_monitorTherad = ThreadPtr(new std::thread(std::bind(_monitor, &m)));
g_timerTherad = ThreadPtr(new std::thread(std::bind(_timer, &m)));
g_socketTherad = ThreadPtr(new std::thread(std::bind(_socket, &m)));
static int weight[] = {
-1, -1, -1, -1, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, };
worker_parm *wp = new worker_parm[thread];
for (int i = 0; i < thread; i++) {
wp[i].m = &m;
wp[i].id = i;
if (i < sizeof(weight) / sizeof(weight[0])) {
wp[i].weight = weight[i];
}
else {
wp[i].weight = 0;
}
ThreadPtr pWork(new std::thread(std::bind(_worker, &wp[i])));
g_threads.push_back(pWork);
}
//////////////////////////////////////////////////////////////////////////
// TODO :: 处理退出步骤,这里退出顺序很重要,具体效果还有待测试
// 1: 等待socket线程最先退出来
g_socketTherad->join();
LogInfo("Socket Thread Exit\n");
// 2: 等待定时器和监控线程退出来
g_timerTherad->join();
LogInfo("Timer Thread Exit\n");
g_monitorTherad->join();
LogInfo("Monitor Thread Exit\n");
// 3: 等待工作线程将所有的消息分发完毕再退出来
while (m.sleep != (int)g_threads.size()) {
LogInfo("Wait DispatchMessageQueue\n");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
m.cond.notify_all();
for (auto it = g_threads.begin(); it != g_threads.end(); ++it) {
(*it)->join();
}
LogInfo("Worket Thread Group Exit\n");
// 3 : 释放所有数据
SNServer::Get()->Release();
delete[] wp;
for (int i = 0; i < m.count; ++i) {
delete m.m[i];
}
delete[] m.m;
}