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; }
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(); }
TEST(ThreadedSerializer, SerializableObject) { ThreadedSerializer<NDC> ser; ser.verbose_ = true; ThreadPtr thread = ser.launch(); // Run in a different thread. string dir = "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 << "/ndc" << setw(3) << setfill('0') << i; ser.push(NDC(i), oss.str()); } ser.quit(); // Finish serializing everything in your queue and shut down your thread. thread->join(); cout << "Done." << endl; }
void TimerMonitor::waitForShutdown() { ThreadPtr thread; { AutoRecursiveLock lock(mLock); thread = mThread; mShouldShutdown = true; wakeUp(); } if (!thread) return; thread->join(); { AutoRecursiveLock lock(mLock); mThread.reset(); } }
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; }