iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tests/test-ThreadPool.C
/*! @file
@author
@copyright GNU Public License (GPL v3)
@section License
@verbatim
// ////////////////////////////////////////////////////////////////////////
// The iLab Neuromorphic Robotics Toolkit (NRT) //
// Copyright 2010-2012 by the University of Southern California (USC) //
// and the iLab at USC. //
// //
// iLab - University of Southern California //
// Hedco Neurociences Building, Room HNB-10 //
// Los Angeles, Ca 90089-2520 - USA //
// //
// See http://ilab.usc.edu for information about this project. //
// ////////////////////////////////////////////////////////////////////////
// This file is part of The iLab Neuromorphic Robotics Toolkit. //
// //
// The iLab Neuromorphic Robotics Toolkit is free software: you can //
// redistribute it and/or modify it under the terms of the GNU General //
// Public License as published by the Free Software Foundation, either //
// version 3 of the License, or (at your option) any later version. //
// //
// The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
// that it will be useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
// PURPOSE. See the GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with The iLab Neuromorphic Robotics Toolkit. If not, see //
// <http://www.gnu.org/licenses/>. //
// ////////////////////////////////////////////////////////////////////////
@endverbatim */
#include <thread>
#include <math.h>
int main()
{
NRT_INFO("Testing ThreadPool...");
// create a vector of functions; here we just use a bunch of C++11 lambda functions that sleep for a while and then
// print something:
std::vector<std::function<void()> > funs;
for (int i = 0; i < 30; ++i)
{
std::function<void()> fun = [=]() { usleep(rand()/1000); NRT_INFO(i); };
funs.push_back(fun);
}
nrt::ThreadPool threadPool(4);
NRT_INFO("Pushing " << funs.size() << " jobs onto a pool of " << threadPool.numWorkers() << " worker threads...");
auto sync = threadPool.pushJobs(funs);
sync.wait();
NRT_DEBUG("DONE");
// Now push the jobs one by one, we get one future for each job:
NRT_INFO("Testing ThreadPool, pushing jobs one at a time");
std::vector<std::future<int> > results;
for(int i = 0; i < 30; ++i)
{
std::function<int()> fun = [=]() {usleep(1000000); return i*10; };
results.push_back(threadPool.pushJob(fun));
}
for (std::future<int> & result : results) NRT_INFO("Result: " << result.get());
NRT_DEBUG("DONE");
NRT_INFO("Testing groupAsync on our jobs...");
std::future<void> fut = nrt::groupAsync(funs);
fut.wait();
NRT_DEBUG("DONE");
return 0;
}