0%

CountDownLatch的基于对象的客户端用户使用方法

2020年6月3日 上午8:33

总结:

基于对象的方式,能够将新建线程相关的函数方法、全局变量封装在一个对象中,并且主线程main也可以通过这个类对象和新建线程交互

方式1:新建线程组等待主线程唤醒

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <muduo/base/CountDownLatch.h>
#include <muduo/base/Thread.h>

#include <boost/bind.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <string>
#include <stdio.h>

using namespace muduo;
/*
利用基于对象的编程方式,将主线程和创建的线程组进行逻辑上的分离
*/
class Test
{
public:
// 第二层新线程组:在构造函数中启动创建线程,并启动
Test(int numThreads)
: latch_(1),
threads_(numThreads)
{
for (int i = 0; i < numThreads; ++i)
{
char name[32];
snprintf(name, sizeof name, "work thread %d", i);
threads_.push_back(new muduo::Thread(
boost::bind(&Test::threadFunc, this), muduo::string(name)));
}
for_each(threads_.begin(), threads_.end(), boost::bind(&Thread::start, _1));
}
// 第二层新线程组:用于主线程中唤醒新线程
void run()
{
latch_.countDown();
}

void joinAll()
{
for_each(threads_.begin(), threads_.end(), boost::bind(&Thread::join, _1));
}

private:
// 第二层新线程组:多线程的共同执行体,会发生锁的争抢
void threadFunc()
{
latch_.wait();
printf("tid=%d, %s started\n",
CurrentThread::tid(),
CurrentThread::name());

printf("tid=%d, %s stopped\n",
CurrentThread::tid(),
CurrentThread::name());
}
// 第二层新线程组:三个新线程需要的共同的全局变量
CountDownLatch latch_;
boost::ptr_vector<Thread> threads_;
};

int main()
{
printf("pid=%d, tid=%d\n", ::getpid(), CurrentThread::tid());
// 第一层主线程:启动三个新线程
Test t(3);
sleep(3);
printf("pid=%d, tid=%d %s running ...\n", ::getpid(), CurrentThread::tid(), CurrentThread::name());
// 第一层主线程:唤醒那三个线程
t.run();
t.joinAll();

printf("number of created threads %d\n", Thread::numCreated());
}

方式2:新建线程组完成功能后将主线程进行唤醒

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <muduo/base/CountDownLatch.h>
#include <muduo/base/Thread.h>

#include <boost/bind.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <string>
#include <stdio.h>

using namespace muduo;

class Test
{
public:
Test(int numThreads)
: latch_(numThreads),
threads_(numThreads)
{
for (int i = 0; i < numThreads; ++i)
{
char name[32];
snprintf(name, sizeof name, "work thread %d", i);
threads_.push_back(new muduo::Thread(
boost::bind(&Test::threadFunc, this), muduo::string(name)));
}
for_each(threads_.begin(), threads_.end(), boost::bind(&muduo::Thread::start, _1));
}

void wait()
{
latch_.wait();
}

void joinAll()
{
for_each(threads_.begin(), threads_.end(), boost::bind(&Thread::join, _1));
}

private:

void threadFunc()
{
sleep(3);
latch_.countDown();
printf("tid=%d, %s started\n",
CurrentThread::tid(),
CurrentThread::name());



printf("tid=%d, %s stopped\n",
CurrentThread::tid(),
CurrentThread::name());
}

CountDownLatch latch_;
boost::ptr_vector<Thread> threads_;
};

int main()
{
printf("pid=%d, tid=%d\n", ::getpid(), CurrentThread::tid());
Test t(3);
t.wait();
printf("pid=%d, tid=%d %s running ...\n", ::getpid(), CurrentThread::tid(), CurrentThread::name());
t.joinAll();

printf("number of created threads %d\n", Thread::numCreated());
}