问题
I have a program which is doing something very interesting. Basicly i have three main threads, all are busy and the problem thread basiclly has a while loop that looks like this:
while(variable which is always true) {
get some data;
process data;
print message;
}
print end message;
Now the print message gets printed for about the first 400 rounds but after this the thread just stops running. I have tried making this thread a high priority. I have tried reducing the priority of other threads. The strangest thing is that this was working previously and while debugging another problem (which was done only with print statements) it started to happen intermittently. Now it has become permanent, as in it happens every time.
Things that solved it temporarily (then it seemingly stopped working again) were:
Reducing the level of print statements in other threads.
Reducing the priority of other threads.
Running at different times.
As a disclaimer I am using a real time operating system called QNX which may be causing the problem directly. I am hoping its not this otherwise we have a big core i7 and no ability to use threads.
Unfortunately the code is very long and is for work so I really shouldn't post it. I am hoping for help that will point me in the correct direction to solving it myself. Does anyone know of issues that can cause these symptoms?
回答1:
This very much sounds like a race condition.
Make sure, that there are no circumstances where a locked mutex is not properly unlocked, because that could lead to a deadlock.
Unfortunately debugging threading issues is very complex, so you're pretty much on your own here.
回答2:
Threads Programming:Synchronization - http://www.cs.cf.ac.uk/Dave/C/node31.html
Read this, scoll down to Synchronization - http://www.qnx.com/developers/docs/6.3.0SP3/neutrino/sys_arch/kernel.html
http://www.qnx.com/developers/docs/6.3.0SP3/neutrino/sys_arch/kernel.html#SCHEDULING
If the same data is being read by all threads then use lock.
pthread_mutex_lock( &m );
. . .
while (!arbitrary_condition) {
pthread_cond_wait( &cv, &m );
}
. . .
pthread_mutex_unlock( &m );
来源:https://stackoverflow.com/questions/13339462/thread-stops-randomly-in-the-middle-of-a-while-loop