From c6e48cf47ca73319a465473053ee3b5f41014060 Mon Sep 17 00:00:00 2001 From: Eric Teunis de Boone Date: Thu, 9 Jul 2020 22:52:04 +0200 Subject: [PATCH] Ex10: Successfull pyBind11 --- ex10/{threadpool.cc => ThreadPool.cc} | 0 ex10/main.py | 20 ++++++++++++++++++++ ex10/makefile | 8 ++++++++ ex10/threadpool.hh | 25 ++++++++++++++++++++----- 4 files changed, 48 insertions(+), 5 deletions(-) rename ex10/{threadpool.cc => ThreadPool.cc} (100%) create mode 100644 ex10/main.py create mode 100644 ex10/makefile diff --git a/ex10/threadpool.cc b/ex10/ThreadPool.cc similarity index 100% rename from ex10/threadpool.cc rename to ex10/ThreadPool.cc diff --git a/ex10/main.py b/ex10/main.py new file mode 100644 index 0000000..e4214a0 --- /dev/null +++ b/ex10/main.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +import ThreadPool + +if __name__ == "__main__": + print(dir(ThreadPool)) + tp = ThreadPool.ThreadPool() + + print("Creating Consumers") + tp.addConsumer( 2000, 20 ) + tp.addConsumer( 2000, 10 ) + + + print("Create Producers") + tp.addProducer( 4000, 5 ) + + print("Run all Threads") + tp.run() + + print("End of Program") diff --git a/ex10/makefile b/ex10/makefile new file mode 100644 index 0000000..52c7362 --- /dev/null +++ b/ex10/makefile @@ -0,0 +1,8 @@ +main: + g++ -pthread ThreadPool.cc main.cpp + +threadpool: + g++ -pthread -D ENABLE_PYBIND=1 -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` ThreadPool.cc -o ThreadPool`python3-config --extension-suffix` + +clean: + rm -rf *.so __pycache__ diff --git a/ex10/threadpool.hh b/ex10/threadpool.hh index e9e56a6..b823d79 100644 --- a/ex10/threadpool.hh +++ b/ex10/threadpool.hh @@ -19,22 +19,19 @@ class ThreadPool _consumers(), _producers() {}; - auto addProducer( int max_t = 3000, int max_n = 10) + void addProducer( int max_t = 3000, int max_n = 10) { std::thread * tp = new std::thread( producer, _producers.size(), std::ref(_q), max_t, max_n ); _producers.push_back(tp); - return tp; } - auto addConsumer( int max_t = 2000, int max_n = 10) + void addConsumer( int max_t = 2000, int max_n = 10) { std::thread * tp = new std::thread( consumer, _consumers.size(), std::ref(_q), max_t, max_n ); _consumers.push_back(tp); - - return tp; } void run() @@ -61,4 +58,22 @@ class ThreadPool // queue SharedQueue _q; }; + +#if ENABLE_PYBIND +/* + * Define Python access + */ +#include + +namespace py = pybind11; + +PYBIND11_MODULE(ThreadPool, tp) +{ + py::class_( tp, "ThreadPool") + .def(py::init()) + .def("addProducer", &ThreadPool::addProducer, "Add a new Producer to the Pool") + .def("addConsumer", &ThreadPool::addConsumer, "Add a new Consumer to the Pool") + .def("run", &ThreadPool::run, "Run all registered Producers and Consumers"); +} +#endif #endif