Thread stops randomly in the middle of a while loop

六月ゝ 毕业季﹏ 提交于 2021-02-07 19:42:33

问题


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:

  1. Reducing the level of print statements in other threads.

  2. Reducing the priority of other threads.

  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!