Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 108007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:50:38+00:00 2026-05-11T01:50:38+00:00

I have a question concerning this code which I want to run on QNX:

  • 0

I have a question concerning this code which I want to run on QNX:

class ConcreteThread : public Thread { public:     ConcreteThread(int test)     {         testNumber = test;     }      void *start_routine()      {          for(int i = 0; i < 10; i++)         {             sleep(1);             cout << testNumber << endl;         }        }  private:     int testNumber; };     class Thread  { public:     Thread(){};      int Create()     {         pthread_t m_id;         return pthread_create(&m_id, NULL, &(this->start_routine_trampoline), this);     }  protected:     virtual void *start_routine() = 0;  private:      static void *start_routine_trampoline(void *p)     {         Thread *pThis = (Thread *)p;         return pThis->start_routine();     } }; 

Now, when I run this code without the sleep in *start_routine, it will simply print the number 10 times, before continuing on to the next line of code (sequential instead of parallel). However, when I use a sleep like in the code, it doesn’t print any numbers at all and simply goes on to the next line of code. Why doesn’t sleep work and how can I make a thread like this work, instead of running sequential?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:50:39+00:00Added an answer on May 11, 2026 at 1:50 am

    Note 1: If you only have 1 processor the code can only be done sequentially no matter how many threads you create. Each thread is given a slice of processor time before it is swapped out for the next threads.

    Note 2: If the main thread exits pthreads will kill all child threads before they have a chance to execute.

    Now to answer you questions:

    Without the sleep. The thread once started has enough time in the single slice it was given to execute the loop 10 times completely.

    With the sleep: Your worker thread is going to sleep for a full second. So your main thread has time to do a lot of work. If the main thread exits in this time the worker will be killed.

    I would make the following changes:

    //  Remove the Create() method //  Put thread creation in the constructor. //  Make the thread variable part of the object  pthread_t m_id;  Thread() {     if (pthread_create(&m_id, NULL, &(this->start_routine_trampoline), this) != 0)     {         throw std::runtime_error('Thread was not created');     } }  // Make sure the destructor waits for the thread to exit. ~Thread() {     pthread_join(m_id); } 

    If you go and look at boost threading library. you will find that all the little mistakes like this have already been taken care of; Thus making threading easier to use.

    Also note. That using a static may work but it is non portable. This is because pthread’s is a C library and is thus expecting a function pointer with a C ABI. You are just getting lucky for your platform here. You need to define this as a function and declare the ABI by using extern ‘C’

    // This needs to be a standard function with C Interface. extern 'C' void *start_routine_trampoline(void *p) { } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question concerning subtypes of built-in types and their constructors. I want
I have a question concerning how the this pointer is treated in a nested
I have a question concerning page speed and code optimization. I have a page
I have question concerning a function I created. I would like to show the
I have a question concerning Core Data and how, if at all, Entities get
I have a question concerning number conversion i JS. I have a number like
I have a simple question concerning Javascript. I am trying to print in a
I have a strange question concerning subroutines: As I'm creating a minimal language and
I have a really basic question concerning unix timestamp and mysql date. I'm trying
I have a question about the singleton pattern. I saw two cases concerning the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.