Example #1
0
std::vector< coroutine >
queue_to_vec_cor( queue q, event::condition_variable* cv, event::mutex* mt, size_t min )
{
  if ( cv ) {
    assert( mt );
  }

  std::vector< coroutine > cors;

  if ( q.type( ) == queue_type::serial ) {
    if ( cv ) {
      auto func = [ q = std::move( q ), cv, mt, min ]( ) mutable
      {
        for ( size_t i = 0; q.run_once( ); ++i ) {
          if ( i >= min ) {
            boost::lock_guard< event::mutex > lock( *mt );
            cv->notify( );
          }
        }
      };
      cors.emplace_back( std::move( func ) );
    } else {
      auto func = [q = std::move( q )]( ) mutable
      {
        q.run_until_empty( );
      };
      cors.emplace_back( std::move( func ) );
    }
  } else {
    cors.reserve( q.work_queue.size( ) );
    if ( cv ) {
      auto atomic = std::make_shared< std::atomic< size_t > >( 0 );
      for ( auto& work : q.work_queue ) {
        auto func = [ work = std::move( work ), atomic, cv, mt, min ]( ) mutable
        {
          ( *work )( );
          size_t atom_val = ++( *atomic );
          if ( atom_val >= min ) {
            boost::lock_guard< event::mutex > lock( *mt );
            cv->notify( );
          }
        };
        cors.emplace_back( std::move( func ) );
      }
    } else {
      for ( auto& work : q.work_queue ) {
        cors.emplace_back( std::move( work ) );
      }
    }
  }

  return cors;
}