Go - Making Arrays and Slices Safe for Concurrent Use

发布时间 2023-10-08 10:26:50作者: ZhangZhihuiAAA

Problem: You want to make arrays and slices safe for concurrent use by multiple goroutines.


Solution: Use a mutex from the sync library to safeguard the array or slice. Lock the array or slice before modifying it, and unlock it after modifications are made.

 

Arrays and slices are not safe for concurrent use. If you are going to share a slice or array between goroutines, you need to make it safe from race conditions. Go provides a sync package that can be used for this, in particular, Mutex .

Race conditions occur when a shared resource is used by multiple goroutines trying to access it at the same time:

var   shared   [] int   =   [] int { 1 ,   2 ,   3 ,   4 ,   5 ,   6 } 

//  increase  each  element  by  1 
func   increase ( num   int )   { 
      fmt . Printf ( "[+%d  a]  :  %v\n" ,   num ,   shared ) 
      for   i   :=   0 ;   i   <   len ( shared );   i ++   { 
          time . Sleep ( 20   *   time . Microsecond ) 
          shared [ i ]   =   shared [ i ]   +   1 
      } 
      fmt . Printf ( "[+%d  b]  :  %v\n" ,   num ,   shared ) 
} 
 
//  decrease  each  element  by  1 
func   decrease ( num   int )   { 
      fmt . Printf ( "[ - %d  a]  :  %v\n" ,   num ,   shared ) 
      for   i   :=   0 ;   i   <   len ( shared );   i ++   { 
          time . Sleep ( 10   *   time . Microsecond ) 
          shared [ i ]   =   shared [ i ]   -   1 
      } 
      fmt . Printf ( "[ - %d  b]  :  %v\n" ,   num ,   shared ) 
}

 

func   main ()   { 
      for   i   :=   0 ;   i   <   5 ;   i ++   { 
          go   increase ( i ) 
      } 
      for   i   :=   0 ;   i   <   5 ;   i ++   { 
          go   decrease ( i ) 
      } 
      time . Sleep ( 2   *   time . Second ) 
}

When you run the program, you will see something like this:

[ - 4  a]  :  [1  2  3  4  5  6]
[ - 1  a]  :  [0  2  3  4  5  6]
[ - 2  a]  :  [0  1  3  4  5  6]
[ - 3  a]  :  [0  1  2  4  5  6]
[+0  a]  :  [ - 2  1  2  3  5  6]
[+1  a]  :  [ - 3  - 1  2  3  4  6]
[ - 4  b]  :  [ - 2  - 2  1  3  4  5]
[+3  a]  :  [ - 2  - 2  0  3  4  5]
[+4  a]  :  [ - 1  - 1  - 1  1  4  5]
[ - 1  b]  :  [1  0  0  0  1  4]
[ - 2  b]  :  [1  0  0  0  1  3]
[ - 3  b]  :  [1  0  0  0  1  2]
[+2  a]  :  [1  0  0  0  1  2]
[ - 0  a]  :  [2  2  1  1  1  2]
[+0  b]  :  [1  2  3  2  1  3]
[ - 0  b]  :  [1  2  3  3  2  2]
[+1  b]  :  [1  2  3  4  4  3]
[+3  b]  :  [1  2  3  4  4  4]
[+4  b]  :  [1  2  3  4  4  5]
[+2  b]  :  [1  2  3  4  5  6]

How can you prevent such race conditions? Go has the sync package in the standard library that provides you with a mutex , or a mutual exclusion lock:

 

var   shared   [] int   =   [] int { 1 ,   2 ,   3 ,   4 ,   5 ,   6 } 
var   mutex   sync . Mutex 
 
//  increase  each  element  by  1 
func   increaseWithMutex ( num   int )   { 
      mutex . Lock () 
      fmt . Printf ( "[+%d  a]  :  %v\n" ,   num ,   shared ) 
      for   i   :=   0 ;   i   <   len ( shared );   i ++   { 
          time . Sleep ( 20   *   time . Microsecond ) 
          shared [ i ]   =   shared [ i ]   +   1 
      } 
      fmt . Printf ( "[+%d  b]  :  %v\n" ,   num ,   shared ) 
      mutex . Unlock () 
} 

//  decrease  each  element  by  1 
func   decreaseWithMutex ( num   int )   { 
      mutex . Lock () 
      fmt . Printf ( "[ - %d  a]  :  %v\n" ,   num ,   shared ) 
      for   i   :=   0 ;   i   <   len ( shared );   i ++   { 
          time . Sleep ( 10   *   time . Microsecond ) 
          shared [ i ]   =   shared [ i ]   -   1 
      } 
      fmt . Printf ( "[ - %d  b]  :  %v\n" ,   num ,   shared ) 
      mutex . Unlock () 
} 

Using it is quite simple. First you need to declare a mutex. Then, you call Lock on the mutex before you start modifying the shared slice. This will lock the shared slice such that nothing else can use it. When you’re done, you call Unlock to unlock the mutex.

Here’s the output if you call these functions from main as before:

zzh@ZZHPC:/zdata/MyPrograms/Go/testing$ go run main.go
[ - 4 a] : [1 2 3 4 5 6]
[ - 4 b] : [0 1 2 3 4 5]
[+0 a] : [0 1 2 3 4 5]
[+0 b] : [1 2 3 4 5 6]
[+1 a] : [1 2 3 4 5 6]
[+1 b] : [2 3 4 5 6 7]
[+2 a] : [2 3 4 5 6 7]
[+2 b] : [3 4 5 6 7 8]
[+3 a] : [3 4 5 6 7 8]
[+3 b] : [4 5 6 7 8 9]
[+4 a] : [4 5 6 7 8 9]
[+4 b] : [5 6 7 8 9 10]
[ - 0 a] : [5 6 7 8 9 10]
[ - 0 b] : [4 5 6 7 8 9]
[ - 1 a] : [4 5 6 7 8 9]
[ - 1 b] : [3 4 5 6 7 8]
[ - 2 a] : [3 4 5 6 7 8]
[ - 2 b] : [2 3 4 5 6 7]
[ - 3 a] : [2 3 4 5 6 7]
[ - 3 b] : [1 2 3 4 5 6]
zzh@ZZHPC:/zdata/MyPrograms/Go/testing$ go run main.go
[+2 a] : [1 2 3 4 5 6]
[+2 b] : [2 3 4 5 6 7]
[+0 a] : [2 3 4 5 6 7]
[+0 b] : [3 4 5 6 7 8]
[ - 0 a] : [3 4 5 6 7 8]
[ - 0 b] : [2 3 4 5 6 7]
[+3 a] : [2 3 4 5 6 7]
[+3 b] : [3 4 5 6 7 8]
[ - 4 a] : [3 4 5 6 7 8]
[ - 4 b] : [2 3 4 5 6 7]
[ - 1 a] : [2 3 4 5 6 7]
[ - 1 b] : [1 2 3 4 5 6]
[ - 2 a] : [1 2 3 4 5 6]
[ - 2 b] : [0 1 2 3 4 5]
[+4 a] : [0 1 2 3 4 5]
[+4 b] : [1 2 3 4 5 6]
[ - 3 a] : [1 2 3 4 5 6]
[ - 3 b] : [0 1 2 3 4 5]
[+1 a] : [0 1 2 3 4 5]
[+1 b] : [1 2 3 4 5 6]