是C#数组线程安全的? [英] Are C# arrays thread safe?

查看:627
本文介绍了是C#数组线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在特定


  1. 创建一个函数取一个数组和一个索引作为参数。<​​/ li>
  2. 创建A N元素的数组。

  3. 创建A N计数循环。

  4. 在一个新的线程在循环中分配对象的新实例,使用传入的索引数组。

我知道如何管理等等。我很感兴趣,知道线程,如果这是做什么的线程安全的方式。

 类节目
{
    //虚假对象
    类SomeObject
    {
        私人诠释值1;
        私人诠释值2;        公共SomeObject(INT值1,INT值2)
        {
            this.value1 =值;
            this.value2 =值2;
        }
    }    静态无效的主要(字串[] args)
    {        变种S =新SomeObject [10];
        VAR线程= Environment.ProcessorCount - 1;
        VAR STP =新SmartThreadPool(1000,线程,线程);
        为(变量I = 0; I&小于10;我+ +)
        {
            stp.QueueWorkItem(的createElement,S,I);
        }    }    静态无效的createElement(SomeObject [] S,INT指数)
    {
        S [指数] =新SomeObject(指数,2);
    }
}


解决方案

我相信,如果每个线程只能在阵列的一个独立部分,一切都会好起来。如果你要的共享的数据(即线程之间沟通呢),那么你就需要某种记忆障碍,以避免内存模型问题。

我的认为的,如果你产卵一堆线程,每个填充自己的数组的部分,然后等待所有这些线程使用线程结束。加入,那会做足够的壁垒方面你是安全的。我没有对任何支持文档的那一刻,你要知道...

编辑:您的样品code是安全的。在任何时候,两个线程访问相同的元素 - 就好像他们每个人都有不同的变量。然而,这并不倾向于自身有用的。在某些时候,正常的线程将要共享的状态 - 一个线程会想读什么又写。否则,有一个在他们写入到一个共享的阵列,而不是变成自己的私有变量没有任何意义。的就是的上,你需要小心点 - 线程之间的协调

In particular

  1. Create a function to take an array and an index as parameters.
  2. Create a n element array.
  3. Create a n count loop.
  4. Inside the loop on a new thread assign a new instance of the object to the array using the indexer passed in.

I know how to manage the threads etc. I am interested in know if this is thread safe way of doing something.

 class Program
{
    // bogus object
    class SomeObject
    {
        private int value1;
        private int value2;

        public SomeObject(int value1, int value2)
        {
            this.value1 = value1;
            this.value2 = value2;
        }
    }

    static void Main(string[] args)
    {

        var s = new SomeObject[10];
        var threads = Environment.ProcessorCount - 1;
        var stp = new SmartThreadPool(1000, threads, threads);
        for (var i = 0; i < 10; i++)
        {
            stp.QueueWorkItem(CreateElement, s, i);
        }

    }

    static void CreateElement(SomeObject[] s, int index)
    {
        s[index] = new SomeObject(index, 2);
    }
}

解决方案

I believe that if each thread only works on a separate part of the array, all will be well. If you're going to share data (i. e. communicate it between threads) then you'll need some sort of memory barrier to avoid memory model issues.

I believe that if you spawn a bunch of threads, each of which populates its own section of the array, then wait for all of those threads to finish using Thread.Join, that that will do enough in terms of barriers for you to be safe. I don't have any supporting documentation for that at the moment, mind you ...

EDIT: Your sample code is safe. At no time are two threads accessing the same element - it's as if they each have separate variables. However, that doesn't tend to be useful on its own. At some point normally the threads will want to share state - one thread will want to read what another has written. Otherwise there's no point in them writing into a shared array instead of into their own private variables. That's the point at which you need to be careful - the coordination between threads.

这篇关于是C#数组线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆