午夜国产理论片中文飘花|97在线起碰视频|在线观看免费网站看v片|欧美日韩在线视频一区

  1. <delect id="frdys"></delect>
  2. <delect id="frdys"></delect>
  3. <optgroup id="frdys"><ruby id="frdys"><dfn id="frdys"></dfn></ruby></optgroup>
  4. <pre id="frdys"><dd id="frdys"></dd></pre>
  5. <strike id="frdys"><blockquote id="frdys"><center id="frdys"></center></blockquote></strike>
      <delect id="frdys"><style id="frdys"><track id="frdys"></track></style></delect>
      知識庫 > 多線程之間如何實現(xiàn)通訊?

      多線程之間如何實現(xiàn)通訊?

      多線程之間如何實現(xiàn)通訊?

      999人瀏覽
      石塘網(wǎng)
      相關(guān)欄目: 知識庫
      最新回答 2023-05-15 15:57:07
      分享
      共有1條回答
      張羽赫

      一:兩個進(jìn)程間的兩個線程通信,相當(dāng)于進(jìn)程間通信

      二:一個進(jìn)程中的兩個線程間通信

        通信方式:

      1.互斥鎖

        mutex;

        lock_guard (在構(gòu)造函數(shù)里加鎖,在析構(gòu)函數(shù)里解鎖)

        unique_lock?自動加鎖、解鎖

      2.讀寫鎖

        shared_lock

      3.信號量

        c++11中未實現(xiàn),可以自己使用mutex和conditon_variable 實現(xiàn)

        代碼實現(xiàn)如下:

          

      #pragma once

      #include <mutex>

      #include <condition_variable>

      class Semaphore

      {

      public:

      ?explicit Semaphore(unsigned int count); //用無符號數(shù)表示信號量資源?

      ?~Semaphore();

      public:

      ?void wait();

      ?void signal();

      private:

      ?int m_count; //計數(shù)器必須是有符號數(shù)?

      ?std::mutex m_mutex;

      ?std::condition_variable m_condition_variable;

      };

      #include "Semaphore.h"

      Semaphore::Semaphore(unsigned int count) :m_count(count) {

      }

      Semaphore::~Semaphore()

      {

      }

      void Semaphore::wait() {

      ?std::unique_lock<std::mutex> unique_lock(m_mutex);

      ?--m_count;

      ?while (m_count < 0) {

      ??m_condition_variable.wait(unique_lock);

      ?}

      }

      void Semaphore::signal() {

      ?std::lock_guard<std::mutex> lg(m_mutex);

      ?if (++m_count < 1) {

      ??m_condition_variable.notify_one();

      ?}

      }

      4.條件變量

        condition_variable

      登錄后才能進(jìn)行回答
       
      關(guān)注石塘網(wǎng)
      關(guān)注我們