<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    Boost Thread學習筆記四

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    barrier
    barrier類的接口定義如下:

     1 class barrier : private boost::noncopyable   // Exposition only
     2 {
     3 public:
     4   // construct/copy/destruct
     5   barrier(size_t n);
     6   ~barrier();
     7 
     8   // waiting
     9   bool wait();
    10 };


    barrier類為我們提供了這樣一種控制線程同步的機制:
    前n - 1次調用wait函數將被阻塞,直到第n次調用wait函數,而此后第n + 1次到第2n - 1次調用wait也會被阻塞,直到第2n次調用,依次類推。
    barrier::wait的實現十分簡單:

     1 barrier::barrier(unsigned int count)
     2     : m_threshold(count), m_count(count), m_generation(0)
     3 {
     4     if (count == 0)
     5         throw std::invalid_argument("count cannot be zero.");
     6 }
     7 
     8 bool barrier::wait()
     9 {
    10     boost::mutex::scoped_lock lock(m_mutex);    // m_mutex is the base of barrier and is initilized by it's default constructor.
    11     unsigned int gen = m_generation;    // m_generation will be 0 for call 1~n-1, and 1 for n~2n - 1, and so onRFID設備管理軟件
    12 
    13     if (--m_count == 0)
    14     {
    15         m_generation++;    // cause m_generation to be changed in call n/2n/RFID設備管理軟件
    16         m_count = m_threshold;    // reset count
    17         m_cond.notify_all();    // wake up all thread waiting here
    18         return true;
    19     }
    20 
    21     while (gen == m_generation)    // if m_generation is not changed, lock current thread.
    22         m_cond.wait(lock);
    23     return false;
    24 }


    因此,說白了也不過是mutex的一個簡單應用。
    以下是一個使用barrier的例子:

     1 #include <boost/thread/thread.hpp>
     2 #include <boost/thread/barrier.hpp>
     3 
     4 int i = 0;
     5 boost::barrier barr(3);    // call barr.wait 3 * n times will release all threads in waiting
     6 
     7 void thread()
     8 {
     9     ++i;
    10     barr.wait();
    11 }
    12 
    13 int main()
    14 {
    15     boost::thread thrd1(&thread);
    16     boost::thread thrd2(&thread);
    17     boost::thread thrd3(&thread);
    18 
    19     thrd1.join();
    20     thrd2.join();
    21     thrd3.join();
    22 
    23     return 0;
    24 }


    如果去掉其中thrd3相關的代碼,將使得線程12一直處于wait狀態,進而使得主線程無法退出。

    xtime
    xtime是boost::thread中用來表示時間的一個輔助類,它是一個僅包含兩個成員變量的結構體:

    1 struct xtime
    2 {
    3 //RFID設備管理軟件
    4     xtime_sec_t sec;
    5     xtime_nsec_t nsec;
    6 };


    condition::timed_wait、thread::sleep等涉及超時的函數需要用到xtime。
    需要注意的是,xtime表示的不是一個時間間隔,而是一個時間點,因此使用起來很不方便。為了方便使用xtime,boost提供了一些輔助的xtime操作函數,如xtime_get、xtime_cmp等。
    以下是一個使用xtime來執行sleep的例子(跟簡單的一句Sleep比起來,實在是太復雜了),其中用到了xtime初始化函數xtime_get:

     1 #include <boost/thread/thread.hpp>
     2 #include <boost/thread/xtime.hpp>
     3 #include <iostream>
     4 
     5 int main()
     6 {
     7     boost::xtime xt;
     8     boost::xtime_get(&xt, boost::TIME_UTC);    // initialize xt with current time
     9     xt.sec += 1;    // change xt to next second
    10     boost::thread::sleep(xt);    // do sleep
    11 
    12     std::cout << "1 second sleep over." << std::endl;
    13 
    14     return 0;
    15 } RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全