4 ms·
How are goroutines not threads? Do you mean because it's possible for them to communicate without shared mutable state? Edit: Oh, apparently you all mean OS th
by abecedarius 14y ago
How are goroutines not threads? Do you mean because it's possible for them to communicate without shared mutable state?
Edit: Oh, apparently you all mean OS threads. So say so. (For example, in Haskell they're called threads without any implication that each one is an OS thread. Haskell's not unusual that way.)
- dlsspy 14y agogoroutines are not threads because thread means something and goroutine means something else.
- lbarrow 14y agoThread usually means an in-process concurrency construct controlled by calls to the operating system. A goroutine is managed by the Go runtime. It's an important distinction between using many (read: hundreds) of OS threads in a single process isn't performant, while using hundreds or even tens of thousands of goroutines is just fine.
- michaelfairley 14y agoGoroutines are green threads[1], and thus are threads, just like Ruby 1.8s threads are threads. Also, you can easily run 10s of thousands of kernel threads (provided you decrease the threads' stack size). 1: http://en.wikipedia.org/wiki/Green_threads http://en.wikipedia.org/wiki/Green_threads
- AYBABTME 14y agoGo schedules its own 'threads' (goroutines) to run on one or many (OS) threads. In this case, threads refer to not-a-process units of execution in an operating system. For whatever its worth, Wikipedia defines threads as: the smallest sequence of programmed instructions that can be managed independently by an operating system scheduler. I think that's also the commonly accepted definition: that threads are related to the OS scheduler.
- tptacek 14y agoThe notion that goroutines aren't threads seems to exist primarly because Golang also makes use of OS-native threads in order to schedule goroutines, and so needs to draw a distinction in order to explain how the runtime works. But that's just a detail. In reality, goroutines are threads; they're just userland, non-preemptive threads. Similar constructions have been available, even to C programmers, for well over a decade (and probably much longer).
- furyofantares 14y ago> Similar constructions have been available, even to C programmers, for well over a decade (and probably much longer). Of course they aren't called threads there, either (probably for the same reason as they aren't called threads in Go)
- tptacek 14y agoSure they are. For instance, Gnu Pth. Programming language support for threading predates direct operating system support (at least in mainstream operating systems) by a lot of years, from what I can tell.
- furyofantares 14y agoThe ones I've used have just been called coroutines, though I suppose I haven't used any that function as drop-in replacements for threads like Pth
- georgemcbay 14y agoIt is really common for Go programmers (I've even seen members of the Go core team do it) to say 'thread' when they mean goroutine. It is important to recognize there is a difference, but calling someone out for saying 'thread' when they mean 'goroutine' is pedantic bullshit on the order of calling someone out for calling a class method in an OO language a function.