Shared enum between multiple threads

佐手、 提交于 2019-12-12 04:49:22

问题


I have an enumeration that is shared between multiple threads:

public enum Action
{
   Read,
   Write,
   None
}

Within a class I have a variable of Action type:

public Action _action;

This is a shared variable, that is, it is updated and read from multiple threads.

For example, from one thread I do:

_action = Action.Read

And from another one:

if (_action == Action.Read)
{
}
else if (_action == Action.Write)
{
}
else if (_Action == Action.None)
{
}
else
{
}

So I would like to use Interlock to update and/or read it from different threads at the same time. How can I do it through a property?

I have seen many posts, for example below one:

How to apply InterLocked.Exchange for Enum Types in C#?

Problem here is that enumeration needs to cast to an int, but I would like to keep enumeration without casting. Is it possible? If so, could you post some example? Also Is it possible to combine volatile with interlock? I mean apply interlock on a volatile enumeration.


回答1:


In this scenario Interlocked wouldn't be useful. Your series of if/then checks depend on the value of _action remaining unchanged as they all execute. Otherwise _action==Action.Read could be false, but before the next statement executes _action is set to Action.Read and all of the other conditions are false.

You'd want to use lock to ensure that nothing modifies _action while those statements are executing.

So you might have an object for your lock:

private readonly _lockObject = new object();

And then when _action is getting set:

lock(_lockObject)
{
    _action = newValue;
}

And when executing your conditions you could just read the value of _action within the lock and then release it. That way the lock is held for the shortest time possible. If _action gets modified while you're executing your conditions you won't be affected because you've created a separate value and you're no longer depending on the value of _action.

Action action;
lock(_lockObject)
{
    action = _action
}
if (action == Action.Read)
{
}
else if (action == Action.Write)
{
}
else if (action == Action.None)
{
}
else
{
}


来源:https://stackoverflow.com/questions/44123466/shared-enum-between-multiple-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!