0

This may have an obvious answer but I will ask anyway.

Is there a variable equivalent of the blocking collection for C#? What I want is for all my threads to be able to access a shared variable. It will not be a collection, just a shared variable that will be adjusted in value as each thread uses it. What I like about the blockingcollection is that ques, and locks, are managed by C#, and was hoping there was something similar for just a variable?

I could use a public static variable and create the lock myself but thought I should check.

EDIT: Would the interlock be a possibility.

Danrex
  • 1,657
  • 4
  • 31
  • 44
  • This might help you http://stackoverflow.com/questions/1360533/how-to-share-data-between-different-threads-in-c-sharp-using-aop – coster128 Jun 08 '15 at 23:57
  • 4
    You should be using a `lock` or `Mutex` (or other alternatives). There is no built-in "thread-safe variable" because it makes no sense to begin with. Managing thread synchronization in a get or in a set is meaningless - what you should be doing is make the code that uses the variable thread-safe. If there is a specific logic regarding the variable (lets assume `int`) you wish to enforce, you can represent it as a class with methods such as `ResetToZero`, `AddOne`, `CheckEven`, etc - and make that class thread-safe. – SimpleVar Jun 09 '15 at 00:00
  • 4
    Synchronization requires blocking code, it isn't possible with variables. Making variables thread-safe is the subject of intensive study, called STM (Software Transactional Memory) that just can't seem to make it to our machines. Biggest problem with STM is that nobody has proven that it is practical, nobody has created an operating system based on STM yet. – Hans Passant Jun 09 '15 at 00:09
  • I agree with @HansPassant, plus this question is overly broad. Practical use-case and code samples will definitely add some value, otherwise it's more appropriate for other StackExchange group (e.g. Programmers) or forums. Wrapping up: from my practical experience using TPL, I found good use of ConcurrentStack, ConcurrentQueue, ConcurrentDictionary(TKey, TValue). With MaxDegreeOfParallelism set to number of CPU cores it works rather efficiently. Best regards, – Alexander Bell Jun 09 '15 at 00:54

1 Answers1

0

What sort of variable and what do you want to do with this variable?

Interlocked will allow you access a variable in an atomic way.

This means that for primitive type (int, float, object ref) you can perform a few primitive actions (increment, swap values, etc) in an atomic and therefore thread safe way. For example, if you want a counter to be incremented/decremented from multiple threads this could be a good solution.

Other (more powerful) uses include conditional atomic swaps, which are part of a idiom of software transactional memory and allow safe changes to a variable from multiple threads without using other synchronization mechanisms (such as a mutex).

However, interlocked operations must perform some locking on the hardware level and so have a certain performance penalty and are not a magic wand for multi threaded data access and race conditions.

Also, they only really work on primitive types and operations. Interleaving a few operations on a few types at the same time requires more wiring and is not directly supported by interlocked class.

Yosef O
  • 367
  • 2
  • 7