X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=src%2Fthreadgenesorter.cpp;fp=src%2Fthreadgenesorter.cpp;h=d4fe06995d2d850298cb011346c3700668d88dce;hb=6ce65cd9df9c007b0c7ad0e37fb82ac04a802a90;hp=2276a2fdd271f05a04ae286831fb30ce9d0a20fc;hpb=6b0585c4c6aa12aad13965d5f17be9a4a51653b5;p=germs.git diff --git a/src/threadgenesorter.cpp b/src/threadgenesorter.cpp index 2276a2f..d4fe069 100644 --- a/src/threadgenesorter.cpp +++ b/src/threadgenesorter.cpp @@ -23,6 +23,9 @@ #include "genealgorithms.h" +#include +#include + #include using namespace std; @@ -72,15 +75,41 @@ void ThreadGeneSorter::join(){ } void ThreadGeneSorter::stop(){ + Mutex m(&_queuelock); _done = true; + pthread_cond_signal(&_waiting); } size_t ThreadGeneSorter::size(){ - //TODO: thread safety.. + Mutex m(&_solutionslock); return _solutions.size(); } -double progress(int time, int solutions = 1){ +bool ThreadGeneSorter::running(){ + return !_done; +} + +double ThreadGeneSorter::wait(time_t time, size_t solutions){ + + timeval now; + gettimeofday(&now, NULL); + timespec timeout; + timeout.tv_sec = now.tv_sec + time; + timeout.tv_nsec = 0; + + int err = 0; + + Mutex m(&_solutionslock); + size_t n = _solutions.size(); + + while (!_done && err != ETIMEDOUT + && _solutions.size() - n < solutions){ + err = pthread_cond_timedwait(&_addedSolution,&_solutionslock + , &timeout); + } + if (_solutions.size() == 0) + return 0.0; + return _solutions.top().first; } ThreadGeneSorter::SolutionsQueue ThreadGeneSorter::solutions(){