• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Goroutines on the same thread

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an application where I use goroutines, they communicate with each other using channels, and simple calculations. I put it on Kubernetes where I have set 1CPU - if I understand correctly it is one thread. And here everything is fine. When I add another CPU, however, I do not see a significant acceleration. And if I understand this correctly, what is happening is that the scheduler is splitting goroutines between two threads. And here the golang power goes to hell because you have to jump between threads anyway. Is there any way to have all the goroutines that communicate with each other fired on the same thread? I don't know if I explained well, what I mean. I hope someone can understand it. From what I know in Rust Actix it is possible to do something like this.



----------------------
Ask me how to design thinking business strategy

 
Saloon Keeper
Posts: 15714
367
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

If you want them all to run on the same thread, why are you using goroutines in the first place?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Coroutines" don't necessarily need to use different threads.
When a goroutine does some async I/O or "delay" or waiting for some "deferred", it does not use its thread, so it can share the thread with other goroutines.
The benefit of that:
1. Saving threads
2. If you run all your "concurrent" tasks from a single thread, you do have to worry less about race conditions. Less locks.

In languages like Kotlin, you define the "context" of the coroutines. One of its elements is the thread pool from which threads.
Examples: On mobile dev, for example, you need to do most of the UI on the "main thread" - so you use a context with a thread pool that always gives the main thread.
You can also use a Dispatcher.IO to dispatch coroutines from a big thread pool, for I/O tasks.


 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic