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
  • 0 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

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The answer is in a simple class: TypeConverter. (and yes,… May 12, 2026 at 12:30 pm
  • Editorial Team
    Editorial Team added an answer You can use jquery-json library `$('#jsonmodel').html($.toJSON(data));` May 12, 2026 at 12:30 pm
  • Editorial Team
    Editorial Team added an answer hash.each do |k,v| date = k.split('_')[1] Entry.create(:date => date, :price… May 12, 2026 at 12:30 pm

Related Questions

i want to parse VB6 code via Regex. However being new to Regex I
Duplicate : Function overloading by return type? Maybe this is a very silly question
I want to get the number of Registrations back from a time period (say
I have a common code of serializing a class object in my 3-4 methods

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.