示例#1
0
bool Thread::start()
{
  // Don't do anything if the thread is already starting or running.
  if (m_starting || running())
  {
    waitStarted();

    return running();
  }

  m_starting = true;
  m_finished = false;

  if (!m_joined)
  {
    join();
  }

  m_joined = false;

  if (!m_impl->start())
  {
    m_finished = true;
    m_starting = false;
    m_joined = true;

    return false;
  }
  else
  {
    waitStarted();

    return true;
  }
}
示例#2
0
文件: threads.cpp 项目: gatgui/gcore
bool gcore::Thread::restart(bool waitStart) {
  
  if (mRunning == true) {
    fprintf(stderr, "Thread is already running...\n");
    return false;
  }
  
  if (mProc == 0) {
    fprintf(stderr, "No thread procedure...\n");
    return false;
  }
  
  DWORD dw;
  
  mStarted = false;
  mRunning = true;
  
  mSelf = (ThreadID)CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&_ThreadEntryFunc, this, 0, &dw);
  
  if (mSelf != 0) {
    mSelfId = (void*) GetThreadId((HANDLE)mSelf);
    //mRunning = true;
    if (waitStart) {
      waitStarted();
    }
    return true;
  } else {
    return false;
  }
  
}
void Thread::cancel()
{
  LOGGING_TRACE_CO(IclCoreThread, Thread, threadInfo(), "Begin." << endl);
  waitStarted();
  if (running())
  {
    LOGGING_DEBUG_CO(IclCoreThread, Thread,  threadInfo(), "Still running." << endl);
    m_execute = false;
    m_impl->cancel();
    m_finished = true;
  }
  LOGGING_DEBUG_CO(IclCoreThread, Thread, threadInfo(), "Done." << endl);
}
bool Thread::wait(const icl_core::TimeStamp& until)
{
  bool success = false;

  if (m_joined)
  {
    return true;
  }

  waitStarted();

  if (m_finished)
  {
    success = true;
  }
  else if ((until == icl_core::TimeStamp::maxTime() && m_thread_mutex.lock())
           || m_thread_mutex.lock(until))
  {
    m_thread_mutex.unlock();
  }
  else if (icl_core::TimeStamp::now() < until)
  {
    LOGGING_ERROR_CO(IclCoreThread, Thread, threadInfo(),
                     "Thread is running and we should still wait, but LockMutex() returned unexpected."
                     "The wait function will now block further until the thread is really finished."
                     "But consider that your implementation could have a failure in locking ..." << endl);

    while (icl_core::TimeStamp::now() < until && !m_finished)
    {
      os::sleep(1);
    }
  }

  if (m_finished)
  {
    success = true;
  }

  if (success)
  {
    join();
    return true;
  }
  else
  {
    LOGGING_ERROR_CO(IclCoreThread, Thread, threadInfo(), "Wait not succesful." << endl);
    return false;
  }
}
示例#5
0
文件: threads.cpp 项目: gatgui/gcore
bool gcore::Thread::restart(bool waitStart) {
  
  if (mRunning == true) {
    fprintf(stderr, "Thread already running...\n");
    return false;
  }
  
  if (mProc == 0) {
    fprintf(stderr, "No procedure for thread...\n");
    return false;
  }

  pthread_t thr;
  pthread_attr_t attr;
  
  pthread_attr_init(&attr);
  
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
  
  mStarted = false;
  mRunning = true; // before create thread
  
  bool created = (pthread_create(
    &thr, &attr, &_ThreadEntryFunc, (void*)this) == 0);
  
  pthread_attr_destroy(&attr);
  
  if (created) {
    mSelf = (ThreadID)thr;
    //mRunning = true;
    if (waitStart) {
      waitStarted();
    }
    return true;
  } else {
    return false;
  }
}
/*! Suspends the calling thread until thread is started.
 *  \deprecated Obsolete coding style.
 */
void Thread::WaitStarted() const
{
  waitStarted();
}