tags:

views:

1013

answers:

4

Say, i need to run a bunch of code that is prone to crash so i need to run it on a different process. Typically i'd do it something like this:

  pid = fork();
  if (pid == -1) {
    std::cout << "Cant Spawn New Thread";
    exit(0);
  } else if (pid == 0) {
    std::cout << "Im a child that will crash\n";
    char *foo = (char *) 0xffff;
    std::cout << foo;
    exit(0);
  } else {
    std::cout << "PID:  " << pid << "\n";
  }
  do {
    std::cout << "Waiting for " << pid << "  to finish .. \n";
    pid_w = waitpid(pid,&status,0);
  } while (pid_w == -1);

Obviously i can just use fork in my qt4 application but im wondering can i archive same functionality with any anything that qt provides or any portable manner without resorting to having bunch of architecture ifdefs ?

In any case, im targeting this app to have only pthread implementation but i'd still like to keep things as much close to "native" qt api as possible.

I've tested QThread, and segfaulting in thread crashes the whole application obvously and it seems that QProcess is only targetted to be used when spawning completely different executables. Any other alternatives ?

A: 

Have you tried try...catch statements & figuring out how to avoid crashing????

ryansstack
Sorry but its not an option, the code that crashes *should* just crash if it wants to and my application should just catch that and just keep on ticking without crashing itself.
rasjani
Trying to catch AVs and swallow them is a massive security vulnerability
Paul Betts
+2  A: 

I think you should go with QtConcurrent as it's the most high-level API for multithreaded programming available in Qt. This way your code will be more simple and cleaner.
As this is a high-level API it's probably implemented on top of lower-level APIs you already tried so this probably may not solve your problem, however.

Piotr Dobrogost
+2  A: 

Windows flat-out doesn't have fork() in any publicly consumable way, so there's no Qt call to emulate it; you'll need to do something like start yourself with special command-line params or something.

Paul Betts
A: 

If you use Qt than don't use any native libs/functions, like pthreads, or fork(), because that is the whole point of using cross-platform framework that you use it to be cross-platform and not building your own cross-platform(ish) code on the side. Use QThread and QProcess, QtConcurrent, use Qt's idioms and style and you'll be OK.