What is sync.Pool in golang and How to use it
sync.Pool (1/2)
Many Go libraries include custom thread-safe free lists, like this:
var objPool = make(chan *Object, 10)
func obj() *Object {
select {
| import UIKit | |
| class DumbViewController: UIViewController, UITableViewDataSource { | |
| var items: [Int] = [Int]() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| self.view.backgroundColor = UIColor.whiteColor() |
| <?php | |
| class db{ | |
| static $count=0;//current data | |
| static $undos=array();//stack | |
| } | |
| function add($n){ | |
| db::$count+=$n; | |
| db::$undos[]=function() use($n){ | |
| db::$count-=$n; |
What is sync.Pool in golang and How to use it
sync.Pool (1/2)
Many Go libraries include custom thread-safe free lists, like this:
var objPool = make(chan *Object, 10)
func obj() *Object {
select {
| /* | |
| A Goroutine safe pattern using channels that abstracts away the channels | |
| This is a concept of creating a goroutine safe object that uses channels under the covers to communicate | |
| with the internal map[string]string structure. I know that typically this kind of solution may done | |
| with mutexes but the excercise was in using channels on purpose although they are heavier. | |
| Note a couple of points: | |
| - When using channels, you can still build a public-facing api that nicely abstracts them away, therefore |
| package main | |
| import ( | |
| "log" | |
| "time" | |
| "sync" | |
| ) | |
| const NUM = 2 | |
| var finished = make(chan bool, NUM) |
| <?php | |
| include __DIR__ . '/threads.php'; | |
| $commands = array(); | |
| for ( $i=0; $i<10; $i++ ) { | |
| $commands[] = "bash -c 'sleep `shuf -i 1-5 -n 1`; echo $i'"; | |
| } |
| package main | |
| import ( | |
| "archive/zip" | |
| "io" | |
| "log" | |
| "os" | |
| "path/filepath" | |
| ) |
| // Long Polling (Recommened Technique - Creates An Open Connection To Server ∴ Fast) | |
| (function poll(){ | |
| $.ajax({ url: "server", success: function(data){ | |
| //Update your dashboard gauge | |
| salesGauge.setValue(data.value); | |
| }, dataType: "json", complete: poll, timeout: 30000 }); | |
| })(); |