# HG changeset patch # User John Tsiombikas # Date 1401422204 -10800 # Node ID 4336acf8389dacde009c0fc10ead542bd90b536c # Parent 56d504cc555ace02160d90a7f4ecde6046caa20f implemented thread pool. not using it yet. diff -r 56d504cc555a -r 4336acf8389d liberebus/liberebus.vcxproj --- a/liberebus/liberebus.vcxproj Thu May 29 07:47:52 2014 +0300 +++ b/liberebus/liberebus.vcxproj Fri May 30 06:56:44 2014 +0300 @@ -25,6 +25,7 @@ + @@ -37,6 +38,7 @@ + @@ -78,7 +80,7 @@ Level3 Disabled WIN32;_DEBUG;_LIB;_USE_MATH_DEFINES;%(PreprocessorDefinitions) - 4244;4305 + 4244;4305;4996 Windows @@ -94,7 +96,7 @@ true true WIN32;NDEBUG;_LIB;_USE_MATH_DEFINES;%(PreprocessorDefinitions) - 4244;4305 + 4244;4305;4996 Windows diff -r 56d504cc555a -r 4336acf8389d liberebus/liberebus.vcxproj.filters --- a/liberebus/liberebus.vcxproj.filters Thu May 29 07:47:52 2014 +0300 +++ b/liberebus/liberebus.vcxproj.filters Fri May 30 06:56:44 2014 +0300 @@ -57,6 +57,9 @@ Header Files + + Header Files + @@ -89,6 +92,9 @@ Source Files + + Source Files + diff -r 56d504cc555a -r 4336acf8389d liberebus/src/erebus_impl.h --- a/liberebus/src/erebus_impl.h Thu May 29 07:47:52 2014 +0300 +++ b/liberebus/src/erebus_impl.h Fri May 30 06:56:44 2014 +0300 @@ -6,6 +6,7 @@ #include "erebus.h" #include "image.h" #include "scene.h" +#include "threadpool.h" struct Option { enum Type { INT, FLOAT, VEC } type; @@ -28,6 +29,8 @@ Image accum; // sample accumulator per pixel Option options[ERB_NUM_OPTIONS]; + ThreadPool tpool; + // render state float inv_gamma; long cur_time; diff -r 56d504cc555a -r 4336acf8389d liberebus/src/threadpool.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liberebus/src/threadpool.cc Fri May 30 06:56:44 2014 +0300 @@ -0,0 +1,58 @@ +#include "threadpool.h" + +ThreadPool::ThreadPool(int num_threads) +{ + quit = false; + + if(num_threads == -1) { + num_threads = std::thread::hardware_concurrency(); + } + + printf("creating thread pool with %d threads\n", num_threads); + + thread = new std::thread[num_threads]; + for(int i=0; i func) +{ + std::unique_lock lock(workq_mutex); + workq.push_back(func); +} + +void ThreadPool::thread_func() +{ + std::unique_lock lock(workq_mutex); + for(;;) { + if(quit) break; + + condvar.wait(lock); + + if(!quit && !workq.empty()) { + std::function work = workq.front(); + workq.pop_front(); + lock.unlock(); + + work(); + + lock.lock(); + } + } +} \ No newline at end of file diff -r 56d504cc555a -r 4336acf8389d liberebus/src/threadpool.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liberebus/src/threadpool.h Fri May 30 06:56:44 2014 +0300 @@ -0,0 +1,30 @@ +#ifndef THREAD_POOL_H_ +#define THREAD_POOL_H_ + +#include +#include +#include +#include +#include + +class ThreadPool { +private: + int num_threads; + std::thread *thread; // array of threads + + std::list> workq; + std::mutex workq_mutex; + std::condition_variable condvar; + + bool quit; + + void thread_func(); + +public: + ThreadPool(int num_threads = -1); + ~ThreadPool(); + + void add_work(std::function func); +}; + +#endif // THREAD_POOL_H_ \ No newline at end of file