本文介绍了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

推荐答案






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

- 理论性的。


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

瓶颈。


-

Jon Skeet - < sk *** @ pobox.com>


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



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




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

08-23 02:15