Generic Thread.VolatileRead and Thread.VolatileWrite

I am thinking about using a generic version of Thread.VolatileRead and Thread.VolatileWrite. The code is similar to existing one (actually there are plenty of overloads) with the difference of generic type parameter:

[MethodImpl(MethodImplOptions.NoInlining)]
public T VolatileRead(ref T address)
{
    T result = address;
    Thread.MemoryBarrier();
    return result;
}

[MethodImpl(MethodImplOptions.NoInlining)]
public void VolatileWrite(ref T address, T value)
{
    Thread.MemoryBarrier();
    address = value;
}

Why would I use generics if there is plenty of overloads already there? Because you have to perform casting to and from object since there is a single overload that accepts reference (ref object address that is). Here is an example:

Tubo tubo = new Tubo();
object param = tubo;
Tubo read = (Tubo)Thread.VolatileRead(ref param);

Isn’t the following code much better?

Tubo tubo = new Tubo();
Tubo read = MyThread.VolatileRead(ref tubo);

The questions is whether my code is correct or not. Sure it looks like correct but one never knows for sure when dealing with threading. Feedback appreciated.


Avtor: Anonymous, objavljeno na portalu SloDug.si (Arhiv)

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.

signs clomid is working forum
signs clomid is working forum - četrtek, 16. januar 2025

Hameed S, et al <a href=https://clomid2buy.top/>where to buy clomid near me</a>

cytotec misoprostol 200mcg buy
cytotec misoprostol 200mcg buy - nedelja, 01. december 2024

s 2009 study 50 in the first month <a href=https://cytotec2buy.top/>where to buy cytotec tablets</a> After pelleting, diets are often dried by heating the diet, although some formulations may be air dried

HedaWhece
HedaWhece - nedelja, 17. november 2024

<a href=https://fastpriligy.top/>priligy dapoxetine 60mg</a> 29 Also, there is evidence for the presence of PGE2 receptors in rat kidney, most of which are found in the outer medulla with a similar distribution to the Tamm Horsfall glycoprotein, indicating that they are located in TAL cells