I’ve been using Boost threads in my iPhone application for a while, and have just stumbled upon a problem. Every so often, when the application tries to start such a thread, it throws an error while trying to call start_thread(), and the application crashes.
I don’t really know where to begin debugging this – it doesn’t seem obvious from the debugger why the boost thread can’t be initialised. It should be noted that this only happens after the application has been running for a while, and after several threads have already been started successfully.
I’ve recently been working on the UI, and I’ve been using the following command to ensure that every update to the UI takes place immediately upon being called:
[myViewController performSelectorOnMainThread : @ selector(show_myButton) withObject:nil waitUntilDone:YES];
While that command seems to work, and the UI is updated immediately (where before it was delayed), I fear it might be responsible for the boost thread problems I’m having. Does that sound likely? Has anyone experienced anything like this before, and have any suggestions as to what might be wrong?
Here’s the code that is meant to start the thread:
void ObjectiveCPlusPlusInterface::startMyThread(void* a, myObject* b)
{
boost::thread AVPlayerThread = boost::thread(&ObjectiveCPlusPlusInterface::myThreadFunction, this, a, b);
}
(where a & b are variables)
void ObjectiveCPlusPlusInterface::myThreadFunction(void* a, myObject* b)
{
...
}
As you can see, startMyThread() tries to start a boost thread, which performs the function myThreadFunction(). However, myThreadFunction() is not reached, as starting the boost thread throws an error.
Looking at the manual, we see that the
boost::threadconstructor can throw an exception. Make sure that you check for that:Update: After your edit, I see that you don’t join or detach the thread! That’s not allowed.
You can put the entire block above literally inside your
startMyThreadfunction.