Multi-thread terminated unexpectedly with POSIX threads
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Multi-thread terminated unexpectedly with POSIX threads
Hello,
I am using POSIX threads to create several worker threads and scheduling them to run one by one. However, after some random execution time, all of the worker threads got terminated. In addition, this problem does not happen every time. Could you please kindly suggest how to debug this issue? Thanks very much.
Not wanting to sound patronising, but have you tried literally running the code in a debugger? Also, what do you mean by "all of the worker threads got terminated"? Did the whole program terminate? Was there a message printed to the console? What happened?
Not wanting to sound patronising, but have you tried literally running the code in a debugger? Also, what do you mean by "all of the worker threads got terminated"? Did the whole program terminate? Was there a message printed to the console? What happened?
Thanks, John. Basically, I have four threads. 1st is a data acquisition, 2nd is data processing, 3rd is data communication, 4th is data logging. The four threads are scheduled via condition variables to realize execution from 1 to 4 every 100 ms. The code can be running smoothly without any problems for quite a while with the console printing, but occasionally the whole program got stuck and no more output available in the console. I have tried to run in the debug mode to locate which line of code caused this issue. But the whole OS crashes, changed from GUI to console mode. I am also wondering if the crash of my application caused the OS crash. But as this happened occasionally, I find it hard to grasp the intermediate information of the application to analyse the problem. Let me know if you need any other details. Thank you.
Perhaps your program has eaten up every single bit of memory?
Should not be. I have monitored the resources with the vmstat and memory should be enough. The OS crashed only occasionally. Most of the time my program hanged but the OS still keeped running. Strange point is that when the application hangs, the camera capture thread keeps running, while all the other threads get killed. So this camera capture thread might be the problem?
Seriously? You mean the *whole* OS? As in you have to reboot your computer? What OS are you using, and on what platform?
I am using Ubuntu 12.04 running on an Intel Core2Duo computer stack. Today I tried more times and OS seems to be stable Now just trying to figure out the reason for the crash.
It sounds like your threads are deadlocking. Can you share your code or give a more exacting description of how the threads interact, including all synchronization constructs?
It sounds like your threads are deadlocking. Can you share your code or give a more exacting description of how the threads interact, including all synchronization constructs?
Sorry for my late update. I should have identified the problem which is a mutex issue. The situation is that one worker thread is capturing images continuously and updating the images which are accessed by other worker threads. One image data structure is defined for storing the updated image from the capture thread and accessed by other worker threads. I tried to use the mutex mechanism, but the application still got crashed randomly once executed. Looking forward to your suggestions on this. Thank you.
I read your description again and it actually doesn't sound like deadlock, so sorry about that. sundialsvcs is right that it's likely some race condition in your code...
If there are multiple ways to access an image as it is being processed, then make sure the same mutex lock surrounds the code in each of them. Also, there is a 'try_acquire' or similar in the POSIX threads API. If you are using that, you should consider whether the thread that tries to acquire the lock does something that assumes the lock is held. That said, you should minimize the places where you can access the shared image, probably to synchronous read/write methods and have all access occur through those -- at least to start with.
That's all I can think of right now. A debugger probably would help since you'll be able to see what the threads were doing when they crashed. You'll still need to do some reasoning about why what they were doing was allowed to happen.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.