c# - Interlocked.Exchange和值类型 [英] c# - Interlocked.Exchange and value types

查看:936
本文介绍了c# - Interlocked.Exchange和值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//我想做什么


使用Interlocked.Exchange方法使用枚举类型

建议请


//我对问题的估计


好​​像是Interlocked.Exchange只适用于整数,可引用的对象,

和浮点数


//背景


我有一个包装工作线程来处理各种请求的类

线程函数是作为一个实现的基于循环交换机的状态机

状态机状态被枚举(通过枚举)

请求由客户通过类暴露的各种方法进行请求

请求使用上述枚举体现

请求的枚举状态变量通过互锁交换获取

以供工作线程稍后检索..


//忏悔


适用于C ++和CRTL。我是C#新手


欢呼

// What I want to do

Use enumerated types with the Interlocked.Exchange methods
Suggestions please

// My estimation of problem

Seems like Interlocked.Exchange only works with ints, referencable objects,
and floats

// Background

I have a class that wraps up a worker thread to handle various requests
The thread function is implemeted as a looped switch based state machine
The state machine states are enumerated (via an enum)
Requests are made by the client through various methods exposed by the class
Requests are embodied using the enumeration described above
The requested enum state variable gets filed away via interlocked exchange
for later retrieval by worker thread..

// Confessions

works fine with C++ and CRTL. I''m a C# newbie

cheers

推荐答案

我有一个令我惊讶的解决方法(好吧它至少编译)


c#编译器似乎没有抱怨从枚举转换为

整数类型(顺便说一句,这种行为与C和C ++)..所以我可以将我的c#enum中的
转换为int,然后再使用int重载转换为

Interlocked.Exchange。


我猜测如果我尝试将一个整数

值转换为枚举范围之外的枚举类型,我将得到运行时异常。将试试。


无论如何,如果它有效我很高兴。评论??


" steve"写道:
I have a workaround that surprises me a little (well it compiles at least)

The c# compiler does not appear to complain about casts from enums to
integer types (btw this behaviour is different to C and C++).. So I can cast
from my c# enum to an int and back again using the int overload for
Interlocked.Exchange.

I''m guessing that I''ll get a runtime exception if I try to cast an integer
value to an enum type that is out of range of the enum.. will try.

Anyway, if it works I''m happy. Comments??

"steve" wrote:
//我想做什么

使用Interlocked.Exchange方法使用枚举类型
建议请

//我对问题的估计

好像Interlocked.Exchange只适用于整数,可引用的对象,

//
//

我有一个包装工作线程来处理各种请求的类
线程函数被实现为基于循环交换机的状态机
状态机状态被枚举(通过枚举)<客户通过类暴露的各种方法提出请求
使用上述枚举体现请求
请求的枚举状态变量通过互锁交换进行归档
以便以后检索工作线程..

//忏悔

适用于C ++和CRTL。我是C#newbie

欢呼
// What I want to do

Use enumerated types with the Interlocked.Exchange methods
Suggestions please

// My estimation of problem

Seems like Interlocked.Exchange only works with ints, referencable objects,
and floats

// Background

I have a class that wraps up a worker thread to handle various requests
The thread function is implemeted as a looped switch based state machine
The state machine states are enumerated (via an enum)
Requests are made by the client through various methods exposed by the class
Requests are embodied using the enumeration described above
The requested enum state variable gets filed away via interlocked exchange
for later retrieval by worker thread..

// Confessions

works fine with C++ and CRTL. I''m a C# newbie

cheers



steve< st *** @ discuss.microsoft。 COM>写道:
steve <st***@discussions.microsoft.com> wrote:
我有一个让我感到惊讶的解决方法(好吧它至少编译)

c#编译器似乎没有抱怨从枚举转换为
整数类型(顺便说一句,这个行为与C和C ++不同)..所以我可以使用int重载从我的c#enum转换为int并再次返回
Interlocked.Exchange。

我猜测如果我尝试将一个整数
值转换为超出枚举范围的枚举类型,我将得到一个运行时异常。将尝试。


不,你不会。 Enums在这方面并不严格。

无论如何,如果它有效我很高兴。评论??
I have a workaround that surprises me a little (well it compiles at least)

The c# compiler does not appear to complain about casts from enums to
integer types (btw this behaviour is different to C and C++).. So I can cast
from my c# enum to an int and back again using the int overload for
Interlocked.Exchange.

I''m guessing that I''ll get a runtime exception if I try to cast an integer
value to an enum type that is out of range of the enum.. will try.
No, you won''t. Enums aren''t strict on that front.
Anyway, if it works I''m happy. Comments??




当然 - 为什么不使用volatile变量或锁定?我知道理论性能已经达到了理论性能,但对于大多数应用来说,它只是

- 理论性的。


我只考虑使用Interlocked类,如果我完全知道使用volatile变量或锁是一个真正的性能

瓶颈。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



Sure - why not just use a volatile variable or locking? I know there''s
a theoretical performance hit, but for most applications it''s just that
- theoretical.

I would only consider using the Interlocked class if I absolutely knew
that the use of a volatile variable or a lock was a real performance
bottleneck.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


嗨史蒂夫,


Interlocked.Exchange方法有三个重载版本:


1. public static int Exchange(ref int,int);

2. public static object Exchange(ref object,object);

3. public static float Exchange (ref float,float);


由于你使用的枚举不适合任何一个,你需要

将它投射到批准在将其作为参数传递之前的priate值。


尝试以下代码并且它将起作用:


< CODE>

object someType3 =(object)someType1;

someType2 =(MyEnumType)Interlocked.Exchange(ref someType3,

(object)someType2);

someType1 =(MyEnumType)someType3;

< / CODE>


但我猜它会破坏使用Interlocked.Exchange的目的

操作将不再是* Atomic *操作。


如果您只想要独占的一个线程操作,您可以使用Monitor或Lock。

检查:

http://msdn.microsoft.com/library/de...classtopic.asp


-

干杯,

Rahul Anand


" steve"写道:
Hi steve,

The Interlocked.Exchange Method has three overloaded versions:

1. public static int Exchange(ref int, int);
2. public static object Exchange(ref object, object);
3. public static float Exchange(ref float, float);

Since you are using an enum which does not fit to any of them, you need to
cast it to appropriate value before passing it as parameter.

Try the following code and it will work:

<CODE>
object someType3 = (object) someType1;
someType2 = (MyEnumType)Interlocked.Exchange(ref someType3,
(object)someType2);
someType1 = (MyEnumType)someType3;
</CODE>

But i guess it will defeat the purpose of using Interlocked.Exchange as this
operation will no more be an *Atomic* operation.

If you only want exclusive one thread operation you can use Monitor or Lock.
Check :

http://msdn.microsoft.com/library/de...classtopic.asp

--
Cheers,
Rahul Anand

"steve" wrote:
我有一个让我感到惊讶的解决方法(好吧它至少编译)

c#编译器似乎没有抱怨来自枚举的强制转换整数类型(顺便说一句,这个行为与C和C ++不同)..所以我可以将我的c#enum转换为一个int,然后再使用int重载转换为
Interlocked.Exchange 。

我猜测如果我尝试将一个整数
值转换为超出枚举范围的枚举类型,我将得到运行时异常。将试试。

无论如何,如果它有效我很高兴。评论??

史蒂夫写道:
I have a workaround that surprises me a little (well it compiles at least)

The c# compiler does not appear to complain about casts from enums to
integer types (btw this behaviour is different to C and C++).. So I can cast
from my c# enum to an int and back again using the int overload for
Interlocked.Exchange.

I''m guessing that I''ll get a runtime exception if I try to cast an integer
value to an enum type that is out of range of the enum.. will try.

Anyway, if it works I''m happy. Comments??

"steve" wrote:
//我想做什么

使用Interlocked.Exchange方法使用枚举类型
建议请

//我对问题的估计

好像Interlocked.Exchange只适用于整数,可引用的对象,

//
//

我有一个包装工作线程来处理各种请求的类
线程函数被实现为基于循环交换机的状态机
状态机状态被枚举(通过枚举)<客户通过类暴露的各种方法提出请求
使用上述枚举体现请求
请求的枚举状态变量通过互锁交换进行归档
以便以后检索工作线程..

//忏悔

适用于C ++和CRTL。我是C#新手

欢呼
// What I want to do

Use enumerated types with the Interlocked.Exchange methods
Suggestions please

// My estimation of problem

Seems like Interlocked.Exchange only works with ints, referencable objects,
and floats

// Background

I have a class that wraps up a worker thread to handle various requests
The thread function is implemeted as a looped switch based state machine
The state machine states are enumerated (via an enum)
Requests are made by the client through various methods exposed by the class
Requests are embodied using the enumeration described above
The requested enum state variable gets filed away via interlocked exchange
for later retrieval by worker thread..

// Confessions

works fine with C++ and CRTL. I''m a C# newbie

cheers



这篇关于c# - Interlocked.Exchange和值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆