> On 5 Jul 2016, at 10:38, Pierre Durand <pierredur...@gmail.com> wrote:
> 
> Hello!
> 
> My code: https://play.golang.org/p/pg-p17UuEW
> I'm trying to append lines to a file concurrently.
> 
> My first write() function is buggy, because WriteString() are called by 
> several goroutines in unexpected order.
> 
> So, I've written writeMutex(), that uses a mutex.
> It works as expected, but in my real code, I'd prefer to avoid mutex.
> (in my real code write() can write to different files; the file's name is 
> given as argument)

Use one mutex per file then, and make your write() lock the correct mutex. Note 
that if you have a map of mutexes, you may also need to protect that map with a 
mutex, but that only needs protecting whilst you access the map with the 
possibility of a concurrent write; however, be aware of the danger of lock 
inversion.

-- 
Alex Bligh




-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to