Portable fork () method in a Qt4 application?

Let's say I need to run a bunch of code that is crashing, so I need to run it in another process. I usually do it 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 use fork in my Qt4 application, but I wonder if I can archive the same functionality with anything Qt4 provides, or in any portable way, without having to create a heap of #ifdefs architecture?

In any case, I only targeted this application with the pthread implementation, but I would like to keep everything as close to the native Qt API as possible.

I tested QThread , and segfault in the stream gives an autopsy of the entire application, and it seems that QProcess intended only to be used for spawning of completely different executable files. Any other alternatives?

+6
c ++ qt qt4 fork
source share
4 answers

Windows flat-out does not have fork () in any public way, so there is no call to Qt to emulate it; you will need to do something, for example, start with special command line options or something like that.

+7
source share

I think you should go with QtConcurrent , as this is the highest level multithreaded programming API available in Qt. This way your code will be simpler and cleaner.
Since this is a high-level API, it is probably implemented on top of the lower-level APIs that you have already tried, so this may probably not solve your problem.

+3
source share

Have you tried to try ... output statements and find out how to avoid failures ????

+1
source share

You can try QThread . It is older and appears to be less convenient than QtConcurrent. It is not automatically configured for the number of cores on the processor, and thus does not evenly distribute the workload. However, this is probably the closest Qt analog to fork (), although it is not a replacement replacement and may work better for you. Both QThread and QtConcurrent are portable across platforms.

0
source share

All Articles