7

I'm coming from Python and heading into C++ at full-throttle. And, one of the questions that I came up with recently is this: Is there a widely used open source multiprocessing abstraction library in C++? I'm thinking of something that makes multiprocessing (ala fork) a bit easier to manage, similar to Python's multiprocessing stdlib library.

I guess there isn't anything like this. I fully expected there to be a Boost::Process just like there is a Boost::Thread.

bitcycle
  • 7,632
  • 16
  • 70
  • 121
  • You really should look into OpenMP, although this is meant for multi-threading. I suppose the question is, what is really your use model, are you truly having one app calling another completely different app and forking off to it, or are you wanting to build apps that need to thread off within itself? – trumpetlicks Feb 04 '13 at 17:06

6 Answers6

1

Would MPI fit? I think its easy enough to program once you understood it. And it is truly multi-proccess

TeaOverflow
  • 2,468
  • 3
  • 28
  • 40
1

OpenMP (Open Multi-Processing) is the only library I know of http://en.wikipedia.org/wiki/OpenMP -- it does not however, handle things the way Python does by creating new processes. OpenMP is a compiler extension, supported by Microsoft and GNU GCC.

Example: OpenMP sieve of eratosthenes

// odd-only sieve
int eratosthenesOdd(int lastNumber, bool useOpenMP)
{
  // enable/disable OpenMP
  omp_set_num_threads(useOpenMP ? omp_get_num_procs() : 1);
  // instead of i*i <= lastNumber we write i <= lastNumberSquareRoot to help OpenMP
  const int lastNumberSqrt = (int)sqrt((double)lastNumber);
  int memorySize = (lastNumber-1)/2;
  // initialize
  char* isPrime = new char[memorySize+1];
  #pragma omp parallel for
  for (int i = 0; i <= memorySize; i++)
    isPrime[i] = 1;
  // find all odd non-primes
  #pragma omp parallel for schedule(dynamic)
  for (int i = 3; i <= lastNumberSqrt; i += 2)
    if (isPrime[i/2])
      for (int j = i*i; j <= lastNumber; j += 2*i)
        isPrime[j/2] = 0;
  // sieve is complete, count primes
  int found = lastNumber >= 2 ? 1 : 0;
  #pragma omp parallel for reduction(+:found)
  for (int i = 1; i <= memorySize; i++)
    found += isPrime[i];
  delete[] isPrime;
  return found;
}
silverjam
  • 400
  • 1
  • 7
  • Your's is, IMO, the best answer here. Though its not what I was looking for, exactly. :( I might have to build what I'm looking for, if it doesn't exist. – bitcycle Feb 11 '13 at 16:05
  • 1
    openmp is not about multiprocessing and not a library. It just has nothing to do with the question – Slava Sep 23 '15 at 08:20
0

Intel TBB and Microsoft PPL are two task-based multithreading libraries.

Pubby
  • 51,882
  • 13
  • 139
  • 180
0

I am looking the the POCO library, which has a section about multiprocessing.
I am also considering fork(), which has a few implementation for Windows, too.

Community
  • 1
  • 1
Pietro
  • 12,086
  • 26
  • 100
  • 193
0

Take a look at http://pocoproject.org/ (Process::launch()) and http://www.highscore.de/boost/process/ . I would go for the second if I had to choose, but both of them are actually quite far from convenience Python offers (sigh).

Slava
  • 1,528
  • 1
  • 15
  • 23
0

There is now Boost support for multiprocessing: https://www.boost.org/doc/libs/1_76_0/doc/html/boost_process/tutorial.html

From some messages returned by my compiler (LLVM/clang), it looks like boost::process is a wrapper aroung POSIX fork (and the corresponding one for Windows), just like the POCO library does.

Pietro
  • 12,086
  • 26
  • 100
  • 193