diff --git a/utils/threadpool/threadpool.cpp b/utils/threadpool/threadpool.cpp index 77a3c00e0..d4ea02565 100644 --- a/utils/threadpool/threadpool.cpp +++ b/utils/threadpool/threadpool.cpp @@ -20,7 +20,6 @@ * * ***********************************************************************/ -#define NOLOGGING #include using namespace std; @@ -128,7 +127,7 @@ void ThreadPool::join(uint64_t thrHandle) for (iter = fWaitingFunctors.begin(); iter != end; ++iter) { foundit = false; - if (iter->first == thrHandle) + if (iter->hndl == thrHandle) { foundit = true; break; @@ -158,7 +157,7 @@ void ThreadPool::join(std::vector thrHandle) std::vector::iterator thrEnd = thrHandle.end(); for (thrIter = thrHandle.begin(); thrIter != thrEnd; ++thrIter) { - if (iter->first == *thrIter) + if (iter->hndl == *thrIter) { foundit = true; break; @@ -282,7 +281,7 @@ void ThreadPool::beginThread() throw() for (i = 0; i < num; i++) { try { - (*todoList[i]).second(); + (*todoList[i]).functor(); } catch(exception &e) { ++fFunctorErrors; @@ -366,7 +365,11 @@ int64_t ThreadPool::addFunctor(const Functor_T &func) if (fNextFunctor == fWaitingFunctors.end()) bAtEnd = true; - fWaitingFunctors.push_back(make_pair(fNextHandle, func)); +// PoolFunction_T poolFunction(fNextHandle, func); + PoolFunction_T poolFunction; + poolFunction.hndl = fNextHandle; + poolFunction.functor = func; + fWaitingFunctors.push_back(poolFunction); waitingFunctorsSize++; if (bAtEnd) { diff --git a/utils/threadpool/threadpool.h b/utils/threadpool/threadpool.h index a14a6e4fb..dd5f13b56 100644 --- a/utils/threadpool/threadpool.h +++ b/utils/threadpool/threadpool.h @@ -55,12 +55,10 @@ namespace threadpool * executing tasks. It is responsible for creating threads and tracking which threads are "busy" * and which are idle. Idle threads are utilized as "work" is added to the system. */ - class ThreadPool { public: typedef boost::function0 Functor_T; - typedef pair PoolFunction_T; /********************************************* * ctor/dtor @@ -158,6 +156,13 @@ public: protected: private: + // Used internally to keep a handle associated with each functor for join() + struct PoolFunction_T + { + int64_t hndl; + Functor_T functor; + }; + /** @brief initialize data memebers */ void init(); @@ -193,8 +198,8 @@ private: struct NoOp { void operator () () const - {}} - ; + {} + }; size_t fThreadCount; size_t fMaxThreads; @@ -205,7 +210,7 @@ private: Container_T::iterator fNextFunctor; // Functor_T * fThreadCreated; - uint32_t issued; + uint32_t issued; boost::mutex fMutex; boost::condition fThreadAvailable; // triggered when a thread is available boost::condition fNeedThread; // triggered when a thread is needed diff --git a/utils/threadpool/tp.cpp b/utils/threadpool/tp.cpp index 92eaeeae7..ec128a28e 100644 --- a/utils/threadpool/tp.cpp +++ b/utils/threadpool/tp.cpp @@ -56,13 +56,14 @@ struct foo int64_t fData; int64_t fThd; string start; + bool running; void operator ()() { start = timeNow(); std::cout << "foo thd = " << fThd << " start " << start << std::endl; - for (int64_t i = 0; i < 1024*1024*fThd*128; i++) + for (int64_t i = 0; i < 1024*1024*(fThd+0)*128; i++) // simulate some work fData++; @@ -70,11 +71,11 @@ struct foo std::cout << "foo thd = " << fThd << " start " << start << " fin " << timeNow() << std::endl; } - foo(int64_t i) : fThd(i), fData(i) {start=timeNow();} + foo(int64_t i) : fThd(i), fData(i), running(true) {start=timeNow();} - foo(const foo& copy) : fData(copy.fData), fThd(copy.fThd), start(copy.start) {std::cout << "new foo" << endl;} + foo(const foo& copy) : fData(copy.fData), fThd(copy.fThd), start(copy.start), running(copy.running) {std::cout << "new foo " << fThd << endl;} - ~foo() {} + ~foo() {running=false;} }; @@ -87,11 +88,18 @@ int main( int argc, char **argv) int t1 = hndl.capacity(); uint64_t testHndl; uint64_t thdhndl=999; - for (int64_t y = 0; y < 20; y++) + int64_t thd = 1; + boost::function0 foofunc; + boost::function0 foofunc2; + for (int64_t y = 0; y < 1; y++) { foo bar(y); -// for (int64_t i = 0; i < 10; ++i) +// foofunc = bar; +// foofunc2 = foofunc; + std::cout << "Done with assign" << std::endl; + for (int64_t i = 0; i < 1; ++i) { + bar.fThd=thd++; thdhndl = pool.invoke(bar); if (y<10) { @@ -117,5 +125,6 @@ int main( int argc, char **argv) std::cout << "*** WAIT ***" << std::endl; pool.wait(); pool.dump(); + sleep(2); return 0; } diff --git a/utils/threadpool/tp.vpj b/utils/threadpool/tp.vpj index bffc57e50..a8f1497cb 100644 --- a/utils/threadpool/tp.vpj +++ b/utils/threadpool/tp.vpj @@ -11,7 +11,8 @@ DebugCallbackName="gdb" Version="1" OutputFile="%bdtp" - CompilerConfigName="Latest Version"> + CompilerConfigName="Latest Version" + Defines='"/DNOLOGGING"'> + CompilerConfigName="Latest Version" + Defines='"/DNOLOGGING"'>