Threadsafe值类型 [英] Threadsafe value types

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

问题描述

如果值类型是不可变的,我想它是读线程的线程安全吗?但是没有

线程安全为它分配一个新值(任何值类型都可以真实地

不可变吗?没有给它分配一个全新的值,比如做一个

修改,当没有涉及参考?我不太了解

CLR)


目前整个:


锁定(对象)

{

threadsafevar =新的东西(1,2,3,4,5) ; //这是否需要值

和ref类型?

}


很好,但它会非常好能够*要求*在访问之前锁定了特定的

变量。


谢谢,


John

解决方案

我不确定在这种情况下我知道你的意思是不可变的。字符串是不可变的,因为如果你尝试分配一个字符串,就会创建一个新的
字符串。这意味着生成了一个新的引用。值类型如

int等可以只读,一旦初始化就不能分配。


据我所知,绝对没有迫使程序员使用

锁定的方法。您还应该知道,应该使用volatile关键字

与多线程应用程序一起使用的字段,以防止编译器导致缓存的优化,从而导致无论使用何时出现问题

锁定。如果在多个线程中访问某些内容,如果它是值或引用类型则不会产生

差异。


Thomas P. Skinner [MVP]


" John" < JS ************ @ ecclesdeletethiscollege.ac.uk>在留言中写道

news:uE **************** @ TK2MSFTNGP12.phx.gbl ...

如果是一个值类型是不可变的,我想这是读它的线程安全吗?但是没有
线程安全给它分配一个新值(任何值类型都可以真正地不可变吗?没有为它分配一个全新的值,比如做一个
修改,当没有涉及参考?我不太了解
CLR)

目前整体:

锁定(对象)
{
threadsafevar = new something(1,2,3,4,5); //对于值
和ref类型都需要这个吗?

很好,但是能够*要求*那将是非常好的
特定变量在访问前被锁定。

谢谢,

John



嗨Thomas,


在我看来有很多类型我不能挥发..包括任何结构

我已经制作了,和DateTimes!


我以为lock()删除了''volatile''的需要?


通过不可变值类型我的意思就像DateTime,你不能除了通过创建新实例之外,更改

字段/属性。这似乎被微软的C#boffins视为好东西。那个白板

关于MSDN电视的讲座肯定表明它是(我需要更多)。


然而,由于我对CLR的理解有限,我会我认为:


DateTime a = new DateTime(1970,1,1);

....

a =新的DateTime(1971,1,1);


实际上打破了''a''的不变性。它是一种价值类型,所以我们是否通过做年= 1971这样的事情来有效地改变它?或者

旧''a''是否仍然存在,直到它被GCed?

Thomas P. Skinner [MVP]" < to*@bu.edu>在留言中写道

news:u2 ************** @ TK2MSFTNGP15.phx.gbl ...

我不确定我在这种情况下知道你的意思是不可变的。字符串
是不可变的,因为如果你尝试分配一个字符串,就会创建一个新的字符串。这意味着生成了一个新的引用。值类型
像int等可以只读取一次,并且一旦
初始化就不能分配。

据我所知,绝对没有办法强迫程序员使用
锁。您还应该知道volatile关键字应该与多线程应用程序使用的字段一起使用,以防止编译器优化导致缓存,从而无论锁的使用如何都会出现问题。如果在多个线程中访问某些内容,如果它是值或引用类型则不会产生差异。

Thomas P. Skinner [MVP]

" ;约翰" < JS ************ @ ecclesdeletethiscollege.ac.uk>在消息中写道
新闻:uE **************** @ TK2MSFTNGP12.phx.gbl ...

如果值类型是不可变的,我想这是读它的线程安全吗?但是没有
线程安全给它分配一个新值(任何值类型都可以真正地不可变吗?没有为它分配一个全新的值,比如做一个
修改,当没有涉及参考?我不太了解
CLR)

目前整体:

锁定(对象)
{
threadsafevar = new something(1,2,3,4,5); //对于
值和ref类型都需要这个吗?

很好,但是真的很好能够*要求*那个
特定变量在访问前被锁定。

谢谢,

John




你是对的。结构不能变得易变。任何引用类型都可以作为各种整数,浮点数,字符和枚举。但是它们必须是字段。


至于你的例子,只需将其设为只读。考虑一下:


int i = 1;

int i = 2;


i不是一成不变的,但是1和2是。与DateTime相同。


Thomas P. Skinner [MVP]


" John" < JS ************ @ ecclesdeletethiscollege.ac.uk>在消息中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

嗨Thomas ,

在我看来有很多类型我不能挥发..包括我已经制作的任何结构,以及DateTimes!

我想lock()删除了对''volatile''的需求?

对于不可变值类型,我的意思是DateTime,你不能改变
字段/属性,除非创建一个新实例。这似乎被微软的C#boffins视为好东西。那个白板在MSDN电视上的演讲肯定表明它是(我需要更多)。

然而,由于我对CLR的理解有限,我原以为:

DateTime a = new DateTime(1970,1,1);
...
a = new DateTime(1971,1,1);

实际上是打破''a'的不变性。这是一种价值类型,我们是否通过做年= 1971这样的事情来有效地改变它?或者是旧的''a'仍然存在于某个地方,直到它被GCed?

Thomas P. Skinner [MVP]" < to*@bu.edu>在消息中写道
新闻:u2 ************** @ TK2MSFTNGP15.phx.gbl ...

我不知道我的意思是什么意思在这种情况下由不可变的。字符串
是不可变的,因为如果你尝试分配一个字符串
就会创建一个新的字符串。这意味着生成了一个新的引用。像int等类似的值可以只读取,并且一旦初始化就不能分配。

据我所知,绝对没有办法强迫程序员使用
锁。您还应该知道volatile关键字应该与多线程应用程序使用的字段一起使用,以防止编译器优化导致缓存,从而无论锁的使用如何都会出现问题。如果在多个线程中访问某些内容,如果它是值或引用类型则不会产生差异。

Thomas P. Skinner [MVP]

" ;约翰" < JS ************ @ ecclesdeletethiscollege.ac.uk>在消息中写道
新闻:uE **************** @ TK2MSFTNGP12.phx.gbl ...

如果值类型是不可变的,我想这是读它的线程安全吗?但是没有线程安全来为它分配一个新值(任何值类型都可以真正地不可变吗?没有为它分配一个全新的值,比如做一个
修改,当没有涉及参考?我不太了解
CLR)

目前整体:

锁定(对象)
{
threadsafevar = new something(1,2,3,4,5); //对于
值和ref类型都需要这个吗?

很好,但是真的很好能够*要求*那个
特定变量在访问前被锁定。

谢谢,

John





If a value type is immutable, I guess it''s threadsafe to read it? But not
threadsafe to assign a new value to it (can any value type be truely
immutable? Isn''t assigning a totally new value to it, like doing an
modification, when no references are involved? I don''t know enough about
CLR)

At the moment the whole:

lock(anobject)
{
threadsafevar = new something(1,2,3,4,5); // is this needed for both value
and ref types?
}

Is fine, but it would be really good be able to *require* that a particular
variable was locked before access.

Thanks,

John

解决方案

I am not sure I know what you mean by immutable in this context. Strings are
immutable by virtue of the fact that if you try to assign to a string a new
string is created. This means a new reference is generated. Value types like
int etc. can be made readonly and can''t be assigned to once initialized.

As far as I know there is absolutely no way to force a programmer to use
lock. You should also be aware that the volatile keyword should be used
with fields used with multi-threaded applications to prevent compiler
optimizations that cause caching and hence problems regardless of the use of
the lock. If something is accessed in more than one thread it make no
difference if it is a value or reference type.

Thomas P. Skinner [MVP]

"John" <js************@ecclesdeletethiscollege.ac.uk> wrote in message
news:uE****************@TK2MSFTNGP12.phx.gbl...

If a value type is immutable, I guess it''s threadsafe to read it? But not
threadsafe to assign a new value to it (can any value type be truely
immutable? Isn''t assigning a totally new value to it, like doing an
modification, when no references are involved? I don''t know enough about
CLR)

At the moment the whole:

lock(anobject)
{
threadsafevar = new something(1,2,3,4,5); // is this needed for both value
and ref types?
}

Is fine, but it would be really good be able to *require* that a
particular variable was locked before access.

Thanks,

John



Hi Thomas,

There seem to me many types I can''t make volatile.. including any structs
I''ve made, and DateTimes!

I thought lock() removed the need for ''volatile''?

By immutable value types I meant ones like DateTime, where you can''t alter
fields / properties, except by creating a new instance. This seems to be
seen as ''a good thing'' by the C# boffins at Microsoft. That whiteboard
lecture on MSDN TV certainly indicated it was (I need to get out more).

However, with my limited understanding of CLR, I would have thought that:

DateTime a = new DateTime(1970,1,1);
....
a = new DateTime(1971,1,1);

is actually breaking the immutability of ''a''. It''s a value type, so are we
effectively changing it by doing something like ''year = 1971''? Or would the
old ''a'' still exist somewhere until it''s GCed?
"Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message
news:u2**************@TK2MSFTNGP15.phx.gbl...

I am not sure I know what you mean by immutable in this context. Strings
are immutable by virtue of the fact that if you try to assign to a string a
new string is created. This means a new reference is generated. Value types
like int etc. can be made readonly and can''t be assigned to once
initialized.

As far as I know there is absolutely no way to force a programmer to use
lock. You should also be aware that the volatile keyword should be used
with fields used with multi-threaded applications to prevent compiler
optimizations that cause caching and hence problems regardless of the use
of the lock. If something is accessed in more than one thread it make no
difference if it is a value or reference type.

Thomas P. Skinner [MVP]

"John" <js************@ecclesdeletethiscollege.ac.uk> wrote in message
news:uE****************@TK2MSFTNGP12.phx.gbl...

If a value type is immutable, I guess it''s threadsafe to read it? But not
threadsafe to assign a new value to it (can any value type be truely
immutable? Isn''t assigning a totally new value to it, like doing an
modification, when no references are involved? I don''t know enough about
CLR)

At the moment the whole:

lock(anobject)
{
threadsafevar = new something(1,2,3,4,5); // is this needed for both
value and ref types?
}

Is fine, but it would be really good be able to *require* that a
particular variable was locked before access.

Thanks,

John




You are correct. A struct can''t be made volatile. Any reference type can as
well as various ints, floats, char and enum. They must be fields however.

As to your example, Just make it readonly. Consider this:

int i=1;
int i=2;

i is not immutable, but the 1 and the 2 are. Same thing with DateTime.

Thomas P. Skinner [MVP]

"John" <js************@ecclesdeletethiscollege.ac.uk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

Hi Thomas,

There seem to me many types I can''t make volatile.. including any structs
I''ve made, and DateTimes!

I thought lock() removed the need for ''volatile''?

By immutable value types I meant ones like DateTime, where you can''t alter
fields / properties, except by creating a new instance. This seems to be
seen as ''a good thing'' by the C# boffins at Microsoft. That whiteboard
lecture on MSDN TV certainly indicated it was (I need to get out more).

However, with my limited understanding of CLR, I would have thought that:

DateTime a = new DateTime(1970,1,1);
...
a = new DateTime(1971,1,1);

is actually breaking the immutability of ''a''. It''s a value type, so are we
effectively changing it by doing something like ''year = 1971''? Or would
the old ''a'' still exist somewhere until it''s GCed?
"Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message
news:u2**************@TK2MSFTNGP15.phx.gbl...

I am not sure I know what you mean by immutable in this context. Strings
are immutable by virtue of the fact that if you try to assign to a string
a new string is created. This means a new reference is generated. Value
types like int etc. can be made readonly and can''t be assigned to once
initialized.

As far as I know there is absolutely no way to force a programmer to use
lock. You should also be aware that the volatile keyword should be used
with fields used with multi-threaded applications to prevent compiler
optimizations that cause caching and hence problems regardless of the use
of the lock. If something is accessed in more than one thread it make no
difference if it is a value or reference type.

Thomas P. Skinner [MVP]

"John" <js************@ecclesdeletethiscollege.ac.uk> wrote in message
news:uE****************@TK2MSFTNGP12.phx.gbl...

If a value type is immutable, I guess it''s threadsafe to read it? But
not threadsafe to assign a new value to it (can any value type be truely
immutable? Isn''t assigning a totally new value to it, like doing an
modification, when no references are involved? I don''t know enough about
CLR)

At the moment the whole:

lock(anobject)
{
threadsafevar = new something(1,2,3,4,5); // is this needed for both
value and ref types?
}

Is fine, but it would be really good be able to *require* that a
particular variable was locked before access.

Thanks,

John





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

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