Making concurrent programs to run parallely in Go

139 views
Skip to first unread message

Abeshek R

unread,
Jun 15, 2022, 12:10:01 PM6/15/22
to golang-nuts
Hi,
I'm trying to make a key-value store like LevelDB from scratch for learning about database internals. I got stuck in a place where I need to flush some data to disk file parallely while the main thread is executing reads and writes. I tried writing it as a go-routine but as I understand it, the go-routine (for flushing data to disk) starts executing but blocks at file write and switches back to main go-routine. 

When I tried to print how many cores go runtime was using with runtime.NumCPU(), I got 12 as the result. But still I had to use sync.WaitGroup to actually make the main go-routine wait before this file write happens.

I'm relatively new to writing concurrent programs in Go. Is there any way to make it execute paralley?

Thanks

Brian Candler

unread,
Jun 15, 2022, 12:41:17 PM6/15/22
to golang-nuts
As I understand, it will execute in parallel by default.  There's a pool of threads for running goroutines, defaulting initially to the number of cores. If a syscall blocks, then other threads will continue to run - and (I think) an extra thread is started too.

> But still I had to use sync.WaitGroup to actually make the main go-routine wait before this file write happens.

Doesn't that prove that the main goroutine and the file write/flush goroutine are taking place in parallel?

Anderson Queiroz

unread,
Jun 16, 2022, 12:10:04 PM6/16/22
to golang-nuts
You will need a barrier, the wait Group in your case, to guarantee the main goroutine will only continue after all the others have finished their work. Besides if any of your goroutines blocks due to IO the Go scheduler will look for another goroutine to run while the blocked one waits.
Another thing is that you don't know which goroutine will run, you cannot force the Go scheduler to run one goroutine and keep other waiting, for that you need to synchronize them.
I don't fully understand what is your problem, you solution is correct, having a barrier to avoid the main goroutine moving forward than it should. 
Another thing, Concurrency is not parallelism, goroutines will run concurrently, due to the multi-core processors we have, they will likely run in parallel, but this does not guarantees 2 gorounites started at the same time will run in parallel. I don't think you can force goroutines to run in parallel, you can synchronize to ensure the correct behaviour of your program.

Best,
Anderson
Reply all
Reply to author
Forward
0 new messages