void testThreadsRunningAndTerminating() { // ONE stopped, TWO stopped IThread *one = new ThreadSimple(); IThread *two = new ThreadSimple(); assert(one->getId() == 0); assert(!one->isActive()); assert(two->getId() == 0); assert(!two->isActive()); assert(ThreadHolder::getActiveThreads() == 0); // ONE started, TWO started one->start(); two->start(); assert(one->getId() != 0); assert(one->isActive()); assert(two->getId() != 0); assert(two->isActive()); assert(ThreadHolder::getActiveThreads() == 2); Sleep(USUAL_DELAY); // ONE stopped, TWO started one->terminate(); assert(one->getId() == 0); assert(!one->isActive()); assert(two->getId() != 0); assert(two->isActive()); assert(ThreadHolder::getActiveThreads() == 1); Sleep(USUAL_DELAY); // ONE started, TWO started one->start(); assert(one->getId() != 0); assert(one->isActive()); assert(two->getId() != 0); assert(two->isActive()); assert(ThreadHolder::getActiveThreads() == 2); Sleep(USUAL_DELAY); // ONE stopped, TWO stopped one->terminate(); two->terminate(); assert(one->getId() == 0); assert(!one->isActive()); assert(two->getId() == 0); assert(!two->isActive()); assert(ThreadHolder::getActiveThreads() == 0); delete one; delete two; }
TEST(IThread, Test1) { class ThreadHandle: public IThread { public: ThreadHandle():num(1) {} virtual ~ThreadHandle() {} private: public: virtual void run() { ASSERT_EQ(num, 1u); num = 2; } public: size_t num; }; { ThreadHandle thr; thr.start(); thr.join(); ASSERT_EQ(thr.num, 2u); } { IThread* ptr = new ThreadHandle(); ptr->start(); ptr->join(); delete ptr; } }
void startStatThread() { if(startStat) { curl_global_init(CURL_GLOBAL_ALL); //nlinfo("startStatThread"); CStatThread *statThread = new CStatThread(); IThread *thread = IThread::create (statThread); nlassert (thread != NULL); thread->start (); } }
void startWebigNotificationThread() { static bool startedWebigNotificationThread = false; if(!startedWebigNotificationThread) { curl_global_init(CURL_GLOBAL_ALL); //nlinfo("startStatThread"); CWebigNotificationThread *webigThread = new CWebigNotificationThread(); IThread *thread = IThread::create (webigThread); nlassert (thread != NULL); thread->start (); startedWebigNotificationThread = true; } }
// starts a thread static void* start_thread(void* param) { KLKASSERT(param != NULL); IThread* thread = static_cast<IThread*>(param); if (thread == NULL) { KLKASSERT(thread != NULL); return NULL; } // ignore several signals in threads sigset_t sigset; sigemptyset(&sigset); sigaddset(&sigset, SIGTERM); sigaddset(&sigset, SIGINT); sigaddset(&sigset, SIGPIPE); pthread_sigmask(SIG_SETMASK, &sigset, NULL); try { // all threads should be able to communicate with db // check thread safety BOOST_ASSERT(mysql_thread_safe() == 1); if (mysql_thread_init() != 0) { throw Exception(__FILE__, __LINE__, "Error in mysql_thread_init()"); } thread->start(); } catch(const std::exception& err) { klk_log(KLKLOG_ERROR, "Got an exception during thread execution: %s", err.what()); } catch(...) { klk_log(KLKLOG_ERROR, "Got an unknown exception during thread execution"); } mysql_thread_end(); return NULL; }