Documentation for concurrent and small improvements
git-svn-id: svn://db.shs.com.ru/pip@868 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -11,7 +11,8 @@ PIThreadPoolExecutor::PIThreadPoolExecutor(size_t corePoolSize, PIBlockingDequeu
|
||||
auto runnable = taskQueue->poll(100, std::function<void()>());
|
||||
if (runnable) {
|
||||
runnable();
|
||||
} else if (isShutdown_) threadPool[i]->stop();
|
||||
}
|
||||
if (isShutdown_ && taskQueue->size() == 0) threadPool[i]->stop();
|
||||
});
|
||||
threadPool.push_back(thread);
|
||||
thread->start();
|
||||
@@ -35,7 +36,6 @@ void PIThreadPoolExecutor::shutdownNow() {
|
||||
|
||||
PIThreadPoolExecutor::~PIThreadPoolExecutor() {
|
||||
shutdownNow();
|
||||
taskQueue->getConditionVar()->notifyAll();
|
||||
while (threadPool.size() > 0) delete threadPool.take_back();
|
||||
delete threadFactory;
|
||||
delete taskQueue;
|
||||
@@ -48,3 +48,7 @@ void PIThreadPoolExecutor::execute(const std::function<void()> &runnable) {
|
||||
volatile bool PIThreadPoolExecutor::isShutdown() const {
|
||||
return isShutdown_;
|
||||
}
|
||||
|
||||
void PIThreadPoolExecutor::shutdown() {
|
||||
isShutdown_ = true;
|
||||
}
|
||||
|
||||
@@ -3,13 +3,12 @@
|
||||
//
|
||||
|
||||
#include "piplatform.h"
|
||||
|
||||
#ifdef WINDOWS
|
||||
#define _WIN32_WINNT 0x0600
|
||||
#include "synchapi.h"
|
||||
#include <windef.h>
|
||||
#include <winbase.h>
|
||||
#else
|
||||
|
||||
#endif
|
||||
|
||||
#include "piconditionvar.h"
|
||||
@@ -68,15 +67,15 @@ void PIConditionVariable::wait(PIConditionLock& lk, const std::function<bool()>&
|
||||
}
|
||||
|
||||
bool PIConditionVariable::waitFor(PIConditionLock &lk, int timeoutMs) {
|
||||
bool isTimeout;
|
||||
bool isNotTimeout;
|
||||
#ifdef WINDOWS
|
||||
isTimeout = SleepConditionVariableCS(&PRIVATE->nativeHandle, (PCRITICAL_SECTION)lk.handle(), timeoutMs) != 0;
|
||||
isNotTimeout = SleepConditionVariableCS(&PRIVATE->nativeHandle, (PCRITICAL_SECTION)lk.handle(), timeoutMs) != 0;
|
||||
#else
|
||||
timespec abstime = {.tv_sec = timeoutMs / 1000, .tv_nsec = timeoutMs * 1000 * 1000};
|
||||
isTimeout = pthread_cond_timedwait(&PRIVATE->nativeHandle, (pthread_mutex_t*)lk.handle(), &abstime) == 0;
|
||||
isNotTimeout = pthread_cond_timedwait(&PRIVATE->nativeHandle, (pthread_mutex_t*)lk.handle(), &abstime) == 0;
|
||||
#endif
|
||||
if (PRIVATE->isDestroying) return false;
|
||||
return isTimeout;
|
||||
return isNotTimeout;
|
||||
}
|
||||
|
||||
bool PIConditionVariable::waitFor(PIConditionLock& lk, int timeoutMs, const std::function<bool()> &condition) {
|
||||
|
||||
@@ -31,3 +31,28 @@ TEST(ExcutorIntegrationTest, execute_is_not_execute_after_shutdown) {
|
||||
piMSleep(WAIT_THREAD_TIME_MS);
|
||||
ASSERT_FALSE(isRunnableInvoke);
|
||||
}
|
||||
|
||||
TEST(ExcutorIntegrationTest, execute_is_execute_before_shutdown) {
|
||||
bool isRunnableInvoke = false;
|
||||
PIThreadPoolExecutor executorService(1);
|
||||
executorService.execute([&]() {
|
||||
piMSleep(WAIT_THREAD_TIME_MS);
|
||||
isRunnableInvoke = true;
|
||||
});
|
||||
executorService.shutdown();
|
||||
piMSleep(2 * WAIT_THREAD_TIME_MS);
|
||||
ASSERT_TRUE(isRunnableInvoke);
|
||||
}
|
||||
|
||||
TEST(ExcutorIntegrationTest, execute_is_awaitTermination_wait) {
|
||||
PIThreadPoolExecutor executorService(1);
|
||||
executorService.execute([&]() {
|
||||
piMSleep(2 * WAIT_THREAD_TIME_MS);
|
||||
});
|
||||
executorService.shutdown();
|
||||
PITimeMeasurer measurer;
|
||||
ASSERT_TRUE(executorService.awaitTermination(4 * WAIT_THREAD_TIME_MS));
|
||||
double waitTime = measurer.elapsed_m();
|
||||
ASSERT_GE(waitTime, WAIT_THREAD_TIME_MS);
|
||||
ASSERT_LE(waitTime, 3 * WAIT_THREAD_TIME_MS);
|
||||
}
|
||||
Reference in New Issue
Block a user