10 examples of 'golang mutex' in Go

Every line of 'golang mutex' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
36func main() {
37 var l = sync.Mutex{}
38 var c = sync.NewCond(&l)
39
40 for _, site := range sites {
41 println("starting scraper for " + site)
42 go scrapeSite(site, c)
43 }
44 for doneCount < len(sites) {
45 time.Sleep(1 * time.Second)
46 }
47 println("Done!")
48}
235func (s *Server) getMutex() (cluster.Mutex, error) {
236 s.mutexMutex.Lock()
237 defer s.mutexMutex.Unlock()
238
239 if s.mutex != nil {
240 return s.mutex, nil
241 }
242
243 mutex, err := s.cluster.Mutex(lockKey)
244 if err != nil {
245 return nil, err
246 }
247
248 s.mutex = mutex
249
250 return s.mutex, nil
251}
18func main() {
19 runtime.LockOSThread()
20 pid := os.Getpid()
21 printPid(pid)
22 time.Sleep(10 * time.Second)
23
24 for {
25 printPid(pid)
26 sayhi()
27 }
28}
15func main() {
16 go foo()
17 go foo()
18
19 // go fooByAtomic()
20 // go fooByAtomic()
21
22 // go fooByMoreScope()
23 // go fooByMoreScope()
24
25 time.Sleep(time.Second)
26}
105func main() {
106 cpu, _ := strconv.Atoi(os.Args[1])
107 lib := os.Args[2]
108 method := os.Args[3]
109 fmt.Println("using CPUs:", cpu)
110 runtime.GOMAXPROCS(cpu)
111 num_routines := 6
112 num_renders := 20
113
114 if method == "chan" {
115 if lib == "rubex" {
116 render_pages2("rubex", re2, num_routines, num_renders)
117 } else {
118 render_pages2("regexp", re1, num_routines, num_renders)
119 }
120 } else {
121 if lib == "rubex" {
122 render_pages("rubex", re2, num_routines, num_renders)
123 } else {
124 render_pages("regexp", re1, num_routines, num_renders)
125 }
126
127 }
128 d, _ := time.ParseDuration("5s")
129 for i := 0; i < 100; i++ {
130 fmt.Println("goroutine:", runtime.NumGoroutine())
131 time.Sleep(d)
132
133 }
134 fmt.Println("Done")
135}
10func main() {
11 var mu sync.Mutex
12 if rand.Int() > 0 {
13 mu.Unlock()
14 } else {
15 mu.Lock()
16 }
17 a = 5
18 _ = a
19}
2148func OpenMutex(desiredAccess uint32, inheritHandle bool, name *uint16) (handle Handle, err error) {
2149 var _p0 uint32
2150 if inheritHandle {
2151 _p0 = 1
2152 } else {
2153 _p0 = 0
2154 }
2155 r0, _, e1 := syscall.Syscall(procOpenMutexW.Addr(), 3, uintptr(desiredAccess), uintptr(_p0), uintptr(unsafe.Pointer(name)))
2156 handle = Handle(r0)
2157 if handle == 0 {
2158 if e1 != 0 {
2159 err = errnoErr(e1)
2160 } else {
2161 err = syscall.EINVAL
2162 }
2163 }
2164 return
2165}
90func (cs *clusterStorage) Lock() error {
91 err := cs.mutexGoReady()
92 if err != nil {
93 return err
94 }
95
96 return cs.mutex.Lock()
97}
271func (c *Context) lockWithMutex(mutex *sync.Mutex) {
272 c.mutex.Lock()
273 mutex.Lock()
274}
9func main() {
10 mutex := sync.Mutex{}
11 mutex.Lock()
12 mutex.Unlock()
13 a = 5
14}

Related snippets