Thread/ThreadPool enhancements (thread stack size control, static function use as a thread routine) are back ported from trunk.

This commit is contained in:
Sergey Skorokhodov
2008-05-07 18:32:07 +00:00
parent bed329e7d7
commit 9e07d619fb
13 changed files with 629 additions and 57 deletions

View File

@@ -35,11 +35,18 @@
#include "CppUnit/TestSuite.h"
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include "Poco/ThreadTarget.h"
#include "Poco/Event.h"
#include <iostream>
#if defined(__sun) && defined(__SVR4)
#define __EXTENSIONS__
#include <limits.h>
#endif
using Poco::Thread;
using Poco::Runnable;
using Poco::ThreadTarget;
using Poco::Event;
@@ -74,6 +81,13 @@ public:
_event.set();
}
static void staticFunc()
{
++_staticVar;
}
static int _staticVar;
private:
bool _ran;
std::string _threadName;
@@ -81,6 +95,21 @@ private:
};
int MyRunnable::_staticVar = 0;
void freeFunc()
{
++MyRunnable::_staticVar;
}
void freeFunc(void* pData)
{
MyRunnable::_staticVar += *reinterpret_cast<int*>(pData);
}
ThreadTest::ThreadTest(const std::string& name): CppUnit::TestCase(name)
{
}
@@ -195,6 +224,82 @@ void ThreadTest::testJoin()
}
void ThreadTest::testThreadTarget()
{
ThreadTarget te(&MyRunnable::staticFunc);
Thread thread;
assert (!thread.isRunning());
int tmp = MyRunnable::_staticVar;
thread.start(te);
thread.join();
assert (tmp + 1 == MyRunnable::_staticVar);
ThreadTarget te1(freeFunc);
assert (!thread.isRunning());
tmp = MyRunnable::_staticVar;
thread.start(te1);
thread.join();
assert (tmp + 1 == MyRunnable::_staticVar);
}
void ThreadTest::testThreadFunction()
{
Thread thread;
assert (!thread.isRunning());
int tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp);
thread.join();
assert (tmp * 2 == MyRunnable::_staticVar);
assert (!thread.isRunning());
tmp = MyRunnable::_staticVar = 0;
thread.start(freeFunc, &tmp);
thread.join();
assert (0 == MyRunnable::_staticVar);
}
void ThreadTest::testThreadStackSize()
{
int stackSize = 50000000;
Thread thread;
assert (0 == thread.getStackSize());
thread.setStackSize(stackSize);
assert (stackSize == thread.getStackSize());
int tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp);
thread.join();
assert (tmp * 2 == MyRunnable::_staticVar);
stackSize = 1;
thread.setStackSize(stackSize);
#if defined(POCO_OS_FAMILY_UNIX) && !defined(POCO_OS_CYGWIN)
assert (PTHREAD_STACK_MIN == thread.getStackSize());
#else
assert (stackSize == thread.getStackSize());
#endif
tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp);
thread.join();
assert (tmp * 2 == MyRunnable::_staticVar);
thread.setStackSize(0);
assert (0 == thread.getStackSize());
tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp);
thread.join();
assert (tmp * 2 == MyRunnable::_staticVar);
}
void ThreadTest::setUp()
{
}
@@ -214,6 +319,9 @@ CppUnit::Test* ThreadTest::suite()
CppUnit_addTest(pSuite, ThreadTest, testCurrent);
CppUnit_addTest(pSuite, ThreadTest, testThreads);
CppUnit_addTest(pSuite, ThreadTest, testJoin);
CppUnit_addTest(pSuite, ThreadTest, testThreadTarget);
CppUnit_addTest(pSuite, ThreadTest, testThreadFunction);
CppUnit_addTest(pSuite, ThreadTest, testThreadStackSize);
return pSuite;
}