Skip to content

runtime: documentation for LockOSThread is not clear #11069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
joaonrb opened this issue Jun 4, 2015 · 1 comment
Closed

runtime: documentation for LockOSThread is not clear #11069

joaonrb opened this issue Jun 4, 2015 · 1 comment

Comments

@joaonrb
Copy link

joaonrb commented Jun 4, 2015

I plan to bind a functionality in my app to not run in parallel but without losing the concurrency solutions, much like a GIL but for a specific thread. I planned to use runtime.LockOSThread() but the documentation is unclear.

First of all is the current goroutine that will bind to a OS thread or is the next being created?

And second any goroutine created by the one locked to the OS thread will also execute exclusively to that OS thread or any other?

I used an example suggested here:

package main
// #include <pthread.h>
import "C"
import (
 "fmt"
 "runtime"
     "time"
)

func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    ch1 := make(chan bool)
    ch2 := make(chan bool)
    fmt.Println("main", C.pthread_self())
    runtime.LockOSThread()
    go func() {
        fmt.Println("locked", C.pthread_self())
        time.Sleep(1000 * time.Millisecond)
        go func() {
            time.Sleep(1000 * time.Millisecond)
            fmt.Println("locked child", C.pthread_self())
            ch1 <- true
        }()
        ch2 <- true
    }()
    <-ch1
    <-ch2
}

And I come up with the result:

main 139673634682688
locked 139673609656064
locked child 139673609656064

It looks that the second goroutine executes in the same OS thread as the first one (witch fits my needs but I don't know if is a reliable way).

@adg adg changed the title Documentation for runtime.LockOSThread() is not clear runtime: documentation for LockOSThread is not clear Jun 4, 2015
@adg
Copy link
Contributor

adg commented Jun 4, 2015

The docs for LockOSThread say

LockOSThread wires the calling goroutine to its current operating system thread.

(emphasis mine)

So in your program it is the main goroutine (the one executing the main function) that is locked to a specific OS thread (and no other goroutine may execute in that thread).

It is just a coincidence that the other two goroutines execute in the same thread. There is no facility to ensure that a group of goroutines all execute in the same thread (although the scheduler is likely smart enough to run related goroutines in the same thread).

Closing this issue because the docs are explicit about which goroutine is locked to the thread.

@adg adg closed this as completed Jun 4, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants