acl/lib_acl_cpp/samples/thread_pool/main.cpp

215 lines
3.8 KiB
C++
Raw Normal View History

#include "stdafx.h"
//////////////////////////////////////////////////////////////////////////
class mythread : public acl::thread_job
{
public:
mythread(bool auto_destroy = false)
{
auto_destroy_ = auto_destroy;
}
~mythread() {}
protected:
virtual void* run()
{
const char* myname = "run";
printf("%s: thread id: %lu\r\n",
myname, acl::thread::thread_self());
sleep(1);
if (auto_destroy_)
delete this;
return NULL;
}
private:
bool auto_destroy_;
};
//////////////////////////////////////////////////////////////////////////
class mythread_pool : public acl::thread_pool
{
public:
mythread_pool() {}
~mythread_pool()
{
printf("thread pool destroy now, tid: %lu\r\n",
(unsigned long) acl_pthread_self());
}
protected:
virtual bool thread_on_init()
{
const char* myname = "thread_on_init";
printf("%s: curr tid: %lu\r\n", myname,
acl::thread::thread_self());
return true;
}
virtual void thread_on_exit()
{
const char* myname = "thread_on_exit";
printf("%s: curr tid: %lu\r\n", myname,
acl::thread::thread_self());
}
private:
};
//////////////////////////////////////////////////////////////////////////
static void test_thread_pool(int n)
{
if (n == 0)
{
mythread_pool* threads = new mythread_pool;
threads->start();
mythread thread1, thread2;
threads->execute(&thread1);
threads->execute(&thread2);
// <20><>Ϊ thread1, thread2 <20>Ƕ<EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// threads->stop <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD>
threads->stop();
delete threads;
}
else if (n == 1)
{
mythread_pool threads;
threads.start();
mythread *thread1 = new mythread, *thread2 = new mythread;
threads.execute(thread1);
threads.execute(thread2);
// Ϊ<>˱<EFBFBD>֤ threads1, thread2 <20><>̬<EFBFBD>ڴ汻<DAB4><E6B1BB>ȷ<EFBFBD>ͷţ<CDB7>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> threads.stop <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD>߳<EFBFBD><DFB3>н<EFBFBD><D0BD><EFBFBD><EFBFBD>ͷ<EFBFBD>
threads.stop();
// <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD><EFBFBD>ͷŶ<CDB7>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
delete thread1;
delete thread2;
}
else if (n == 2)
{
mythread_pool threads;
threads.start();
mythread thread1, thread2;
threads.execute(&thread1);
threads.execute(&thread2);
// <20><>Ϊ thread1, thread2 <20>Ƕ<EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// threads->stop <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD>
threads.stop();
}
else if (n == 3)
{
mythread_pool threads;
threads.start();
mythread *thread1 = new mythread(true);
mythread *thread2 = new mythread(true);
threads.execute(thread1);
threads.execute(thread2);
threads.stop();
}
else if (n == 4)
{
static mythread_pool threads;
threads.set_idle(1);
threads.start();
mythread *thread1 = new mythread(true);
mythread *thread2 = new mythread(true);
threads.execute(thread1);
threads.execute(thread2);
if (threads.threads_count() > 0)
{
printf("current task count: %d\r\n", threads.task_qlen());
threads.stop();
}
}
else if (n == 5)
{
// <20><><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ʹ<EFBFBD>û<EFBFBD><C3BB><EFBFBD>
static acl::thread_pool threads;
threads.set_idle(1);
threads.start();
mythread *thread1 = new mythread(true);
mythread *thread2 = new mythread(true);
threads.execute(thread1);
threads.execute(thread2);
if (threads.threads_count() > 0)
{
printf("current task count: %d\r\n", threads.task_qlen());
threads.stop();
}
}
else
{
mythread_pool threads;
threads.start();
static mythread thread1, thread2;
threads.execute(&thread1);
threads.execute(&thread2);
threads.stop();
}
}
//////////////////////////////////////////////////////////////////////////
static void usage(const char* proc)
{
printf("usage: %s -h [help]\r\n"
" -c which_case [0, 1, 2, 3, 4, 5]\r\n",
proc);
}
int main(int argc, char* argv[])
{
int ch, n = 0;
// <20><>ʼ<EFBFBD><CABC> acl <20><>
acl::acl_cpp_init();
while ((ch = getopt(argc, argv, "hc:")) > 0)
{
switch (ch)
{
case 'h':
usage(argv[0]);
return 0;
case 'c':
n = atoi(optarg);
break;
default:
break;
}
}
test_thread_pool(n);
#ifdef WIN32
printf("enter any key to exit ...\r\n");
getchar();
#endif
return 0;
}