2013-01-21 06:15:55 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/pool.h>
|
2013-01-23 02:24:57 +04:00
|
|
|
#include <winpr/interlocked.h>
|
2013-01-21 06:15:55 +04:00
|
|
|
|
2013-01-23 02:24:57 +04:00
|
|
|
static LONG count = 0;
|
2013-01-22 03:33:00 +04:00
|
|
|
|
2013-01-23 02:24:57 +04:00
|
|
|
void CALLBACK test_WorkCallback(PTP_CALLBACK_INSTANCE instance, void* context, PTP_WORK work)
|
2013-01-21 06:15:55 +04:00
|
|
|
{
|
2013-01-23 01:19:32 +04:00
|
|
|
int index;
|
|
|
|
BYTE a[1024];
|
|
|
|
BYTE b[1024];
|
|
|
|
BYTE c[1024];
|
|
|
|
|
2013-03-15 23:03:48 +04:00
|
|
|
printf("Hello %s: %ld (thread: %lu)\n", context,
|
2013-01-23 02:24:57 +04:00
|
|
|
InterlockedIncrement(&count), GetCurrentThreadId());
|
2013-01-23 01:19:32 +04:00
|
|
|
|
|
|
|
for (index = 0; index < 100; index++)
|
|
|
|
{
|
|
|
|
ZeroMemory(a, 1024);
|
|
|
|
ZeroMemory(b, 1024);
|
|
|
|
ZeroMemory(c, 1024);
|
|
|
|
FillMemory(a, 1024, 0xAA);
|
|
|
|
FillMemory(b, 1024, 0xBB);
|
|
|
|
CopyMemory(c, a, 1024);
|
|
|
|
CopyMemory(c, b, 1024);
|
|
|
|
}
|
2013-01-21 06:15:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int TestPoolWork(int argc, char* argv[])
|
|
|
|
{
|
2013-01-22 03:33:00 +04:00
|
|
|
int index;
|
2013-01-22 08:34:46 +04:00
|
|
|
PTP_POOL pool;
|
|
|
|
PTP_WORK work;
|
|
|
|
PTP_CLEANUP_GROUP cleanupGroup;
|
|
|
|
TP_CALLBACK_ENVIRON environment;
|
|
|
|
|
|
|
|
printf("Global Thread Pool\n");
|
2013-01-21 06:15:55 +04:00
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
work = CreateThreadpoolWork((PTP_WORK_CALLBACK) test_WorkCallback, "world", NULL);
|
2013-01-21 06:15:55 +04:00
|
|
|
|
|
|
|
if (!work)
|
|
|
|
{
|
|
|
|
printf("CreateThreadpoolWork failure\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
/**
|
|
|
|
* You can post a work object one or more times (up to MAXULONG) without waiting for prior callbacks to complete.
|
|
|
|
* The callbacks will execute in parallel. To improve efficiency, the thread pool may throttle the threads.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (index = 0; index < 10; index++)
|
|
|
|
SubmitThreadpoolWork(work);
|
|
|
|
|
2013-01-21 06:15:55 +04:00
|
|
|
WaitForThreadpoolWorkCallbacks(work, FALSE);
|
|
|
|
CloseThreadpoolWork(work);
|
|
|
|
|
2013-01-22 08:34:46 +04:00
|
|
|
printf("Private Thread Pool\n");
|
|
|
|
|
|
|
|
pool = CreateThreadpool(NULL);
|
|
|
|
|
|
|
|
SetThreadpoolThreadMinimum(pool, 4);
|
|
|
|
SetThreadpoolThreadMaximum(pool, 8);
|
|
|
|
|
|
|
|
InitializeThreadpoolEnvironment(&environment);
|
|
|
|
SetThreadpoolCallbackPool(&environment, pool);
|
|
|
|
|
|
|
|
cleanupGroup = CreateThreadpoolCleanupGroup();
|
|
|
|
|
|
|
|
if (!cleanupGroup)
|
|
|
|
{
|
|
|
|
printf("CreateThreadpoolCleanupGroup failure\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetThreadpoolCallbackCleanupGroup(&environment, cleanupGroup, NULL);
|
|
|
|
|
|
|
|
work = CreateThreadpoolWork((PTP_WORK_CALLBACK) test_WorkCallback, "world", &environment);
|
|
|
|
|
|
|
|
if (!work)
|
|
|
|
{
|
|
|
|
printf("CreateThreadpoolWork failure\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (index = 0; index < 10; index++)
|
|
|
|
SubmitThreadpoolWork(work);
|
|
|
|
|
|
|
|
WaitForThreadpoolWorkCallbacks(work, FALSE);
|
|
|
|
|
2013-01-23 06:54:13 +04:00
|
|
|
CloseThreadpoolCleanupGroupMembers(cleanupGroup, TRUE, NULL);
|
2013-01-22 08:34:46 +04:00
|
|
|
|
2013-01-23 06:54:13 +04:00
|
|
|
CloseThreadpoolCleanupGroup(cleanupGroup);
|
2013-01-22 08:34:46 +04:00
|
|
|
|
|
|
|
DestroyThreadpoolEnvironment(&environment);
|
|
|
|
|
2013-01-23 02:24:57 +04:00
|
|
|
CloseThreadpoolWork(work);
|
2013-01-22 08:34:46 +04:00
|
|
|
CloseThreadpool(pool);
|
|
|
|
|
2013-01-21 06:15:55 +04:00
|
|
|
return 0;
|
|
|
|
}
|