basics: add example of sync.Mutex
This commit is contained in:
		
							parent
							
								
									2a77e80aec
								
							
						
					
					
						commit
						9a390fa84b
					
				
							
								
								
									
										39
									
								
								basics/mutex/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								basics/mutex/main.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,39 @@ | |||||||
|  | package main | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"sync" | ||||||
|  | 	"time" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | // SafeCounter is safe to use concurrently. | ||||||
|  | type SafeCounter struct { | ||||||
|  | 	mu sync.Mutex | ||||||
|  | 	v  map[string]int | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Inc increments the counter for the given key. | ||||||
|  | func (c *SafeCounter) Inc(key string) { | ||||||
|  | 	c.mu.Lock() | ||||||
|  | 	// Lock so only one goroutine at a time can access the map c.v. | ||||||
|  | 	c.v[key]++ | ||||||
|  | 	c.mu.Unlock() | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Value returns the current value of the counter for the given key. | ||||||
|  | func (c *SafeCounter) Value(key string) int { | ||||||
|  | 	c.mu.Lock() | ||||||
|  | 	// Lock so only one goroutine at a time can access the map c.v. | ||||||
|  | 	defer c.mu.Unlock() | ||||||
|  | 	return c.v[key] | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func main() { | ||||||
|  | 	c := SafeCounter{v: make(map[string]int)} | ||||||
|  | 	for i := 0; i < 1000; i++ { | ||||||
|  | 		go c.Inc("somekey") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	time.Sleep(time.Second) | ||||||
|  | 	fmt.Println(c.Value("somekey")) | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user