.net - Using the C# Volatile keyword in Threaded Application -
I have a class in which there are some arraylists.
My main class creates a new example: at least 2 threads in my main square of this class are included and removed from my class with a simple list. At the moment, everything is going well, but I was wondering if it was my class with variants of simplicity like / or
will be safe to declare as a private volatile miclass; Myclass = new myclass (); ...... myclass.Add (...) myclass.Clear (..)
Use of Volatile Keywords In this example your code will not be thread-safe. The volatile keyword is usually used to make sure that when the value of a variable (i.e. class field) is read or written, the latest value for that variable is either read from the main memory or directly from the cache Is read (for example, a CPU register) for example, unstable keyword is a way of saying "do not use caching optimization with this shared field ", And removes the issue where threads can use local copies of a field and therefore can not see the updates of each other
In your case, the value of Micalus is not actually being updated. (I.e. you are not re-specifying your class) then the unstable is not useful for you, and this is not the update of the Mylclass variable that you really want in this case Aga also to create a safe way.
If you want to update the actual class thread-safe, then using a "lock" around "add" and "clear" is straight forward. This will ensure that only one thread can perform these operations at a time (which updates the internal condition of the mycelos) and should not be done in such a parallel.
A lock can be used as follows:
Private readonly objects syncObj = new object (); Private read-only myclass = new myclass (); ...... lock (syncObj) {myclass.Add (...)} lock (syncObj) {myclass.Clear (..)}
You must also add locking to anyone Even the "Aad" reads the state being updated around the code, if that is the case, although it does not appear in your example code.
This can not be obvious when you need to lock in to add a multi-threaded code to a collection, if we list or list an array as an example, the problems arise internally Because these collections use an array in the form of backing stores, and this array will be dynamically "enlarged" (i.e. by creating a new large array and copying the old content) meet the capabilities. Or goes when asked Add. It all happens internally and requires maintenance of these arrays and variables, as is the current size collection (the actual length of the original array can be as large). Therefore, adding to the collection can involve a number of steps if the internal array should be increased. When using multiple threads in an insecure manner, multiple threads may be indirectly being increased while being added, and thus filling each other's updates. Also adding the issue of multiple threads at the same time, it is also an issue that another thread is trying to read the collection whilst the internal condition is changing using the lock ensures That these types of actions are done without interference from other threads.
Comments
Post a Comment