标签: locking

将sync.Mutex放在包级别变量中对包级别映射有什么影响吗?

在以下函数之一中调用 mu.Lock() 是否会锁定包级 accCache 映射?

package cache

import (
    "sync"
)

type CachedAccount struct {
    Id       string
    Expires  int64
}


var accCache = make(map[string]CachedAccount)

var mu sync.Mutex


func addAccount...
func getAccount...

Run Code Online (Sandbox Code Playgroud)

mutex locking go

-2
推荐指数
1
解决办法
303
查看次数

c#锁不起作用/关键部分

我试图在FOR循环中使用两个不同的线程来推进静态(int)计数器,所以如果循环运行10次,我(应该)得到计数器= 20.由于某种原因,每次运行循环(19,20,21)时我都会得到不同的值,即使我使用LOCK访问该计数器,(代码在控制台中运行):

public static int Counter = 0;
static object syncObject = new object();

static void Main(string[] args)
        { 

int ForLength = 10;

            Thread FirstThread, SecondThread;

            for (int i = 0; i <= ForLength; i++)
            {
                FirstThread = new Thread(RaiseCounter);
                FirstThread.IsBackground = false;

                SecondThread = new Thread(RaiseCounter);
                SecondThread.IsBackground = false;               


                FirstThread.Start();                
                SecondThread.Start();

                //Console.WriteLine(Counter);
            }


            Console.WriteLine(Counter);

            Console.ReadLine();
        }

        public static void RaiseCounter ()
        {
            lock (syncObject)
            {
                Counter++;
            }
        }
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading locking critical-section

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

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
查看次数

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
查看次数

Streamreader锁定文件

我有一个ac#app(Windows服务),它会触发一个读取目录中文件的计时器事件,并使用文件中的数据发送短信.当事件触发时,它会在处理新文件之前尝试将"已处理"目录中的已处理文件移动到"已完成"目录.我不断得到"另一个进程正在使用的文件"异常,虽然我很确定我处理了使用这些文件的所有内容.如果我停止服务并再次启动它,则会释放文件.有任何想法吗?

//Code that fires the timer    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;

namespace SmsWindowsService
{
    public partial class SmsWindowsService : ServiceBase
    {
        private static System.Timers.Timer aTimer;

        public SmsWindowsService()
        {
            InitializeComponent();

            if (!System.Diagnostics.EventLog.SourceExists("MatterCentreSMSSource"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "MatterCentreSMSSource", "MatterCentreSMSLog");
            }
            elMatterCentreSMS.Source = "MatterCentreSMSSource";
            elMatterCentreSMS.Log = "MatterCentreSMSLog";

        }

        protected override void OnStart(string[] args)
        {
            string logText = string.Empty;

            logText = "MatterCentreSMS Service started successfully on " + DateTime.Now;

            WriteEventLog(logText);

            //Create a timer …
Run Code Online (Sandbox Code Playgroud)

c# locking file streamreader

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