views:

279

answers:

5

Is it recommed to put a while loop, which never ends in a constructor? Or should I use threads to get the same result? Is it good when a constructor never terminates? Or is it more secure to avoid segmentation faults?

Hope you understand my bad English..

+9  A: 

An object does not exist if its constructor does not finish. So putting a while(1) loop in a constructor will prevent objects being created using that constructor. You need to describe what problem you think doing this will solve.

anon
and it will also block the caller
Sander Rijken
It sounds like the OP is looking to model a long-running operation with a class instance. If so, *starting* the operation in the constructor is the wrong idea, unless the instance merely spawned an asynchronous operation that it will then allow a caller to wait on, like a future. Actually *doing* the long-running work in the constructor is not acceptable.
seh
Nothing wrong per se with doing long-running work in the constructor - this is a C++ myth.
anon
+1  A: 

I'd say it's perfectly OK to spawn a thread from a constructor, and a horribly bad idea to have an endless loop in the constructor.

In code, the following is OK:

void start_a_thread_with_a_loop()
{
    while(1)
    {
         // consider a while(!stop_condition) or something
         // do something in a loop
    }
}

class x
{
public:
    x()
    {
        start_a_thread_with_a_loop();
    }
};

And the following would be at least a bad idea:

class x
{
public:
    x()
    {
        while(1)
        {
        }
    }
};

Good thing though, is that likely you wouldn't be able to use such object, for reasons Neil pointed out :)

Dmitry
One danger of spawning threads in constructors is that it interacts badly with subclassing. When creating an object of a subclass, the thread can start running before the subclass constructor runs. Aside from just generally being iffy, in C++ this means virtual method calls in the thread may call base class methods instead of subclass methods. (Of course if `this` is not used in the spawned thread, there's no problem.)
Jason Orendorff
Just don't derive from the class then. Giving it a nonvirtual destructor is usually understood as a strong hint that the class isn't designed to be derived from.
jalf
+5  A: 

Is it recommed to put a while loop, which never ends in a constructor?

No.

Or should I use threads to get the same result?

No.

Is it good when a constructor never terminates?

No.

Or is it more secure to avoid segmentation faults?

No.

What problem are you trying to solve?

Jason Orendorff
A: 

In embedded systems, most endless loops should be avoided. They should either be timed or counted iterations. If the loop is terminated, an error check should be performed. This refrains from the system being "locked up".

One exception to the rule is the background loop. This is the main loop in which events are checked and keeps the machine running. Without this loop, machines may just terminate or halt.

Thomas Matthews
+1  A: 

Your code shouldn't generate a segmentation fault whether or not it uses multiple threads or contains infinite loops.

None of those have anything to do with segmentation faults. If those occur, you need to fix that problem

jalf