# Why are goroutines better than traditional pthreads?

**URL:** <https://forum.golangbridge.org/t/why-are-goroutines-better-than-traditional-pthreads/17445>\
**Category:** Technical Discussion\
**Created:** [February 6, 2020, 11:45pm UTC](https://forum.golangbridge.org/t/why-are-goroutines-better-than-traditional-pthreads/17445 "2020-02-06T23:45:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BullardLA](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@BullardLA](https://forum.golangbridge.org/u/BullardLA)\
**Post date:** [February 6, 2020, 11:45pm UTC](https://forum.golangbridge.org/t/why-are-goroutines-better-than-traditional-pthreads/17445/1 "2020-02-06T23:45:17Z")

</div>

Ignoring differences in the default stack size/memory, why are goroutines any better than pthreads? Goroutines still result in high amounts of context switches. If I’m running a fasthttp server that does nothing but respond with a `200` status code, I’m seeing `~250,000 ctxsw/s` (measured with vmstat) when I reach `~200,000` requests per second (8 cores, GOMAXPROCS=8). I see people talk about goroutine context switches being less expensive, but I haven’t seen any data on that - has that been proven to be true?

tl;dr - Memory aside (RAM is cheap), are goroutines better than concurrency models in other languages that map onto pthreads? Why?

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nobbz/32/3096_2.png) [@NobbZ](https://forum.golangbridge.org/u/NobbZ)\
**Post date:** [February 7, 2020, 8:31am UTC](https://forum.golangbridge.org/t/why-are-goroutines-better-than-traditional-pthreads/17445/2 "2020-02-07T08:31:53Z")

</div>

Goroutines are cheaper to create and do not carry any OS overhead.

---

<div class="post-metadata">

**Author:** ![lutzhorn](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/lutzhorn/32/8119_2.png) [@lutzhorn](https://forum.golangbridge.org/u/lutzhorn)\
**Post date:** [February 7, 2020, 8:38am UTC](https://forum.golangbridge.org/t/why-are-goroutines-better-than-traditional-pthreads/17445/3 "2020-02-07T08:38:09Z")

</div>

From the perspective of a programmer the concept of Goroutines ist much easier to understand than the concept of threads. If you follow [the advice](https://blog.golang.org/share-memory-by-communicating)

> Do not communicate by sharing memory; instead, share memory by communicating.

you can avoid a whole class of problems that bite you with threads.

---

<div class="post-metadata">

**Author:** ![BullardLA](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@BullardLA](https://forum.golangbridge.org/u/BullardLA)\
**Post date:** [February 7, 2020, 5:45pm UTC](https://forum.golangbridge.org/t/why-are-goroutines-better-than-traditional-pthreads/17445/4 "2020-02-07T17:45:17Z")

</div>

> Goroutines are cheaper to create

That’s true

> and do not carry any OS overhead.

But is this? If I’m running a program that simply has two goroutines, with one sending something to a channel that the other is listening on in an endless loop, I’m observing hundreds of thousands of OS context switches and each one seems to be taking an average of two microseconds. This doesn’t differ much from context switch times I measure in Java between threads.

I am aware that the general consensus is that Goroutines don’t carry much, if any, OS overhead, and I want to believe that, but that differs from the data I’m observing in my testing.

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nobbz/32/3096_2.png) [@NobbZ](https://forum.golangbridge.org/u/NobbZ)\
**Post date:** [February 7, 2020, 5:52pm UTC](https://forum.golangbridge.org/t/why-are-goroutines-better-than-traditional-pthreads/17445/5 "2020-02-07T17:52:41Z")

</div>

Context switches do not happen in the OS, they happen on the CPU.

You can’t do anything against them.

Goroutines between each other know how to communicate, the scheduler does as well and therefore can optimise scheduling based on this knowledge.

If you were doing this via memory observations between is threads/processes the knowledge were not available, communication were opaque to the scheduler and it had to give every thread a timeslot every now and then, just causing the thread to hand it back again (or if programmed badly wait busily).

---

<div class="post-metadata">

**Author:** ![BullardLA](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@BullardLA](https://forum.golangbridge.org/u/BullardLA)\
**Post date:** [February 7, 2020, 7:14pm UTC](https://forum.golangbridge.org/t/why-are-goroutines-better-than-traditional-pthreads/17445/6 "2020-02-07T19:14:32Z")

</div>

> Goroutines between each other know how to communicate, the scheduler does as well and therefore can optimise scheduling based on this knowledge.

To me, the appeal of goroutines seems to be convenience/memory, and not necessarily an improvement in performance, as @lutzhorn said. And I agree that - with channels and other paradigms offered with Go concurrency - goroutines are very intuitive and easy to grasp.

In my fasthttp vs Vert.x example, when I notice (slightly) more context switches being done by fasthttp and net/http then by Vert.x/Firenio/Undertow/Wizzardo when handling the same throughput and doing the same work, and when context switches are arguably the main reason cited when comparing pthreads to goroutines, are goroutines really reducing system CPU time compared to pthreads?

As a disclaimer, I prefer goroutines significantly more than traditional pthread-based concurrency models. They’re easy to understand and use, and with 2KB per thread (compared to ~64KB per thread minimum in Java), it’s hard to argue that goroutines aren’t objectively better than pthreads.

But from a performance perspective, ignoring RAM constraints, are goroutines better when internally they use similar, syscall heavy paradigms as eventloop based models?

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [May 7, 2020, 7:14pm UTC](https://forum.golangbridge.org/t/why-are-goroutines-better-than-traditional-pthreads/17445/7 "2020-05-07T19:14:34Z")

</div>

Closed on May 7, 2020, 7:14pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
