...

Source file src/runtime/testdata/testprog/map.go

Documentation: runtime/testdata/testprog

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import "runtime"
     8  
     9  func init() {
    10  	register("concurrentMapWrites", concurrentMapWrites)
    11  	register("concurrentMapReadWrite", concurrentMapReadWrite)
    12  	register("concurrentMapIterateWrite", concurrentMapIterateWrite)
    13  }
    14  
    15  func concurrentMapWrites() {
    16  	m := map[int]int{}
    17  	m[5] = 0
    18  	m[6] = 0
    19  	c := make(chan struct{})
    20  	go func() {
    21  		for i := 0; i < 10000; i++ {
    22  			m[5] = 0
    23  			runtime.Gosched()
    24  		}
    25  		c <- struct{}{}
    26  	}()
    27  	go func() {
    28  		for i := 0; i < 10000; i++ {
    29  			m[6] = 0
    30  			runtime.Gosched()
    31  		}
    32  		c <- struct{}{}
    33  	}()
    34  	<-c
    35  	<-c
    36  }
    37  
    38  func concurrentMapReadWrite() {
    39  	m := map[int]int{}
    40  	m[5] = 0
    41  	m[6] = 0
    42  	c := make(chan struct{})
    43  	go func() {
    44  		for i := 0; i < 10000; i++ {
    45  			m[5] = 0
    46  			runtime.Gosched()
    47  		}
    48  		c <- struct{}{}
    49  	}()
    50  	go func() {
    51  		for i := 0; i < 10000; i++ {
    52  			_ = m[6]
    53  			runtime.Gosched()
    54  		}
    55  		c <- struct{}{}
    56  	}()
    57  	<-c
    58  	<-c
    59  }
    60  
    61  func concurrentMapIterateWrite() {
    62  	m := map[int]int{}
    63  	m[5] = 0
    64  	m[6] = 0
    65  	c := make(chan struct{})
    66  	go func() {
    67  		for i := 0; i < 10000; i++ {
    68  			m[5] = 0
    69  			runtime.Gosched()
    70  		}
    71  		c <- struct{}{}
    72  	}()
    73  	go func() {
    74  		for i := 0; i < 10000; i++ {
    75  			for range m {
    76  			}
    77  			runtime.Gosched()
    78  		}
    79  		c <- struct{}{}
    80  	}()
    81  	<-c
    82  	<-c
    83  }
    84  

View as plain text