Go语言条件变量的两个例子
2015-11-29 19:09
393 查看
在Go语言中 sync.Cond 代表条件变量,但它需要配置锁才能有用.
var m Mutex
c := NewCond(&m)
或
c := sync.NewCond(&sync.RWMutex{})
之类.
它有三个函数: wait/signal/broadcast
望文知义,和Windows下的InitializeConditionVariable与WaitForSingleObject()之类,
及Linux下的pthread_cond_t等作用差不多.
弄了两个例子:
对于条件变量和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_variable
std::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
var m Mutex
c := NewCond(&m)
或
c := sync.NewCond(&sync.RWMutex{})
之类.
它有三个函数: wait/signal/broadcast
望文知义,和Windows下的InitializeConditionVariable与WaitForSingleObject()之类,
及Linux下的pthread_cond_t等作用差不多.
弄了两个例子:
/* 条件变量 Cond 例子 Author: xcl Date: 2015-11-29 */ package main import ( "fmt" "runtime" "sync" "time" ) func main() { runtime.GOMAXPROCS(4) test333() } func testCond() { c := sync.NewCond(&sync.Mutex{}) condition := false go func() { time.Sleep(time.Second * 1) c.L.Lock() fmt.Println("[1] 变更condition状态,并发出变更通知.") condition = true c.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..........4 main end... */例二:
/* 条件变量 Cond 例子 Author: xcl Date: 2015-11-29 */ package main import ( "fmt" "runtime" "sync" "time" ) const MAX_CLIENTS = 3 func 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 uint64 cond *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_variable
std::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
相关文章推荐
- SQLSERVER 中GO的作用详解
- 在Go语言程序中使用gojson来解析JSON格式文件
- 举例详解Go语言中os库的常用函数用法
- Go语言中函数的参数传递与调用的基本方法
- 深入解析Go语言的io.ioutil标准库使用
- GO语言的IO方法实例小结
- Go语言的os包中常用函数初步归纳
- Go语言中数组的基本用法演示
- C++中自定义sleep、条件变量sleep实例
- Linux线程管理必备:解析互斥量与条件变量的详解
- 基于条件变量的消息队列 说明介绍
- GO语言类型转换和类型断言实例分析
- 深入解析Go语言编程中的递归使用
- 初步解读Golang中的接口相关编写方法
- Go语言实现的最简单数独解法
- 详解Golang编程中的常量与变量
- Go实现比较时间大小
- 深入剖析Go语言编程中switch语句的使用
- 简单讲解Go程序中使用MySQL的方法
- Go语言的Windows下环境配置以及简单的程序结构讲解