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.
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.
Setting priority is operating system (OS) defined.
Checkout: http://en.cppreference.com/w/cpp/thread/thread/native_handle 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)
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.
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?
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: myThread.waitForThread(false);
just after the dunction call.