线程安全?? [英] Thread safety ??

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

问题描述

这个疑问是关于Singleton模式代码中的同步问题

of C#


我创建了一个类


公共密封类SecuriteManager

{

私有静态易失性SecurityManager实例;

私有静态对象syncRoot = new Object();


私有SecurityManager(){}


public static SecurityManager GetInstance

{

get {

if(null == instance){

lock(syncRoot){

if(instance == null)

instance = new SecurityManager();

}

}

返回实例;

}

}


public bool IsAllowed(string UserName)

{

//这里有一个很长的过程。

//例如访问可能需要3秒的网络服务。

//HttpContext.Current.Session[UserName] = returnValueFromWebservice;

}

}

现在当2个用户交流时cess IsAllowed同时,是进程线程

安全吗?

或者我应该在我的函数中为每个调用使用锁定


public bool IsAllowed(string UserName)

{

lock(syncRoot)

{

//这里有一个很长的过程。

//例如访问可能需要3秒的Web服务。

//HttpContext.Current.Session[UserName] = returnValueFromWebservice; < br $>
}

}


请建议。

This doubt is regarding synchronisation question in Singleton pattern code
of C#

I had created a class as

public sealed class SecuriteManager
{
private static volatile SecurityManager instance;
private static object syncRoot = new Object();

private SecurityManager() { }

public static SecurityManager GetInstance
{
get{
if(null == instance){
lock(syncRoot){
if (instance == null)
instance = new SecurityManager();
}
}
return instance;
}
}

public bool IsAllowed(string UserName)
{
//A very long process here.
//For example access to a webservice which might take 3 seconds.
//HttpContext.Current.Session[UserName] = returnValueFromWebservice;
}
}
Now when 2 users access IsAllowed at the same time, is the process thread
safe ?
Or should I use lock for each call in my function as

public bool IsAllowed(string UserName)
{
lock(syncRoot)
{
//A very long process here.
//For example access to a webservice which might take 3 seconds.
//HttpContext.Current.Session[UserName] = returnValueFromWebservice;
}
}

Please suggest.

推荐答案

支持< an ******* @ discussion.microsoft.com>写道:
Support <an*******@discussions.microsoft.com> wrote:
这个疑问是关于C单声道模式代码中的同步问题

我创建了一个类

公共密封class SecuriteManager
{private static volatile SecurityManager实例;
私有静态对象syncRoot = new Object();
私有SecurityManager(){}
<公共静态SecurityManager GetInstance
{
get {
if(null == instance){
lock(syncRoot){
if(instance == null)
instance = new SecurityManager();
}
}
返回实例;
}
}


任何理由使用这种复杂的模式而不是简单的模式


http://www.pobox.com/~skeet/csharp/singleton.html




public bool IsAllowed(string UserName)
{
//这是一个非常漫长的过程。
//例如,访问可能需要3秒的Web服务。
//HttpContext.Current.Session[UserName] = returnValueFromWebservice;
}

现在当2个用户同时访问IsAllowed时,进程线程是否安全?
This doubt is regarding synchronisation question in Singleton pattern code
of C#

I had created a class as

public sealed class SecuriteManager
{
private static volatile SecurityManager instance;
private static object syncRoot = new Object();

private SecurityManager() { }

public static SecurityManager GetInstance
{
get{
if(null == instance){
lock(syncRoot){
if (instance == null)
instance = new SecurityManager();
}
}
return instance;
}
}
Any reason to use this complicated pattern rather than a simple one as
specified on

http://www.pobox.com/~skeet/csharp/singleton.html

?
public bool IsAllowed(string UserName)
{
//A very long process here.
//For example access to a webservice which might take 3 seconds.
//HttpContext.Current.Session[UserName] = returnValueFromWebservice;
}
}
Now when 2 users access IsAllowed at the same time, is the process thread
safe ?




嗯,这取决于IsAllowed *实际*的作用。两个线程将

当然可以同时调用它,但对于很多东西来说,这很好。另一方面,如果IsAllowed需要读取并且

从单例中写出一些变量,它可能*不是线程安全的。


说这个方式:成为单身人士的一部分并不重要。如果它b / b
通常可以让两个线程在

时间执行你的方法,那很好 - 否则你需要像你一样需要锁定

其他地方。


-

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

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



Well, that depends on what IsAllowed *actually* does. Two threads will
certainly be able to call it at the same time, but for many things
that''s just fine. If, on the other hand, IsAllowed needs to read and
write some variables from the singleton, it may *not* be threadsafe.

Put it this way: being part of a singleton isn''t relevant here. If it
would normally be okay for two threads to execute your method at a
time, that''s fine - otherwise you''ll need locking just as you would
elsewhere.

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





你应该看看Jon Skeet关于单身人士的文章
http:// www。 yoda.arachsys.com/csharp/singleton.html 它详细解释了

如何以更有效的方式使其保持线程安全。


关于你的IsAllowed方法,只要你不使用任何实例

变量就可以了,如果使用实例变量你应该小心

同步。访问它。


干杯,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局

支持 <一个******* @ discussions.microsoft.com>在消息中写道

news:et ************** @ TK2MSFTNGP09.phx.gbl ...
Hi,

You should take a look at Jon Skeet article about singleton in
http://www.yoda.arachsys.com/csharp/singleton.html It explain in details how
to make it thread safe in the more efficient way.

Regarding your IsAllowed method, as long as you don''t use any instance
variable you are fine, if you use an instance variable you should take care
of sync. the access to it.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Support" <an*******@discussions.microsoft.com> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
这个疑问与同步有关单例模式代码中的问题C#
我已经创建了一个类作为

公共密封类SecuriteManager
私有静态不稳定的SecurityManager实例;
私有静态对象syncRoot = new Object();
私有SecurityManager(){}
公共静态SecurityManager GetInstance
{
get {
if(null == instance){
lock(syncRoot){
if(instance == null)
instance = new SecurityManager();
}
}
返回实例;
}

公共bool IsAllowed(字符串UserName)
{
// A这里有很长的过程。
//例如访问webservice可能需要3秒。
//HttpContext.Current.Session[UserName] =
returnValueFromWebservice; }

现在当2个用户同时访问IsAllowed时,进程线程是否安全?
或者我应该在我的函数中为每个调用使用lock as

公共bool IsAllowed(字符串UserName)
{
lock(syncRoot)
{
//这是一个非常漫长的过程。
//例如访问可能需要3秒的Web服务。
//HttpContext.Current.Session[UserName] =
returnValueFromWebservice; }


请建议。
This doubt is regarding synchronisation question in Singleton pattern code
of C#

I had created a class as

public sealed class SecuriteManager
{
private static volatile SecurityManager instance;
private static object syncRoot = new Object();

private SecurityManager() { }

public static SecurityManager GetInstance
{
get{
if(null == instance){
lock(syncRoot){
if (instance == null)
instance = new SecurityManager();
}
}
return instance;
}
}

public bool IsAllowed(string UserName)
{
//A very long process here.
//For example access to a webservice which might take 3 seconds.
//HttpContext.Current.Session[UserName] = returnValueFromWebservice; }
}
Now when 2 users access IsAllowed at the same time, is the process thread
safe ?
Or should I use lock for each call in my function as

public bool IsAllowed(string UserName)
{
lock(syncRoot)
{
//A very long process here.
//For example access to a webservice which might take 3 seconds.
//HttpContext.Current.Session[UserName] = returnValueFromWebservice; }
}

Please suggest.



我看不到伪-code值得一看。


如果方法没有访问共享资源,那么

锁定是没有理由的。 HttpContext.Current对于向您的应用程序发送

HTTP请求并且不在用户之间共享的每个用户都是唯一的。


此外,对于只是阅读那里很少需要锁定,如果对象

不是从应用程序中的其他线程写入的。


- -

Patrik L?wendahl [C#MVP]
www.cshrp .net - 诙谐的程序员优雅代码


"支持" <一个******* @ discussions.microsoft.com>在消息中写道

news:et ************** @ TK2MSFTNGP09.phx.gbl ...
I can''t see anything in that pseudo-code worth looking.

If the method isn''t accessing shared resources, there''s no reason for
locking. The HttpContext.Current will be unique for each user who sent a
HTTP request to your application and aren''t shared between users.

Also, for just reading there''s seldom any need for locking, if the object
isn''t written to from some other thread in your application.

--
Patrik L?wendahl [C# MVP]
www.cshrp.net - "Elegant code by witty programmers"

"Support" <an*******@discussions.microsoft.com> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
这个疑问与同步有关单例模式代码中的问题C#
我已经创建了一个类作为

公共密封类SecuriteManager
私有静态不稳定的SecurityManager实例;
私有静态对象syncRoot = new Object();
私有SecurityManager(){}
公共静态SecurityManager GetInstance
{
get {
if(null == instance){
lock(syncRoot){
if(instance == null)
instance = new SecurityManager();
}
}
返回实例;
}

公共bool IsAllowed(字符串UserName)
{
// A这里有很长的过程。
//例如访问webservice可能需要3秒。
//HttpContext.Current.Session[UserName] =
returnValueFromWebservice; }

现在当2个用户同时访问IsAllowed时,进程线程是否安全?
或者我应该在我的函数中为每个调用使用lock as

公共bool IsAllowed(字符串UserName)
{
lock(syncRoot)
{
//这是一个非常漫长的过程。
//例如访问可能需要3秒的Web服务。
//HttpContext.Current.Session[UserName] =
returnValueFromWebservice; }

请建议。
This doubt is regarding synchronisation question in Singleton pattern code
of C#

I had created a class as

public sealed class SecuriteManager
{
private static volatile SecurityManager instance;
private static object syncRoot = new Object();

private SecurityManager() { }

public static SecurityManager GetInstance
{
get{
if(null == instance){
lock(syncRoot){
if (instance == null)
instance = new SecurityManager();
}
}
return instance;
}
}

public bool IsAllowed(string UserName)
{
//A very long process here.
//For example access to a webservice which might take 3 seconds.
//HttpContext.Current.Session[UserName] = returnValueFromWebservice; }
}
Now when 2 users access IsAllowed at the same time, is the process thread
safe ?
Or should I use lock for each call in my function as

public bool IsAllowed(string UserName)
{
lock(syncRoot)
{
//A very long process here.
//For example access to a webservice which might take 3 seconds.
//HttpContext.Current.Session[UserName] = returnValueFromWebservice; }
}

Please suggest.



这篇关于线程安全??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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