// Return true to let pool to handle the thread: reuse or delete. bool ThreadPool::on_thread_run(ThreadHandle* thread) { // execute one thread with one specified task. // _LogDebug_("ThreadPool", "run thread handle : %d", thread); // Lock the thread attributes. ThreadBase* task = NULL; { // We won't keep this lock during the running of the task. // And the task object will be not delete before it's run() has been completed. AutoLock(thread->lock); task = thread->task; } // if (task) { _LogDebug_("ThreadPoll", "run thread's task: %d", thread->task); return task->run(); } else { _LogDebug_("ThreadPool", "No task to run, send to idle set."); // No task for thread, so just return ture, and according to the reuse, pool will reuse it or delete this thread. return true; } }
ThreadBase *ThreadBase::getThreadById( ThreadId id ) { for( size_t i = 0; i < current_threads.size(); ++i ) { ThreadBase *thread = current_threads[i]; if( pthread_equal( thread->getThreadId(), id ) ) { return thread; } } return NULL; }
bool HapticThreadBase::inHapticThread() { PeriodicThread::ThreadId id = PeriodicThread::getCurrentThreadId(); for( vector< HapticThreadBase *>::iterator i = threads.begin(); i != threads.end(); ++i ) { ThreadBase *thread = dynamic_cast< ThreadBase * >( *i ); if( thread && pthread_equal( thread->getThreadId(), id ) ) return true; } return false; }
void TPool::Init() throw(std::string) { unsigned int curNumber = 0; while (curNumber++ < m_tNumber) { ThreadBase* th = new DefaultThread(this, true); m_workers.push_back(th); th->Start(); } }
void* ThreadBase::runInThread(void *arg) { //派生子线程复写run()方法 //向上转型,ThreadBase指针指向子对象(实现多态) //参数: *arg 为pthread_create()传入的this(即具体的子线程) ThreadBase *pthread = static_cast<ThreadBase*> (arg); pthread->run(); return NULL; }
void TPool::Stop(bool bForce) { std::list<ThreadBase*>::iterator it; for (it = m_workers.begin(); it != m_workers.end(); ) { ThreadBase* ptr = *it; ptr->Cancel(); it = m_workers.erase(it); delete ptr; } }
unsigned int __stdcall ThreadBase::ThreadRoutine( void* param ) { ThreadBase *pTB = (ThreadBase *)param; WDBG_THREADSTART(pTB->mName.c_str()); WTRACE("ThreadBase::ThreadRoutine"); // anything not caught in the derived thread will eventually end up here. // this is the bottom of our threads execution stack int aRet = 0; try { aRet = pTB->threadProcess(); } catch (WONCommon::WONException& ex) { WDBG_AH("WONException caught in ThreadRoutine!"); // set error code // WONException may set this to zero if undefined, so reset to -1 in that case pTB->mLastError = (ex.GetCode() != 0 ? ex.GetCode() : -1); ex.GetStream() << "Exception caught in Thread main. Thread terminated!"; // Set the exception event if needed if (pTB->hExceptionNotify) SetEvent(pTB->hExceptionNotify); aRet = pTB->mLastError; } #ifndef _NO_TOPLEVEL_CATCH catch (...) { WDBG_AH("Unhandled exception caught in ThreadRoutine!"); // Error code is unknown, set to (-1) if(! pTB->mLastError) pTB->mLastError = -1; // Log an error to event log WONCommon::EventLog aLog; aLog.Log("Unhandled Exception propogated to Thread main. Thread terminated!"); // Set the exception event if needed if (pTB->hExceptionNotify) SetEvent(pTB->hExceptionNotify); aRet = pTB->mLastError; } #endif WDBG_THREADSTOP; return aRet; }
void * ThreadBase::ThreadMain(void *pvOwner) { ThreadBase *pcOwner = (ThreadBase *)pvOwner; void *pvResult; pcOwner->bThreadRunning = true; if ((pvResult = pcOwner->Initialise()) == NULL) { pvResult = pcOwner->Execute(); } pcOwner->CleanUp(); pcOwner->SignalThreadDead(); return pvResult; }
void * ThreadBase::threadEntry( void * obj ) { ThreadBase * me = (ThreadBase *)obj; me->main(); _LLgLock; _LLg( LogLevel::Debug ) << me->getClassName() << "::threadEntry -" << "Thread: " << me->threadId << " completed: " << ( me->cleanup ? "deleting me" : "no cleanup" ) << endl; _LLgUnLock; if( me->cleanup ) delete me; pthread_exit( 0 ); return( 0 ); }
BEGIN_ZKIT void* ThreadBase::threadFunc(void *arg) { ThreadBase *thread = (ThreadBase *)arg; if (thread == 0) { return 0; // error. } while (!thread->isTerminated()) { if (thread->run() < 0) { printf("\nthread=%d exit because of return value!", thread->getThreadId()); break; } } thread->threadStopped(); printf("\nthread exit: %d", thread->getThreadId()); return 0; }
__stdcall unsigned ThreadBase::execute (void* data) #endif { ThreadBase* thread = reinterpret_cast<ThreadBase*> (data); thread->invoke (); thread->mutex.acquire (); #if defined(GLAY_LIBRARY_PTHREAD) pthread_exit (0); #elif defined(GLAY_SYSTEM_WINDOWS) ::_endthreadex (0); #endif thread->state = STATE_READY; Atomic::barrier (); thread->mutex.release (); return 0; }
int ThreadBase::thread_runner(void* param) { ThreadBase* thread = (ThreadBase*)param; thread->main(); return 0; }
extern "C" void* thread_start_func(void* th_v){ ThreadBase* th = (ThreadBase*) th_v; th->idle_loop(); return 0; }