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 744737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:00:47+00:00 2026-05-14T09:00:47+00:00

I’m working on a graphical application which looks something like this: while (Simulator.simulating) {

  • 0

I’m working on a graphical application which looks something like this:

while (Simulator.simulating)
{
    Simulator.update();
    InputManager.processInput();
    VideoManager.draw();
}

I do this several times a second, and in the vast majority of cases my computation will be taking up 90 – 99% of my processing time. What I would like to do is take out the processInput and draw functions and have each one run independently.

That way, I can have the input thread always checking for input (at a reasonable rate), and the draw thread attempting to redraw at a given frame rate.

The simulator is already (internally) multithreaded and there is no issues with multiple threads writing to the same data (each one processes a segment).

My issue is I’m not sure how I can properly do this. How would I properly initialize my pthread_t and associated pthread_attr_t so that the thread runs without blocking what I’m doing? In other words, how can I create two threads, each of which run an infinite loop?

To generalize even more, I’m trying to figure out how to do this:

for (int i = 0; i < threads; i++)
    pthread_create(&th[i], NULL, func[i], NULL)

for (int i = 0; i < threads; i++)
    pthread_join(th[i], NULL);

Where func[i] is some arbitrary function which runs in an infinite loop doing some arbitrary thing.

Any help or even a link is appreciated, thanks!

Edit: I should mention it is an interactive simulator, so I do need to have two infinite loops running independent of each other. I can only seem to run at once.

  • 1 1 Answer
  • 1 View
  • 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. Editorial Team
    Editorial Team
    2026-05-14T09:00:48+00:00Added an answer on May 14, 2026 at 9:00 am

    First I would suggest using boost::thread as opposed to pthreads since you are using C++. With boost::thread you can do something like this:

    #include <boost/thread.hpp>
    
    void input_thread()
    {
        //...
    }
    
    void draw_thread()
    {
        //...
    }
    
    int main()
    {
        boost::thread input_th(&input_thread);
        boost::thread draw_th(&draw_thread);
    
        input_th.join();
        draw_th.join();
    
        return 0;
    }
    

    The constructor of a boost::thread automatically spawns a new thread and calls the function passed in. You can also use member functions as threads by using boost::bind. The join function blocks until the thread exits. This is necessary because if main() exits, all of your threads are killed.

    Hopefully that will get you started, but the hard part is synchronizing (sharing data among threads). I suggest you look at the documentation for mutexes and condition variables. Remember that you need to make sure that only one thread is writing to the same memory location at once. Mutexes help solve this problem. Condition variables help by allowing you to signal and wait for signals between threads.

    For instance, in the input thread you may fill a buffer with input then use a condition variable to signal to the draw thread that input is ready. In each thread, a mutex should be locked when accessing the buffer so that it is not overwritten by the input thread while the draw thread is trying to read it. As Goz suggested, a double buffer would make this easier and probably more efficient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 384k
  • Answers 385k
  • 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 Basically, it's the unwinding of the stack that's most expensive.… May 14, 2026 at 11:19 pm
  • Editorial Team
    Editorial Team added an answer It's added by django.contrib.auth.context_processors.auth And the answer for the question… May 14, 2026 at 11:19 pm
  • Editorial Team
    Editorial Team added an answer Short answer: you need to set PYTHONPATH to include C:\py\twisted.… May 14, 2026 at 11:19 pm

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.