FAQ
Hi all,

I'm busy with a project that requires a map accessed by multiple
go-routines. Since maps aren't thread-safe, I implement my own locks.

I've come up with 3 variations:

1. A goroutine handles the map, with a _get_ and a _set_ channel.

2. A goroutine handles the map with a single channel, then type-checks to
decide whether the request is a _get_ or a _set_.

3. Use _sync.RWLock_ to coordinate map access. No goroutines.

I've implemented all three, along with some performance testing:

https://gist.github.com/craigmj/5770480

Here's the thing: using sync is about twice as fast as using a goroutine.
An example output:

         In parallel on 8 CPUs with 10 goroutines
         GoMap: 3.289649085s
         GoMap1Chan: 2.911694845s
         SyncMap: 1.393160905s

So my questions:

1. Can anyone see any obvious problems with my code? Have I made a mistake?

2. Is there a better way?

3. If there's no better way, is this just a situation where goroutines are
not the best solution?

All the best,
Craig

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 6 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 13, '13 at 1:17a
activeJun 13, '13 at 5:27a
posts6
users6
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase