简体   繁体   中英

How to verify if a pthread function began execution

Example:

void start(void)
{
   pthread_create(&threadID, Null, run_thread_function,arguments);

   //is there a way to ensure if the run_thread_function(basically new thread) started   
   //execution before returning from this(start) function    

}

Check the return code.

if ((retcode = pthread_create(&threadID, Null, run_thread_function,arguments)) != 0)
{
   //something went wrong
}

Pass a synchro object, (condvar, event or semaphore), as part of the arguments. Wait on it after calling pthread_create(). In the thread, signal it in the first line, (or after the thread has performed its init stuff, if that's what you are trying to achieve).

Check the return code of the pthread_create function for error.

Update some shared variable and test it from another thread. Remember to use synchronization primitives, like mutex when updating the shared variable.

Or to make simple test, print some message with the thread id, or some other kind of identifier.

在C ++ 11中,直到新线程启动后,才会通过std::thread类型的对象创建线程。

Use pthread_barrier_wait if you want to know for certain that your new thread has begun.

Though, I really question code that cares deeply about this. Seems like you're asking for race conditions.

Note that I should be checking for return values all over the place and I'm not for the sake of succinctness clarity. sigh

#include <iostream>
#include <pthread.h>
#include <unistd.h>

void *newthread(void *vbarrier)
{
   pthread_barrier_t *barrier = static_cast<pthread_barrier_t *>(vbarrier);
   sleep(2);
   int err = pthread_barrier_wait(barrier);
   if ((err != 0) && (err != PTHREAD_BARRIER_SERIAL_THREAD)) {
      ::std::cerr << "Aiee! pthread_barrier_wait returned some sort of error!\n";
   } else {
      ::std::cerr << "I am the new thread!\n";
   }
   return 0;
}

int main()
{
   pthread_barrier_t barrier;
   pthread_barrier_init(&barrier, NULL, 2);
   pthread_t other;
   pthread_create(&other, NULL, newthread, &barrier);
   pthread_barrier_wait(&barrier);
   ::std::cerr << "Both I and the new thread reached the barrier.\n";
   pthread_join(other, NULL);
   return 0;
}

C++11 doesn't have barriers. But barriers can easily be simulated, to an extent, using condition variables:

#include <thread>
#include <condition_variable>
#include <iostream>
#include <unistd.h>

void runthread(::std::mutex &m, ::std::condition_variable &v, bool &started)
{
   sleep(2);
   {
      ::std::unique_lock< ::std::mutex> lock(m);
      started = true;
      v.notify_one();
   }
   ::std::cerr << "I am the new thread!\n";
}

int main()
{
   ::std::mutex m;
   ::std::condition_variable v;
   bool started = false;
   ::std::thread newthread(runthread, ::std::ref(m), ::std::ref(v), ::std::ref(started));
   {
      ::std::unique_lock< ::std::mutex> lock(m);
      while (!started) {
         v.wait(lock);
      }
   }
   ::std::cerr << "Both I and the new thread are running.\n";
   newthread.join();
   return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM