Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a synchronized list in golang

Tags:

concurrency

go

Firstly, forgive me if this is a stupid question. I would like to create a generic synchronised list (like in Java) for reuse in my Go projects. I found the source of Go's linked list and I was wondering would it be sufficient to simply add mutex locks to the list manipulation functions?

like image 400
W.K.S Avatar asked Sep 30 '22 03:09

W.K.S


1 Answers

If you're going to make a concurrent-safe container, you need to protect all access to the data, not just writes. Inspecting an element, or even calling Len() without synchronizing the read could return invalid or corrupt data.

It's probably easier to protect the entire data structure with a mutex, rather than implement your own concurrent linked list.

like image 97
JimB Avatar answered Oct 03 '22 01:10

JimB