muduo 5 網絡庫學習之MutexLock類、MutexLockGuard類、Condition類、CountDownLatch類封裝中的知識點

一、MutexLock 類

class MutexLock : boost::noncopyable

二、MutexLockGuard類

class MutexLockGuard : boost::noncopyable

三、Condition類

class Condition : boost::noncopyable

某個線程:

加鎖                                    

     while (條件)

          wait(); //1、解鎖;2、等待通知;3、得到通知返回前重新加鎖

解鎖

另一個線程:

加鎖

     更改條件

     通知notify(可以移到鎖外)

解鎖

四、CountDownLatch類

class CountDownLatch : boost::noncopyable

既可以用於所有子線程等待主線程發起 “起跑” 也可以用於主線程等待子線程初始化完畢纔開始工作

下面寫兩個程序測試一下CountDownLatch 的作用:

CountDownLatch_test1:

#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());
}

執行結果如下:

simba@ubuntu:~/Documents/build/debug/bin$ ./countdownlatch_test1 pid=2994, tid=2994 pid=2994, tid=2994 main running ... tid=2997, work thread 2 started tid=2997, work thread 2 stopped tid=2996, work thread 1 started tid=2996, work thread 1 stopped tid=2995, work thread 0 started tid=2995, work thread 0 stopped number of created threads 3 simba@ubuntu:~/Documents/build/debug/bin$ 

可以看到其他三個線程一直等到主線程睡眠完執行run(),在裏面執行latch_.countDown() 將計數減爲0,進而執行notifyall 喚醒後,纔開始執行下來。

CountDownLatch_test2:

#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);
       printf("tid=%d, %s started\n",
               CurrentThread::tid(),
               CurrentThread::name());

        latch_.countDown();
     

        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());
}

執行結果輸出如下:

simba@ubuntu:~/Documents/build/debug/bin$ ./countdownlatch_test2 pid=4488, tid=4488 tid=4491, work thread 2 started tid=4491, work thread 2 stopped tid=4490, work thread 1 started tid=4490, work thread 1 stopped tid=4489, work thread 0 started pid=4488, tid=4488 main running ... tid=4489, work thread 0 stopped number of created threads 3

可以看出當其他三個線程都啓動後,各自執行一次 latch_.countDown(),主線程wait() 返回繼續執行下去。

參考:

muduo manual.pdf

《linux 多線程服務器編程:使用muduo c++網絡庫》

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章