Skip to content

Instantly share code, notes, and snippets.

@detunized
Created September 22, 2016 15:53
Show Gist options
  • Save detunized/ef68915060b1ffe5b91eac134f02b7f6 to your computer and use it in GitHub Desktop.
Save detunized/ef68915060b1ffe5b91eac134f02b7f6 to your computer and use it in GitHub Desktop.
Run a lambda in parallel processes
#include <functional>
#include <set>
#include <string>
#include <vector>
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
void
run_all( const std::vector< std::string >& source_files,
std::function< void( const std::string& ) > f )
{
std::set< pid_t > pool;
const size_t pool_max_size = 8;
for ( size_t i = 0, total = source_files.size( ); i < total; i += 1 )
{
// Wait until there's some space in the pool
while ( pool.size( ) >= pool_max_size )
{
int status = 0;
pid_t exited_child = wait( &status );
if ( exited_child <= 0 )
exit( 1 ); // TODO: Shouldn't really happen
pool.erase( exited_child );
}
pid_t pid = fork( );
if ( pid == 0 )
{
// In the child process
const std::string& file = source_files[ i ];
std::cout << "[" << i + 1 << "/" << total << "] " << file << std::endl;
f( file );
// The child process should terminate here
return;
}
else
{
// In the parent process
pool.insert( pid );
}
}
// Wait for the remaining children to terminate
int status = 0;
while (::wait( &status ) > 0 )
{
}
}
int
main( )
{
run_all( {"1", "2", "3"}, []( const std::string& s ) { std::cout << s << std::endl; } );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment