MCPcopy Create free account
hub / github.com/ChunelFeng/CThreadPool / tutorial_threadpool_1

Function tutorial_threadpool_1

tutorial.cpp:15–36  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13using namespace CTP;
14
15void tutorial_threadpool_1(UThreadPoolPtr tp) {
16 /**
17 * 依次向线程池中传入:
18 * 1、普通函数
19 * 2、静态函数
20 * 3、类成员函数
21 * 4、类成员静态函数
22 */
23 int i = 6, j = 3;
24 std::string str = "5";
25 MyFunction mf;
26
27 auto r1 = tp->commit([i, j] { return add(i, j); }); // 可以通过lambda表达式传递函数
28 std::future<float> r2 = tp->commit(std::bind(minusBy5, 8.5f)); // 可以传入任意个数的入参
29 auto r3 = tp->commit(std::bind(&MyFunction::concat, mf, str)); // 返回值可以是任意类型
30 std::future<int> r4 = tp->commit([i, j] { return MyFunction::multiply(i, j); }); // 返回值实际上是std::future<T>类型
31
32 std::cout << r1.get() << std::endl; // 返回值可以是int类型
33 std::cout << r2.get() << std::endl; // 等待r2对应函数执行完毕后,再继续执行。不调用get()为不等待
34 std::cout << r3.get() << std::endl; // 返回值也可是string或其他任意类型
35 std::cout << r4.get() << std::endl;
36}
37
38
39void tutorial_threadpool_2(UThreadPoolPtr tp) {

Callers 1

mainFunction · 0.85

Calls 1

addFunction · 0.85

Tested by

no test coverage detected