Depending on the types of data elements in the data_type structure (and also depending on the operations you want to perform for these members), you can refuse to use a separate synchronization object, instead using the lock functions.
In your code example, all data members are integers, and all operations are assignments (and apparently counted), so you can use InterlockedExchange () to set values atomically and InterlockedCompareExchange () to read values atomically.
If you need to use non-integer types of data elements or if you need to perform more complex operations or you need to coordinate atomic access to several operations at a time (for example, read data [1]. A, and then write data [1] .b), then you have to use a synchronization object like CRITICAL_SECTION.
If you must use a synchronization object, I recommend that you split your dataset into subsets and use one synchronization object for each subset. For example, you can use one CRITICAL_SECTION for each range of 1000 elements in a data array.
source share