fork download
  1. #include <iostream>
  2. #include <pthread.h>
  3.  
  4. class Example {
  5. public:
  6. Example () : thread_() {
  7. int rcode = pthread_create(&thread_, nullptr, Example::task, nullptr);
  8. if (rcode != 0) {
  9. std::cout << "pthread_create failed. Return code: " << rcode << std::endl;
  10. }
  11. }
  12.  
  13. /* New code below this point. */
  14.  
  15. ~Example () {
  16. int rcode = pthread_join(thread_, nullptr);
  17. if (rcode != 0) {
  18. std::cout << "pthread_join failed. Return code: " << rcode << std::endl;
  19. }
  20. }
  21.  
  22. /* New code above this point. */
  23.  
  24. static void * task (void *) {
  25. std::cout << "Running task." << std::endl;
  26. return nullptr;
  27. }
  28.  
  29. private:
  30. pthread_t thread_;
  31. };
  32.  
  33. int main () {
  34. Example example;
  35. }
  36.  
Success #stdin #stdout 0s 11608KB
stdin
Standard input is empty
stdout
Running task.