U1 MT_Thread::PauseThreadSync(){ PauseThread(true); while(!m_bHasPause){ Sleep(0); } return true; }
bool MT_Thread::StartThread(){ //如果已經启动则返回 if(m_hHandle !=NULL){ PauseThread(false); Resume(); return true; } //不暂停 m_bPause = false; //不退出循环调用 m_bExit = false; //开始线程 m_hHandle = (HANDLE)_beginthreadex(NULL,0,MT_Thread::Run,(void*)this,0,&m_iThreadID); //如果线程创建失败 if(m_hHandle==NULL) return false; return true; }
bool CAVISplitter::Suspend() { // the parser cannot set the seek params until the pushing // thread is stopped ThreadMsg msg = m_thread_state; bool bShouldRestart = false; if (msg == tm_start) { BeginFlush(); if (m_threadid != GetCurrentThreadId()) { bShouldRestart = true; PauseThread(); } EndFlush(); } return bShouldRestart; }
HRESULT CPullPin::Seek(REFERENCE_TIME tStart, REFERENCE_TIME tStop) { CAutoLock lock(&m_AccessLock); ThreadMsg AtStart = m_State; if(AtStart == TM_Start) { BeginFlush(); PauseThread(); EndFlush(); } m_tStart = tStart; m_tStop = tStop; HRESULT hr = S_OK; if(AtStart == TM_Start) { hr = StartThread(); } return hr; }
DWORD WINAPI Sampler::AsyncUpdate(LPVOID lpParam) { Sampler& sampler = *(Sampler*)(lpParam); std::vector<std::pair<HANDLE, ThreadEntry*>> openThreads; openThreads.reserve(sampler.targetThreads.size()); for (auto entryIterator = sampler.targetThreads.begin() ; entryIterator != sampler.targetThreads.end() ; ++entryIterator) { ThreadEntry* entry = *entryIterator; DWORD threadID = entry->description.threadID; BRO_VERIFY(threadID != GetCurrentThreadId(), "It's a bad idea to sample specified thread! Deadlock will occur!", continue); HANDLE hThread = GetThreadHandleByThreadID(threadID); if (hThread == 0) continue; openThreads.push_back(std::make_pair(hThread, entry)); } if (openThreads.empty()) return 0; CallStackBuffer buffer; CONTEXT context; while ( sampler.finishEvent.WaitForEvent(sampler.intervalMicroSeconds) ) { // Check whether we are inside sampling scope for (auto entry = openThreads.cbegin() ; entry != openThreads.cend() ; ++entry) { HANDLE handle = entry->first; const ThreadEntry* thread = entry->second; if (!thread->storage.isSampling) continue; uint count = 0; if (PauseThread(handle)) { // Check scope again because it is possible to leave sampling scope while trying to suspend main thread if (thread->storage.isSampling && RetrieveThreadContext(handle, context)) { count = sampler.symEngine.GetCallstack(handle, context, buffer); } ContinueThread(handle); } if (count > 0) { sampler.callstacks.push_back(CallStack(buffer.begin(), buffer.begin() + count)); } } } for (auto entry = openThreads.begin() ; entry != openThreads.end() ; ++entry) ReleaseThreadHandle(entry->first); return 0; }