Skip to content

amitesh-singh/simplethreadpool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simplethreadpool

a simple threadpool library written in C++.

  • starts all threads at start.
  • stops and join all threads at stop.
  • result object for wait.

Build

$ cmake .
$ make
$ make install

Example

#include <iostream>
#include "threadpool.h"
#include <chrono>
#include <algorithm>
#include <ctime>

void doWork()
{
   std::this_thread::sleep_for(std::chrono::seconds(5));
   std::cout << __FUNCTION__ << std::endl;
}

class job
{
  public:
      void run(int index)
        {
           std::this_thread::sleep_for(std::chrono::seconds(5));
           std::cout << __FUNCTION__ << std::endl;
        }
};

int main()
{
   std::srand(std::time(nullptr));
   nonstd::thread_pool pool(4);

   pool.start();

   nonstd::result res;

   std::vector<int> v;
   for (int i = 0; i < 10000000; ++i)
      v.push_back(std::rand() % 15001);

   pool.addJob(doWork); //don't wait
   pool.addJob([&v]()->void {
                                std::sort(std::begin(v), std::end(v));
                                std::cout << "sorting is finished\n";
                            },
                            res);
   {
      job j;
      pool.addJob(std::bind(&job::run, std::ref(j), 1));
   }

   //wait for the job
   res.get();

   std::cout << std::endl;

   pool.stop();

   return 0;
}
$ g++ example.cpp  -lpthread -lthreadpool

TODOs

  • starts threads when required, lazy initialization

About

a simple thread pool library written in C++.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published