I propose a new function: runtime.RunWithMainThreadOnly(). This would allow you to run code with only the main (initial) OS thread: all the other OS threads would be destroyed. Once this code section completes, the runtime would be able to create OS threads again, which would now inherit any state changes.
I'm not in favour of this, Go programmers are isolated from threads by
design. I understand that there are unfortunate realities of
interaction with operating systems, open gl libraries, etc, that make
a mockery of this ideal, but they are unfortunate exceptions, not
justifications for an invasive change like this.
There are also other aspects to consider.
- This feature feels very linux specific, how do these concepts map
onto other platforms that Go supports like windows or iOS?
- Goroutines are hosted on threads not just for user process, but also
runtime processes like garbage collection, finalisation, the
background scavenger, timers, network poller, and so on. Leaking a
thread with modified properties into this mix would be a disaster, so
something akin to LockOSThread would still be needed to make sure that
thread was fenced off.
You've gone into some detail about how this would be implemented, but
I haven't seen justification for why you need it. If this feature was
implemented, how would you use it ?
Hi, all. There are a number of system calls which change the state of the calling thread. Examples include setpriority(2) and setns(2). In the C world, it's easy to make such changes prior to starting any threads, and thus all threads inherit the state of the initial (main) thread. In this way, one can change the state of the entire process, which is usually what is desired.
In Go, this is not possible, since by the time the main() function is called, the runtime has created multiple OS threads. Thus, these calls change the state of the OS thread that happened to be running the main goroutine at the time. At any time the main goroutine can hop between OS threads, with the result that the process state appears to switch back and forth randomly.
My current work-around for this is the double-exec trick: I call runtime.LockOSThread(), make my state-changing system calls and then re-exec with syscall.Exec(), passing an extra command-line flag to disable the state-change+exec. This is a bit horrible. I get away with it because my application only needs to make these changes at startup. If I needed to effect a process-wide state change later on - say based on a decision that happens sometime later - I'd basically be stuck, because I can't afford to throw away all the internal state that's been built up.
I propose a new function: runtime.RunWithMainThreadOnly(). This would allow you to run code with only the main (initial) OS thread: all the other OS threads would be destroyed. Once this code section completes, the runtime would be able to create OS threads again, which would now inherit any state changes.
I hadn't thought of this approach for https://golang.org/issue/1435;
it would be nice if we could make it work.