As the other answers pointed out, no, ++ is not "streaming".
Something that I think will help when you learn about multithreading and its dangers is to start very precisely about what you mean by "threadafe" because different people mean different things. Essentially, the thread safety aspect that bothers you is whether the operation is atomic or not. An βatomicβ operation is an operation that is guaranteed not to be filled halfway if it is interrupted by another thread.
(There are many other problems with threads that have nothing to do with atomicity, but which may still fall under certain definitions of thread safety. For example, if there are two threads, each of which modifies a variable, and two threads, each of which reads a variable , two readers guarantee agreement on the order in which the other two threads made mutations? If your logic depends on this, then you have a very difficult thread safety problem that can be dealt with even if every reading and the record is atomic.)
In C #, virtually nothing is guaranteed atomic. In short:
- read 32 bit integer or float
- reading link
- write 32 bit integer or float
- link record
guaranteed to be atomic (see specification for exact details.)
In particular, reading and writing a 64-bit integer or float is not guaranteed to be atomic. If you say:
Cx = 0xDEADBEEF00000000;
in one thread and
Cx = 0x000000000BADF00D;
in another thread, then this is possible in the third thread:
Console.WriteLine(Cx);
have written 0xDEADBEEF0BADF00D, although logically the variable never held this value. C # language reserves the right to make a record the long equivalent of writing two ints one by one, and in practice, some chips implement it this way. The stream switch after the first write may cause the reader to read something unexpected.
Long and short: do not share anything between two threads without blocking something. Castles are only slow when they are happy; if you have a performance problem due to competing castles, then fix any architectural flaw leading to competing castles. If locks are not approved and are still too slow, then you should consider moving to dangerous methods with a low lock level.
The general low-blocking technique to use here is, of course, calling Threading.Interlocked.Increment , which makes an integer increment a guaranteed atom. (Please note, however, that it still does not guarantee things like what happens if each of the two threads locks two different variables at different times and the other threads try to determine which increment happened "first ". C # does not guarantee that all contiguous sequences of events are scanned by all threads.)