bhupinder
bhupinder

Reputation: 335

Threads creating process in infinite loop

In my application a thread runs while(1){} in it so thread terminates when my app is terminated by user.

Is it safe to do like this? I am using while(1){} because my app continuously monitors devices on system.

After some time I am getting "(R6016) not enough space for thread data" on ffmpeg.

I read this but did not get solution of my problem:

http://support.microsoft.com/kb/126709

Thread description: Thread uses ffmpeg and handle utility (http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx). within while(1){} loop.

ffmpeg and handle is running through QProcess which I am deleting after process ends.

while(1){} loop waits for 5 seconds using msleep(5000).

Upvotes: 0

Views: 435

Answers (1)

egur
egur

Reputation: 7970

This is not safe.

Change while (1) to while (!stopCondition) and have stopCondition change to TRUE when exiting. The main thread should wait for all other thread to finish before exiting.

Note: stopCondition is defined as volatile int stopCondition.

When the main thread exists, a cleanup process starts: - global destructors are called (C++). - C runtime library starts to shut down, releasing all memory allocated with malloc, unloading dynamic libraries and other resources.

A thread that depends on the C runtime being functional will crash or if it runs code from a shared/dynamic libray. If that thread was doing something important like writing to a file, the file will be corrupt. Maybe in your case things are not so bad, but seeing an application crash doesn't looks good to say the least.

This is not the full story, but I think it makes my point.

Upvotes: 3

Related Questions