Threading Questions

bitwise via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 7 19:31:22 PDT 2015


On Wednesday, 7 October 2015 at 09:09:36 UTC, Kagamin wrote:
> On Sunday, 4 October 2015 at 04:24:55 UTC, bitwise wrote:
>> I use C#(garbage collected) for making apps/games, and while, 
>> _in_theory_, the GC is supposed to protect you from leaks, 
>> memory is not the only thing that can leak. Threads need to be 
>> stopped, graphics resources need to be released, etc.
>
> XNA doesn't manage graphics resources?
>
> On Monday, 5 October 2015 at 17:40:24 UTC, bitwise wrote:
>> I'm not sure what's going to be done with shared, but I do 
>> think it's annoying that you can't do this:
>>
>> shared Array!int numbers;
>>
>> someThread... {
>>     numbers.clear(); // 'clear' is not shared
>> }
>>
>> So this means that on top of the already ridiculous number of 
>> attributes D has, now you have to mark everything as shared 
>> too =/
>
> That's illegal in other languages too except that they allow 
> you to do it. If you want concurrent collections, you must code 
> them separately: 
> https://msdn.microsoft.com/en-us/library/system.collections.concurrent%28v=vs.110%29.aspx

I'm not sure what you mean by illegal. AFAIK 'shared' is unique 
to D. As far as simply locking and then accessing a global 
variable(class static member) in C#, there is no problem doing 
that from multiple threads.

If you have System.Collections.Generic.List(T) static class 
member, there is nothing wrong with using it from multiple 
threads like this:

class Foo {
     static List<int> numbers = new List<int>();
     void bar() {
         new Thread(()=>{
         lock(numbers) {
             numbers.Add(1);
         }).Start();
     }
}

     Bit



More information about the Digitalmars-d-learn mailing list