Problem with an infinite loop in a thread


I am calling (from setup function) a thread that I have created a one of my functions doing:

std::thread t([this] { my_function(); });

It was working fine if the function had an end. But my idea was to create a parallel loop in my program so I decided to create a while loop inside the thread:

 void my_function(){
    while(true){ ... //do some things }

Then my program never continues and update function is never reached. I have tried to add std::this_thread::sleep_for inside the while loop, but it doesn’t work.

Besides, debuging the only info I can get is: Program_loop.exe has triggered a breakpoint.

What else could I do? :sweat:


Hi, try using usleep(1000) inside the while loop. (you need #include < unistd.h > )

For example,

while (true) {
//do some things

You can change the value of 1000 microseconds that is equal to 1 millisecond.

Hi ofPAk

It seems that your main loop is trying to join the std::thread you are creating (it is waiting for it to finish). This worked fine when my_function() was not an infinite loop.

I think this is caused due to the fact that the std::thread t object is trying to be destroyed when the scope where it was created ends.

Long story short:


void ofApp::setup()
    std::thread t([this] { my_function(); });

has to be changed to

void ofApp::setup()
    t = std::thread([this] { my_function(); });

and ofApp.h

class ofApp...
    std::thread t;

Check it out and tell us if this fixes the issue

1 Like

That did it!!! Perfect definition of the problem.
Thank you very much Hennio.

On the other hand, is it possible to assign the highest priority to this thread? How could this be done?
I already have 4 threads:

  • 2 threads created with classes which inherit from ofThread,

  • This new thread created from a function

  • The main (setup-update) thread.

Everything is working fine now, but I need to set that priority.


Setting priority is operating system (OS) defined.

Checkout: on how to get the native handle of the thread for your OS. This native handle is a… well… handle to your object that your OS understands (like a OS pointer to your object)

In windows you can use SetThreadPriority to define the priority.

Thank you again SetThreadPriority did it for me.

However, I have found an issue with my threaded solution. As I told you before, I created:

  • 2 threads created with classes which inherit from ofThread
  • A new thread created from a function in ofApp

One of those 2 ofThread classes, just wirtes data from a buffer to a file.
Sometimes, I think the threadedfunction is called when it is executing and I get the following message in the console:
[warning] ofThread: - name: Thread 2 - Cannot start, thread already running.

Then, that block of data is missed and not wirtten to a file, because the ofThread(ed) class was busy. I have tried using an internal variable to the class for locking the call and using isthreadedRunning() with less frequent but still present error.

				`if (myThread.done ==true){ myThread.dothreadedfunction(); ...}`


				`if (myThread.isThreadRunning() == false) {myThread.dothreadedfunction(); ...}` 

A solution could be to create a different variable of the class for every instance or call in the same loop (but I think is not elegant or done right).

Is there any way to ensure that when a ofThread function of a variable is called, it has finished (or queued) its previous execution?

Thank you again

In case this is helping anybody, finally I solved it by taking the call to the secondary threads to the main program (update) and using:
just after the dunction call.

It is supposed to be the same as: Thread.join()