3

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

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
  • Certain forms of `while(1);` can be optimized out in c++0x if it performs: no calls to i/o functions, does not access volatile objects and does not perform synchronization or atomic operations. So beware :) – Johannes Schaub - litb Jan 03 '10 at 14:06
  • If I understand correctly, you want to put a main loop in a constructor? That's a strange idea. I suggest you put the main loop in a method. – Etienne Dechamps Jan 03 '10 at 14:07
  • It is a good way to avoid seg faults. A hung thread cannot do a lot of damage. – Hans Passant Jan 03 '10 at 15:21
  • 2
    Putting work into a separate thread is not generally a good way of avoiding or coping with segfaults. There's no general way to recover from crashing bugs in C++ programs. Threading often makes thing worse, not better, by making the program's behavior less deterministic, which leads to hard-to-reproduce bugs and complicated debugging. – Jason Orendorff Jan 03 '10 at 17:49
  • is this still a problem with gcc 4.7 & boost 1.54? –  Jul 07 '13 at 04:43

5 Answers5

10

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.

  • and it will also block the caller – Sander Rijken Jan 03 '10 at 14:06
  • 1
    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 Jan 03 '10 at 17:39
  • 2
    Nothing wrong per se with doing long-running work in the constructor - this is a C++ myth. –  Jan 03 '10 at 17:48
5

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
  • 42,793
  • 6
  • 62
  • 96
1

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
  • 6,590
  • 2
  • 26
  • 19
  • 1
    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 Jan 03 '10 at 17:23
  • 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 Jan 03 '10 at 22:15
1

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
  • 243,077
  • 51
  • 345
  • 550
0

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
  • 56,849
  • 17
  • 98
  • 154