goroutine

Append not thread-safe?

落花浮王杯 提交于 2019-12-05 20:05:23
问题 I noticed that if I tried appending to a slice using goroutines inside a for loop, there would be instances where I would get missing/blank data: destSlice := make([]myClass, 0) var wg sync.WaitGroup for _, myObject := range sourceSlice { wg.Add(1) go func(closureMyObject myClass) { defer wg.Done() var tmpObj myClass tmpObj.AttributeName = closureMyObject.AttributeName destSlice = append(destSlice, tmpObj) }(myObject) } wg.Wait() Sometimes, when I print all AttributeName s from destSlice ,

How to kill goroutines? [duplicate]

老子叫甜甜 提交于 2019-12-05 18:52:33
This question already has an answer here: cancel a blocking operation in Go 2 answers I would like to know how to kill/stop goroutines. All examples are based on channels and select, which seems to only work if the goroutine contains some repeating task between which it can listen on a channel. Is there a way to stop the below goroutine before it returns? package main import ( "time" ) func main() { stop := make(chan string, 1) go func() { time.Sleep(10 * time.Second) stop <- "stop" return }() <- stop } Is there a way to stop the below goroutine before it returns? No there is not (except

How does Go decide when to context switch between goroutines?

百般思念 提交于 2019-12-05 07:53:05
I am curious as to how the Go language schedules goroutines. Does it switch only during channel requests and I/O or does it have a periodic coroutine switching loop? Go does not have a preemptive scheduler yet, but one is planned for 1.2 . So no, Go will not switch context during CPU-only calculations, only during I/O (reading from memory is also considered I/O if it isn't in a register already). You can read some discussion about it in Issue 543 - preemptive scheduling . 来源: https://stackoverflow.com/questions/18545595/how-does-go-decide-when-to-context-switch-between-goroutines

Time response for HTTP GET request when using goroutines

假如想象 提交于 2019-12-05 05:48:19
问题 I have a simple code that prints GET response time for each URL listed in a text file (url_list.txt). When the requests are fired sequentially the returned times correspond to the expected response times of individual URLs. However, when the same code is executed concurrently the returned response times are typically higher than expected. It seems that the time_start I capture before the http.Get(url) is called is not the time of when the request is actually sent. I guess the execution of

count / display the number of active goroutines

不打扰是莪最后的温柔 提交于 2019-12-05 02:04:21
I have a queue and a function that does both dequeueing and enqueueing. I want to make sure that the right amount of goroutines operate on the queue, as long as there is something in the list. This is the code I am using, but I was wondering if there is a way of printing the amount of currently active goroutines Link to playground var element int func deen(queue chan int) { element := <-queue fmt.Println("element is ", element) if element%2 == 0 { fmt.Println("new element is ", element) queue <- (element*100 + 11) queue <- (element*100 + 33) } } func main() { queue := make(chan int, 10) queue

Shared memory vs. Go channel communication

拈花ヽ惹草 提交于 2019-12-04 23:58:52
One of Go's slogans is Do not communicate by sharing memory; instead, share memory by communicating . I am wondering whether Go allows two different Go-compiled binaries running on the same machine to communicate with one another (i.e. client-server), and how fast that would be in comparison to boost::interprocess in C++? All the examples I've seen so far only illustrate communication between same-program routines. A simple Go example (with separate client and sever code) would be much appreciated! One of the first things I thought of when I read this was Stackless Python. The channels in Go

why this code about golang goruntine running order is “2” first

两盒软妹~` 提交于 2019-12-04 19:32:35
package main import ( "fmt" "sync" ) func main() { runtime.GOMAXPROCS(1) w := &sync.WaitGroup{} w.Add(2) go func() { fmt.Println("1") w.Done() }() go func() { fmt.Println("2") w.Done() }() w.Wait() } https://play.golang.org/p/ESi1mKAo1x_S eh,I don't know why the "2" is print first. I want to check the information.But I don't know what's information should I check.So I post the question there for help. I think the first goroutine is the first one push in queue。The it should be print first. You're not synchronizing your 2 launched goroutines to each other , so there is no guarantee in what order

Is there some elegant way to pause & resume any other goroutine in golang?

感情迁移 提交于 2019-12-04 08:07:55
问题 In my case, I have thousands of goroutines working simultaneously as work() . I also had a sync() goroutine. When sync starts, I need any other goroutine to pause for a while after sync job is done. Here is my code: var channels []chan int var channels_mutex sync.Mutex func work() { channel := make(chan int, 1) channels_mutex.Lock() channels = append(channels, channel) channels_mutex.Unlock() for { for { sync_stat := <- channel // blocked here if sync_stat == 0 { // if sync complete break } }

Golang: Why os.Exit doesn't work inside goroutines

巧了我就是萌 提交于 2019-12-04 06:14:23
I have a research program with very simple algorithm. When success is coming goroutine should be close (end) via os.Exit(0). I'm wait one day, two day.... What? :) Here is the simple code package main import "os" func main() { for { go func() { os.Exit(0) }() } } And my questions: Why os.Exit doesn't terminate the goroutine? What is correct way to terminate (stop) goroutine execute? Playground: http://play.golang.org/p/GAeOI-1Ksc You've run into a sticky corner of the Go scheduler. The answer is that os.Exit does cause the entire process to exit, but the way you had it, the goroutines were

Forcing goroutines into the same thread

你。 提交于 2019-12-04 03:10:46
Is there a way to ensure that a goroutine will run only in a specific OS thread? For example, when GUI operations must run in the GUI thread, but there might be multiple goroutines running GUI code. GOMAXPROCS(1) does the job technically, but that defeats the purpose of multithreading. LockOSThread() works too, but that prevents any other goroutine from running in that thread as well. Is there a way to do this, or must everything that requires the same thread also run in the same goroutine? To the best of my knowledge, not currently. I think the 'go-like' way to do this would be to write a