Go語言條件變數的兩個例子

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。在Go語言中 sync.Cond 代表條件變數,但它需要配置鎖才能有用.

var m Mutex 

c := NewCond(&m)

或
c := sync.NewCond(&sync.RWMutex{})
之類.
它有三個函數: wait/signal/broadcast 
望文知義,和Windows下的InitializeConditionVariable與WaitForSingleObject()之類,
及Linux下的pthread_cond_t等作用差不多.

弄了兩個例子:

/*條件變數 Cond 例子Author: xclDate: 2015-11-29*/package mainimport ("fmt""runtime""sync""time")func main() {runtime.GOMAXPROCS(4)test333()}func testCond() {c := sync.NewCond(&sync.Mutex{})condition := falsego func() {time.Sleep(time.Second * 1)c.L.Lock()fmt.Println("[1] 變更condition狀態,並發出變更通知.")condition = truec.Signal() //c.Broadcast()fmt.Println("[1] 繼續後續處理.")c.L.Unlock()}()c.L.Lock()fmt.Println("[2] condition..........1")for !condition {fmt.Println("[2] condition..........2")//等待Cond訊息通知c.Wait()fmt.Println("[2] condition..........3")}fmt.Println("[2] condition..........4")c.L.Unlock()fmt.Println("main end...")}/*testCond()運行結果:[2] condition..........1[2] condition..........2[1] 變更condition狀態,並發出變更通知.[1] 繼續後續處理.[2] condition..........3[2] condition..........4main end...*/
例二:

/*條件變數 Cond 例子Author: xclDate: 2015-11-29*/package mainimport ("fmt""runtime""sync""time")const MAX_CLIENTS = 3func main() {runtime.GOMAXPROCS(4)testCond()}func testCond() {s := NewServer()go s.IOloop()time.Sleep(time.Second * 1)go func() {s.Release()}()go func() {s.Release()}()time.Sleep(time.Second * 1)s.Release()time.Sleep(time.Second * 1)fmt.Println("[testCond] end.")}type Server struct {clients uint64cond    *sync.Cond}func NewServer() *Server {s := &Server{}s.cond = sync.NewCond(&sync.Mutex{})return s}func (s *Server) IOloop() {for {s.cond.L.Lock()for s.clients == MAX_CLIENTS {fmt.Println("[IOloop] 等於MAX_CLIENTS了,等待Cond通知.即有觸發Release()")s.cond.Wait()}s.cond.L.Unlock()s.clients++fmt.Println("[IOloop] clients:", s.clients)}}func (s *Server) Release() {s.cond.L.Lock()s.clients--fmt.Println("[Release] a clients:", s.clients)s.cond.Signal()fmt.Println("[Release] b clients:", s.clients)s.cond.L.Unlock()}/*運行結果:[IOloop] clients: 1[IOloop] clients: 2[IOloop] clients: 3[IOloop] 等於MAX_CLIENTS了,等待Cond通知.即有觸發Release()[Release] a clients: 2[Release] b clients: 2[Release] a clients: 1[Release] b clients: 1[IOloop] clients: 2[IOloop] clients: 3[IOloop] 等於MAX_CLIENTS了,等待Cond通知.即有觸發Release()[Release] a clients: 2[Release] b clients: 2[IOloop] clients: 3[IOloop] 等於MAX_CLIENTS了,等待Cond通知.即有觸發Release()[testCond] end.*/


對於條件變數和channl,知乎有個問答很精彩,可以看看: http://www.zhihu.com/question/27256570

另外 <<Go語言並發編程>>中也有個同一時間多個Goroutine分別進行對一個檔案進行讀寫操作的例子也很精彩,直觀。


噢,對了,附上C++11條件變數的使用例子:

// condition_variable example#include <iostream>           // std::cout#include <thread>             // std::thread#include <mutex>              // std::mutex, std::unique_lock#include <condition_variable> // std::condition_variablestd::mutex mtx;std::condition_variable cv;bool ready = false;void print_id (int id) {  std::unique_lock<std::mutex> lck(mtx);  while (!ready) cv.wait(lck);  // ...  std::cout << "thread " << id << '\n';}void go() {  std::unique_lock<std::mutex> lck(mtx);  ready = true;  cv.notify_all();}int main (){  std::thread threads[10];  // spawn 10 threads:  for (int i=0; i<10; ++i)    threads[i] = std::thread(print_id,i);  std::cout << "10 threads ready to race...\n";  go();                       // go!  for (auto& th : threads) th.join();  return 0;}
C++11例子來自: http://www.cplusplus.com/reference/condition_variable/condition_variable/

BLOG: http://blog.csdn.net/xcl168


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.