1+ #include < iostream>
2+ #include < thread>
3+ #include < chrono>
4+ #include < atomic>
5+ #include " cppbase/threadpool.hpp"
6+ #include " cppbase/err_code.hpp"
7+
8+ void TestBasicUsage ()
9+ {
10+ std::cout << " === Test Basic Usage ===" << std::endl;
11+
12+ cppbase::ThreadPool pool;
13+ int ret = pool.Start (4 , 100 );
14+ if (ret != cppbase::ErrorCode::EC_OK )
15+ {
16+ std::cout << " Failed to start thread pool, error: " << ret << std::endl;
17+ return ;
18+ }
19+
20+ std::cout << " Thread pool started" << std::endl;
21+
22+ for (int i = 0 ; i < 5 ; ++i)
23+ {
24+ pool.AddTask ([i]()
25+ {
26+ std::cout << " Task " << i << " executed by thread" << std::endl;
27+ });
28+ }
29+
30+ std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
31+
32+ std::cout << std::endl;
33+ }
34+
35+ void TestSubmitWithFuture ()
36+ {
37+ std::cout << " === Test Submit with Future ===" << std::endl;
38+
39+ cppbase::ThreadPool pool;
40+ pool.Start (4 , 100 );
41+
42+ auto future1 = pool.Submit ([]()
43+ {
44+ std::this_thread::sleep_for (std::chrono::milliseconds (50 ));
45+ return 42 ;
46+ });
47+
48+ auto future2 = pool.Submit ([]()
49+ {
50+ return 10 + 20 ;
51+ });
52+
53+ auto future3 = pool.Submit ([]()
54+ {
55+ std::string msg = " Hello ThreadPool" ;
56+ return msg.length ();
57+ });
58+
59+ std::cout << " Future1 result: " << future1.get () << std::endl;
60+ std::cout << " Future2 result: " << future2.get () << std::endl;
61+ std::cout << " Future3 result: " << future3.get () << std::endl;
62+
63+ std::cout << std::endl;
64+ }
65+
66+ void TestAddTaskWithTimeout ()
67+ {
68+ std::cout << " === Test AddTask with Timeout ===" << std::endl;
69+
70+ cppbase::ThreadPool pool;
71+ pool.Start (1 , 2 );
72+
73+ pool.AddTask ([]()
74+ {
75+ std::this_thread::sleep_for (std::chrono::milliseconds (500 ));
76+ std::cout << " Task 1 completed" << std::endl;
77+ });
78+ pool.AddTask ([]()
79+ {
80+ std::this_thread::sleep_for (std::chrono::milliseconds (500 ));
81+ std::cout << " Task 2 completed" << std::endl;
82+ });
83+
84+ std::cout << " Queue full, trying to add with timeout..." << std::endl;
85+
86+ int ret = pool.AddTask ([]()
87+ {
88+ std::cout << " Task 3" << std::endl;
89+ }, std::chrono::milliseconds (100 ));
90+
91+ if (ret == cppbase::ErrorCode::EC_TIMEOUT )
92+ {
93+ std::cout << " AddTask timeout" << std::endl;
94+ }
95+ else if (ret == cppbase::ErrorCode::EC_OK )
96+ {
97+ std::cout << " AddTask success" << std::endl;
98+ }
99+
100+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
101+
102+ std::cout << std::endl;
103+ }
104+
105+ void TestMultiThreaded ()
106+ {
107+ std::cout << " === Test Multi-threaded ===" << std::endl;
108+
109+ cppbase::ThreadPool pool;
110+ pool.Start (4 , 100 );
111+
112+ const int numTasks = 20 ;
113+ std::atomic<int > counter{0 };
114+
115+ std::vector<std::thread> threads;
116+ for (int t = 0 ; t < 4 ; ++t)
117+ {
118+ threads.emplace_back ([&pool, &counter, t, numTasks]()
119+ {
120+ for (int i = 0 ; i < numTasks; ++i)
121+ {
122+ pool.AddTask ([&counter, t, i]()
123+ {
124+ std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
125+ counter++;
126+ });
127+ }
128+ });
129+ }
130+
131+ for (auto & th : threads)
132+ {
133+ th.join ();
134+ }
135+
136+ std::this_thread::sleep_for (std::chrono::milliseconds (500 ));
137+
138+ std::cout << " Expected: " << numTasks * 4 << " , Actual: " << counter << std::endl;
139+ std::cout << std::endl;
140+ }
141+
142+ void TestExceptionHandling ()
143+ {
144+ std::cout << " === Test Exception Handling ===" << std::endl;
145+
146+ cppbase::ThreadPool pool;
147+ pool.Start (2 , 10 );
148+
149+ auto future = pool.Submit ([]()
150+ {
151+ throw std::runtime_error (" Test exception" );
152+ return 0 ;
153+ });
154+
155+ try
156+ {
157+ future.get ();
158+ }
159+ catch (const std::exception& e)
160+ {
161+ std::cout << " Caught exception: " << e.what () << std::endl;
162+ }
163+
164+ std::cout << std::endl;
165+ }
166+
167+ int main ()
168+ {
169+ TestBasicUsage ();
170+ TestSubmitWithFuture ();
171+ TestAddTaskWithTimeout ();
172+ TestMultiThreaded ();
173+ TestExceptionHandling ();
174+
175+ std::cout << " All tests completed!" << std::endl;
176+ return 0 ;
177+ }
0 commit comments