Using shared memory

The good thing with shared memory and mutexes is that, in theory, they usually take a very small amount of the code, which means that the rest of the code can work concurrently without any other delays. However, only after you have implemented something can you see what really happens!

The name of this implementation will be WCshared.go and will be presented in five parts: the first part of the utility is the following:

package main 
 
import ( 
   "bufio" 
   "fmt" 
   "io" 
   "os" 
   "path/filepath" 
   "regexp" 
   "sync" 
) 
 
type File struct { 
   Filename   string 
   Lines      int 
   Words      int 
   Characters int 
   Error      error 
} 
 
var aM sync.Mutex 
var values = make([]File, 0) 

The values slice will be the shared variable of the program whereas the name of the mutex variable ...

Get Go Systems Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.