Is this the best way to implement a concurrent integer in C# [duplicate]

 internal class Employee
 {
     internal int concurrentState; 

     internal void SetConcurrentState(int concurrentStatus) 
     { 
         Interlocked.Exchange(ref this.concurrentState, concurrentStatus);
     }
     internal int GetConcurrentState() 
     { 
         return Interlocked.CompareExchange(ref this.concurrentState, 
                                            0, 
                                            -this.concurrentState); 
     }
}

Like the title says, is this the best way to implement a concurrent (atomic) integer in C#? I only need to get a value so the convoluted get method defeats the comparison operation. It seems that Microsoft would have provided a better way.

Addendum: @mjwills posted a link to the C Sharp spec and from it is this…

9.6 Atomicity of variable references Reads and writes of the following data types shall be atomic: bool, char, byte, sbyte, short, ushort,
uint, int, float, and reference types. In addition, reads and writes
of enum types with an underlying type in the previous list shall also
be atomic. Reads and writes of other types, including long, ulong,
double, and decimal, as well as user-defined types, need not be
atomic. Aside from the library functions designed for that purpose,
there is no guarantee of atomic read-modify-write, such as in the case
of increment or decrement.

However a CLI document says atomic ops are are garanteed only on the condition of proper word boundary alignment in linked this text…

A conforming CLI shall guarantee that read and write access of
built-in primitive value types and pointers to properly aligned memory
locations no larger than the native word size (the size of type native
int) is atomic (see §I.12.6.2) when all the write accesses to a
location are the same size.

Second Addendum: This answer to a similar question points out that later changing an int to a long might break atomicity of a read. So a good reason to use Interlocked.CompareExchange is to avoid relying on the inherent atomicity of a reaon on an int that might get revised to a long. This is now a closed question but if I were to answer it, I would answer that this is a good method and the risk of breakage was probably what I was thinking about when I wrote it (way back when). I would clean it up by using the equivalent Interlocked.CompareExchange(ref v, 0, 0).

25

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Is this the best way to implement a concurrent integer in C# [duplicate]

 internal class Employee
 {
     internal int concurrentState; 

     internal void SetConcurrentState(int concurrentStatus) 
     { 
         Interlocked.Exchange(ref this.concurrentState, concurrentStatus);
     }
     internal int GetConcurrentState() 
     { 
         return Interlocked.CompareExchange(ref this.concurrentState, 
                                            0, 
                                            -this.concurrentState); 
     }
}

Like the title says, is this the best way to implement a concurrent (atomic) integer in C#? I only need to get a value so the convoluted get method defeats the comparison operation. It seems that Microsoft would have provided a better way.

Addendum: @mjwills posted a link to the C Sharp spec and from it is this…

9.6 Atomicity of variable references Reads and writes of the following data types shall be atomic: bool, char, byte, sbyte, short, ushort,
uint, int, float, and reference types. In addition, reads and writes
of enum types with an underlying type in the previous list shall also
be atomic. Reads and writes of other types, including long, ulong,
double, and decimal, as well as user-defined types, need not be
atomic. Aside from the library functions designed for that purpose,
there is no guarantee of atomic read-modify-write, such as in the case
of increment or decrement.

However a CLI document says atomic ops are are garanteed only on the condition of proper word boundary alignment in linked this text…

A conforming CLI shall guarantee that read and write access of
built-in primitive value types and pointers to properly aligned memory
locations no larger than the native word size (the size of type native
int) is atomic (see §I.12.6.2) when all the write accesses to a
location are the same size.

Second Addendum: This answer to a similar question points out that later changing an int to a long might break atomicity of a read. So a good reason to use Interlocked.CompareExchange is to avoid relying on the inherent atomicity of a reaon on an int that might get revised to a long. This is now a closed question but if I were to answer it, I would answer that this is a good method and the risk of breakage was probably what I was thinking about when I wrote it (way back when). I would clean it up by using the equivalent Interlocked.CompareExchange(ref v, 0, 0).

25

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật