这种同步结构正确吗?

发布于 2024-09-24 21:43:28 字数 716 浏览 1 评论 0原文

我有一个多线程应用程序。线程使用 ABC.Connector。我希望一次只有一个线程可以访问连接器属性。

class ABC
{
    /// <summary>
    /// Synchronization object.
    /// </summary>
    static object _syncObject = new object();

    static DataAccess _connector;
    /// <summary>
    /// Global object for data access.
    /// </summary>
    public static DataAccess Connector
    {
        get
        {
            lock (_syncObject)
            {
                return _connector.CreateCopy(); // get a copy of original _connector
            }
        }
        set
        {
            lock (_syncObject)
            {
                _connector = value;
            }
        }
    }
}

正确吗?

I have a multi-threaded application. Threads use ABC.Connector. I want that only one thread at a time have access to a Connector property.

class ABC
{
    /// <summary>
    /// Synchronization object.
    /// </summary>
    static object _syncObject = new object();

    static DataAccess _connector;
    /// <summary>
    /// Global object for data access.
    /// </summary>
    public static DataAccess Connector
    {
        get
        {
            lock (_syncObject)
            {
                return _connector.CreateCopy(); // get a copy of original _connector
            }
        }
        set
        {
            lock (_syncObject)
            {
                _connector = value;
            }
        }
    }
}

Is it correct ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静赏你的温柔 2024-10-01 21:43:28

好吧,这肯定会使获取和设置 Connector 属性成为线程安全的(尽管我会将 _syncObject 设为只读)。但是,它并不使 DataAccess 线程安全...互斥体仅在线程获取和设置属性时应用。。

换句话说,如果两个线程都执行以下操作:

ABC.DataAccess.DoSomeLongRunningOperation();

则两个线程仍将同时运行 DoSomeLongRunningOperation()。如果该操作不是线程安全的,它仍然会是一个问题。

如果您一次只希望一个线程使用DataAccess,那么您可以这样写:

public static void UseConnector(Action<DataAccess> action)
{
    lock (_syncObject)
    {
        action(_connector);
    }
}

那么如果两个线程都这样做:

ABC.UseConnector(access => access.DoLongRunningOperation());

那么DoLongRunningOperation()一次只会在一个线程中运行。您仍然遇到行为不端的客户可能会写的问题:

DataAccess naughty = null;
ABC.UseConnector(access => naughty = access);
// Haha! I've circumvented your thread safety!
naughty.DoLongRunningOperation();

...但希望这对您来说不是问题。

Well, that will certainly make getting and setting the Connector property thread-safe (although I'd make _syncObject read-only). However, it doesn't make DataAccess thread-safe... the mutex will only apply while threads are getting and setting the property.

In other words, if two threads both do:

ABC.DataAccess.DoSomeLongRunningOperation();

then DoSomeLongRunningOperation() will still be run concurrently by the two threads. If that operation isn't thread-safe, it's still going to be a problem.

If you only want one thread at a time to use the DataAccess then you could write:

public static void UseConnector(Action<DataAccess> action)
{
    lock (_syncObject)
    {
        action(_connector);
    }
}

Then if two threads both do:

ABC.UseConnector(access => access.DoLongRunningOperation());

then DoLongRunningOperation() will only run in one thread at a time. You've still got the issue that misbehaving clients could write:

DataAccess naughty = null;
ABC.UseConnector(access => naughty = access);
// Haha! I've circumvented your thread safety!
naughty.DoLongRunningOperation();

... but hopefully that isn't an issue for you.

黑凤梨 2024-10-01 21:43:28

是的,这通常是正确的。但请注意,在 Connector get 返回 _connector 引用后,对其的访问是不同步的。

Yes, this is generally correct. But take into account, that after Connector get returns the _connector reference, access to it is unsynchronized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文