标签: mutex

如何使用关键部分保护我的Indy套接字写入?

cs.Acquire;
try
   AContext.Connection.Socket.Write(packet);
finally
   cs.Release;
end;
Run Code Online (Sandbox Code Playgroud)

要么

EnterCriticalSection(cs);
   AContext.Connection.Socket.Write(packet);
LeaveCriticalSection(cs);
Run Code Online (Sandbox Code Playgroud)

我试图在线程中将我的数据包发送到服务器,但我有20个线程,它也将数据发送到同一个连接套接字.我尝试使用Critical Section或Mutex,它们都不起作用,我收到所有线程发送时的垃圾.

这都是关于我之前的问题

数据包看起来像这样:

LengthData

0000000010HelloWorld

服务器接收数据:

ReadBytes(10);

len := (Then remove zeros from begining);

ReadBytes(len); // data.
Run Code Online (Sandbox Code Playgroud)

有时我会在ReadBytes(10)中收到垃圾,它是长度+数据的混合,例如:"10Hellowor"

如果我只使用一个线程将数据发送到服务器,那么每次都可以正常工作.如果发送了很多线程,那么一切都会出错.

delphi multithreading mutex

-3
推荐指数
1
解决办法
627
查看次数

如何在C#中使用win32 API

如何将win32连接到C#并使用win32函数创建信号量,互斥量等.

c# winapi multithreading mutex semaphore

-3
推荐指数
1
解决办法
785
查看次数

使用g ++的C ++ 11中关于互斥锁的编译错误是什么?

这是什么c ++编译错误?我已经在另一台已配置的机器上编译了我的代码,所以我想我错过了在环境中要在C ++ 11中编译的东西(代码使用--std = c ++ 0x选项进行编译)。该错误似乎与C ++ 11中的新互斥功能有关。

In file included from /usr/include/c++/4.6/mutex:43:0,
                 from include/Ric_box_tele.h:4,
                 from src/Ric_box_tele.cpp:1:
/usr/include/c++/4.6/functional: In member function ‘void std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__call(std::tuple<_Args ...>&&, std::_Index_tuple<_Indexes ...>, typename std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__enable_if_void<_Res>::type) [with _Res = void, _Args = {}, int ..._Indexes = {0, 1, 2}, _Result = void, _Functor = std::_Mem_fn<void (Ric_box_tele::*)(Socket, int)>, _Bound_args = {Ric_box_tele*, Socket, int}, typename std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__enable_if_void<_Res>::type = int]’:
/usr/include/c++/4.6/functional:1378:24:   instantiated from ‘std::_Bind_result<_Result, _Functor(_Bound_args ...)>::result_type std::_Bind_result<_Result, _Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {}, …
Run Code Online (Sandbox Code Playgroud)

c++ mutex g++ c++11

-3
推荐指数
1
解决办法
821
查看次数

如何锁定功能

var mutex sync.Mutex
func main() {
     handle()
    go register()
}

func register(){
   myObject.OnEvent().DoFunc(HandleConnect)
}
func HandleConnect(){
    handle()
}
func handle() bool {

    mutex = sync.Mutex{}
    mutex.Lock()
    defer mutex.Unlock()
    ....some logic.... do login...
    return true
}
Run Code Online (Sandbox Code Playgroud)

我有一个在我的应用程序中被多次调用的 HandleConnect 我想锁定句柄,因为如果有很多调用,我希望只有一个会执行登录逻辑当我运行它时,我收到一个错误 致命错误:同步:解锁未锁定的互斥锁

我该如何解决?

mutex go

-3
推荐指数
1
解决办法
1450
查看次数

How to lock a Rust struct the way a struct is locked in Go?

I'm trying to learn how to do locks in Rust the way they work in Go. With Go I can do something like:

type Info struct {
    sync.RWMutex
    height    uint64 
    verify bool
}
Run Code Online (Sandbox Code Playgroud)

If I have some function/method acting on info I can do this:

func (i *Info) DoStuff(myType Data) error {
    i.Lock()
    //do my stuff
}
Run Code Online (Sandbox Code Playgroud)

It seems like what I need is the sync.RWMutex, so this is what I have tried:

pub struct Info {
    pub lock: sync.RWMutex, …
Run Code Online (Sandbox Code Playgroud)

mutex locking rust

-3
推荐指数
1
解决办法
104
查看次数

不需要互斥,竞争条件并不总是坏的,是吗?

我有一个疯狂的想法,在我们大多数人通常想要并使用互斥同步的某些情况下,可以省略互斥同步.

好吧,假设你有这种情况:

Buffer *buffer = new Buffer(); // Initialized by main thread;

...

// The call to buffer's `accumulateSomeData` method is thread-safe
// and is heavily executed by many workers from different threads simultaneously.
buffer->accumulateSomeData(data); // While the code inside is equivalent to vector->push_back()

...

// All lines of code below are executed by a totally separate timer
// thread that executes once per second until the program is finished.

auto bufferPrev = buffer; // A temporary pointer to previous …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading mutex

-4
推荐指数
1
解决办法
92
查看次数

Golang concurrency write to variable - why this code works?

I'm learning concurrency-related issues in Golang. I wrote some code:

package main

import (
    "fmt"
    "time"
)

func incr(num *int) {
    *num = *num + 1

}

func main() {
    var a = 0

    for i := 0; i < 50; i++ {
        go incr(&a)
    }

    incr(&a)

    time.Sleep(1 * time.Second)
    fmt.Println(a)
}
Run Code Online (Sandbox Code Playgroud)

The result of this code is: 51

In this code I've declared a variable which I'm increasing in 50 running goroutines. What I've read and unsterstood this code …

concurrency mutex locking shared-memory go

-4
推荐指数
1
解决办法
86
查看次数

Mutex.Lock()如何知道要锁定哪些变量?

我是新手,所以请保持温柔。

因此,我已经在某些代码中使用互斥锁了几周了。我了解其背后的概念:锁定对特定资源的访问权限,与之交互(读或写),然后为其他人再次解锁。

我使用的互斥锁代码主要是复制粘贴调整。该代码可以运行,但是我仍在努力解决它的内部问题。到目前为止,我一直在结构中使用互斥锁来锁定该结构。今天,我发现了这个示例,这使我完全不清楚互斥锁实际上是在锁定什么。下面是一段示例代码:

var state = make(map[int]int)

var mutex = &sync.Mutex{}

var readOps uint64
var writeOps uint64

// Here we start 100 goroutines to execute repeated reads against the state, once per millisecond in each goroutine.
for r := 0; r < 100; r++ {
    go func() {
        total := 0
        for {
            key := rand.Intn(5)
            mutex.Lock()
            total += state[key]
            mutex.Unlock()
            atomic.AddUint64(&readOps, 1)

            time.Sleep(time.Millisecond)
        }
    }()
}
Run Code Online (Sandbox Code Playgroud)

这里让我感到困惑的是,互斥锁和应该锁定的值之间似乎没有任何联系。直到今天,我还认为互斥锁可以锁定特定的变量,但是看这段代码,似乎可以以某种方式将整个程序锁定为仅执行锁定下方的行,直到再次运行解锁为止。我想这意味着所有其他goroutine都会暂停片刻,直到再次运行解锁。由于代码已编译,因此我想它可以知道在lock()和之间访问了哪些变量unlock(),但是我不确定是否是这种情况。

如果所有其他程序都暂停片刻,这听起来不像是真正的多处理程序,那么我想我对所发生的事情没有很好的了解。

有人可以帮助我了解计算机如何知道应锁定哪些变量吗?

concurrency mutex multiprocessing go

-4
推荐指数
1
解决办法
72
查看次数

等待互斥锁而不获取它

我一直在编写一个小型微服务,因此熟悉 Go 及其并发机制。

在我的程序中,我有一个具有状态的结构,我想同步该状态,以便多个 goroutine 能够读取它,但不能在另一个 goroutine 更新该状态时进行。

最初我认为 RWMutax 是我需要的,但是根据文档,只有一个 goroutine 可以在任何给定的时刻获得读锁。我要走这条线:

“如果一个 goroutine 持有一个用于读取的 RWMutex,而另一个 goroutine 可能会调用 Lock,那么在初始读锁被释放之前,任何 goroutine 都不应该期望能够获得一个读锁。”

有没有办法在不获取锁的情况下等待互斥锁?

类似的东西:

type stateful struct {
    state        int
    stateMutex   sync.Mutex
    beingUpdated bool
}

type Stateful interface {
    GetState() int
    SetState(st int)
}

func createStateful (sa string) stateful {
    return server{state: 0, stateMutex: sync.Mutex{}, beingUpdated: false}
}

func (s *stateful) SetState(st int) {
    s.stateMutex.Lock()
    s.beingUpdated = true
    s.state = st
    s.beingUpdated = false
    s.stateMutex.Unlock()
}

func (s *stateful) GetState() …
Run Code Online (Sandbox Code Playgroud)

concurrency mutex go

-5
推荐指数
1
解决办法
98
查看次数