void ThreadPoolTest::testPreferredConcurrency()
{ // Check default. auto nThreads = comphelper::ThreadPool::getPreferredConcurrency();
std::size_t nExpected = 4; // UTs are capped to 4.
CPPUNIT_ASSERT_MESSAGE("Expected no more than 4 threads", nExpected >= nThreads);
#ifndef _WIN32 // The result should be cached, so this should change anything.
nThreads = std::thread::hardware_concurrency() * 2;
setenv("MAX_CONCURRENCY", std::to_string(nThreads).c_str(), true);
nThreads = comphelper::ThreadPool::getPreferredConcurrency();
CPPUNIT_ASSERT_MESSAGE("Expected no more than hardware threads",
nThreads <= std::thread::hardware_concurrency());
// Revert and check. Again, nothing should change.
unsetenv("MAX_CONCURRENCY");
nThreads = comphelper::ThreadPool::getPreferredConcurrency();
CPPUNIT_ASSERT_MESSAGE("Expected no more than 4 threads", nExpected >= nThreads); #endif
}
void ThreadPoolTest::testWorkerUsage()
{ // Create tasks for each available worker. Lock a shared mutex before that to make all // tasks block on it. And check that all workers have started, i.e. that the full // thread pool capacity is used.
comphelper::ThreadPool& rSharedPool = comphelper::ThreadPool::getSharedOptimalPool();
std::shared_ptr<comphelper::ThreadTaskTag> pTag = comphelper::ThreadPool::createThreadTaskTag();
UsageTask::mutex.lock(); for (int i = 0; i < rSharedPool.getWorkerCount(); ++i)
{
rSharedPool.pushTask(std::make_unique<UsageTask>(pTag));
osl::Thread::wait(std::chrono::milliseconds(10)); // give it a time to start
}
sal_uInt64 startTicks = tools::Time::GetSystemTicks(); while (UsageTask::count != rSharedPool.getWorkerCount())
{ // Wait at most 5 seconds, that should do even on slow systems.
CPPUNIT_ASSERT_MESSAGE("Thread pool does not use all worker threads.",
startTicks + 5000 > tools::Time::GetSystemTicks());
osl::Thread::wait(std::chrono::milliseconds(10));
}
UsageTask::mutex.unlock();
rSharedPool.waitUntilDone(pTag);
}
namespace
{ class CheckThreadTask : public comphelper::ThreadTask
{
oslThreadIdentifier mThreadId; bool mCheckEqual;
void ThreadPoolTest::testTasksInThreads()
{ // Check that all tasks are run in worker threads, not this thread.
comphelper::ThreadPool& pool = comphelper::ThreadPool::getSharedOptimalPool();
std::shared_ptr<comphelper::ThreadTaskTag> pTag = comphelper::ThreadPool::createThreadTaskTag(); for (int i = 0; i < 8; ++i)
pool.pushTask(
std::make_unique<CheckThreadTask>(osl::Thread::getCurrentIdentifier(), false, pTag));
pool.waitUntilDone(pTag);
}
void ThreadPoolTest::testNoThreads()
{ // No worker threads, tasks will be run in this thread.
comphelper::ThreadPool pool(0);
std::shared_ptr<comphelper::ThreadTaskTag> pTag = comphelper::ThreadPool::createThreadTaskTag(); for (int i = 0; i < 8; ++i)
pool.pushTask(
std::make_unique<CheckThreadTask>(osl::Thread::getCurrentIdentifier(), true, pTag));
pool.waitUntilDone(pTag);
}
void ThreadPoolTest::testDedicatedPool()
{ // Test that a separate thread pool works. The tasks themselves do not matter.
comphelper::ThreadPool pool(4);
std::shared_ptr<comphelper::ThreadTaskTag> pTag = comphelper::ThreadPool::createThreadTaskTag(); for (int i = 0; i < 8; ++i)
pool.pushTask(
std::make_unique<CheckThreadTask>(osl::Thread::getCurrentIdentifier(), false, pTag));
pool.waitUntilDone(pTag);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.