How to create named mutex in Golang?

huangapple go评论67阅读模式
英文:

How to create named mutex in Golang?

问题

我对Golang还比较新,正在尝试创建一个命名互斥锁。我试图复制以下代码:

hMutex = CreateMutex(
    NULL,                        // 默认安全描述符
    FALSE,                       // 未拥有互斥锁
    TEXT("NameOfMutexObject"));  // 对象名称

我在sync中看到的示例似乎没有展示命名互斥锁。

英文:

I am fairly new to Golang and trying to create a named mutex. I am trying to replicate:

    hMutex = CreateMutex( 
    NULL,                        // default security descriptor
    FALSE,                       // mutex not owned
    TEXT("NameOfMutexObject"));  // object name

The examples I see in sync don't appear to illustrate named mutexes.

答案1

得分: 0

命名互斥锁(在Windows中)实际上是用于在进程之间共享互斥锁和/或在代码的不同部分中引用同一个互斥锁的简化方式(在一个地方创建互斥锁,在其他地方“打开”现有的互斥锁)。

在Go中,互斥锁更类似于Windows中的临界区(至少在概念上是这样,我不能对底层实现进行说明)。

在Windows中,命名互斥锁通过获取其名称来使不同的进程共享互斥锁。

要在Go中共享互斥锁,您可以声明互斥锁,以便在需要的地方引用它,或者将引用传递给需要它的任何函数。

由此可见,互斥锁(在Go中)不能在进程之间共享;如果这是您的用例,那么您将需要探索其他跨进程同步机制。但是,如果您只需要在同一个进程中同步线程,只需使用互斥锁,不必担心不能给它命名的事实。 How to create named mutex in Golang?

英文:

Named mutexes (in Windows) are really intended for use when sharing a mutex between/across processes and/or to simplify referencing the same mutex from different parts of the code in a single process (creating the mutex in one place and "opening" an existing mutex in other places).

A mutex in Go is more similar to a critical section in Windows (in concept at least, I cannot speak to the underlying implementation).

In Windows, named mutexes enable different processes to share mutexes by obtaining a reference to one, by its name.

To share a mutex in Go you either declare the mutex such that it is referencable where required or pass a reference to any functions that need it.

It follows that mutexes (in Go) cannot be shared between processes; if that's your use case then you will need to explore other cross-process synchronization mechanisms. But if all you need is to synchronize threads in the same process, just use a mutex and don't worry about the fact that you can't git it a name. How to create named mutex in Golang?

答案2

得分: 0

这是一个使用sync包的命名互斥锁的示例代码,这符合你的需求吗?

package main

import (
	"fmt"
	"sync"
)

var mutexMap sync.Map

func createMutex(name string) *sync.Mutex {
	mutex, ok := mutexMap.Load(name)
	if !ok {
		newMutex := &sync.Mutex{}
		mutex, _ = mutexMap.LoadOrStore(name, newMutex)
	}
	return mutex.(*sync.Mutex)
}

func main() {
	hMutex := createMutex("NameOfMutexObject")
	hMutex.Lock()
	fmt.Println("Locked Mutex: NameOfMutexObject")
	hMutex.Unlock()
	fmt.Println("Unlocked Mutex: NameOfMutexObject")
}

希望对你有帮助!

英文:

Here is an example of a named mutex using sync - is this what you are looking for?

package main

import (
	"fmt"
	"sync"
)

var mutexMap sync.Map

func createMutex(name string) *sync.Mutex {
	mutex, ok := mutexMap.Load(name)
	if !ok {
		newMutex := &sync.Mutex{}
		mutex, _ = mutexMap.LoadOrStore(name, newMutex)
	}
	return mutex.(*sync.Mutex)
}

func main() {
	hMutex := createMutex("NameOfMutexObject")
	hMutex.Lock()
	fmt.Println("Locked Mutex: NameOfMutexObject")
	hMutex.Unlock()
	fmt.Println("Unlocked Mutex: NameOfMutexObject")
}

huangapple
  • 本文由 发表于 2023年2月14日 09:42:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定