这个线程安全吗? [英] Is this thread safe?

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

问题描述



我知道根据MSDN,XslTransform'的Transform是线程安全的,

而且Load不是。因此我将这个简单的Mutex用于它并且

只想确认这是可以的。

XslTransform xslt = null;

Mutex mut = new Mutex();


公共覆盖字符串MapMessage(string messageSource)

{


/ /

//如果需要,创建并加载一个新的转换


if(xslt == null)

{

if(File.Exists(configFile))

{

mut.WaitOne();


xslt = new XslTransform();

xslt.Load(configFile);


mut.ReleaseMutex();

} < br $> b $ b其他

{

throw(新的ApplicationException("配置错误,

XSL文件不存在。) ; + configFile));

}

}


//

//其余的这里的功能代码


}


I know that XslTransform''s Transform is thread safe according to the MSDN,
and that Load is not. I''ve therefore applied this simply Mutex to it and
would just like to confirm this is okay.
XslTransform xslt = null;
Mutex mut = new Mutex();

public override string MapMessage ( string messageSource )
{

//
// create and load up a new transform if required

if ( xslt == null )
{
if ( File.Exists(configFile) )
{
mut.WaitOne();

xslt = new XslTransform();
xslt.Load( configFile );

mut.ReleaseMutex();
}
else
{
throw ( new ApplicationException ( "Configuration error,
XSL file does not exist. " + configFile ) );
}
}

//
// rest of the function code here

}

推荐答案

我知道这是错的!刚刚意识到它可以给我的问题......

这个怎么样?

XslTransform xslt = null;

Mutex mut = new Mutex( );


公共覆盖字符串MapMessage(字符串messageSource)

{


//

//如果需要,创建并加载一个新的转换


mut.WaitOne();


if(xslt == null )

{

if(File.Exists(configFile))

{


xslt = new XslTransform();

xslt.Load(configFile);


}

else

{

mut.ReleaseMutex();

throw(新的ApplicationException("配置错误,

XSL文件不存在。)+ configFile));

}

}


mut.ReleaseMutex();


//

//这里的其余功能代码


}


Dan Bass < danielbass [at] postmaster [dot] co [dot] uk>在留言中写道

新闻:eT ************** @ TK2MSFTNGP14.phx.gbl ...
I know that is wrong! Just realised the problem it could give me...
What about this?
XslTransform xslt = null;
Mutex mut = new Mutex();

public override string MapMessage ( string messageSource )
{

//
// create and load up a new transform if required

mut.WaitOne();

if ( xslt == null )
{
if ( File.Exists(configFile) )
{

xslt = new XslTransform();
xslt.Load( configFile );

}
else
{
mut.ReleaseMutex();
throw ( new ApplicationException ( "Configuration error,
XSL file does not exist. " + configFile ) );
}
}

mut.ReleaseMutex();

//
// rest of the function code here

}

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:eT**************@TK2MSFTNGP14.phx.gbl...

我知道XslTransform的变换是根据MSDN的线程安全,
并且Load不是。因此我将这个简单的Mutex应用于它并且
只想确认这是可以的。

XslTransform xslt = null;
Mutex mut = new Mutex();

公共覆盖字符串MapMessage(string messageSource)
{

//
//如果需要,创建并加载新的转换

if(xslt == null)
{
if(File.Exists(configFile))
{
mut.WaitOne();

xslt = new XslTransform();
xslt.Load(configFile);

mut.ReleaseMutex();
}

{
throw(新的ApplicationException("配置
错误,XSL文件不存在。)+ configFile));
}
}
// //
//其余的功能代码

}

I know that XslTransform''s Transform is thread safe according to the MSDN,
and that Load is not. I''ve therefore applied this simply Mutex to it and
would just like to confirm this is okay.
XslTransform xslt = null;
Mutex mut = new Mutex();

public override string MapMessage ( string messageSource )
{

//
// create and load up a new transform if required

if ( xslt == null )
{
if ( File.Exists(configFile) )
{
mut.WaitOne();

xslt = new XslTransform();
xslt.Load( configFile );

mut.ReleaseMutex();
}
else
{
throw ( new ApplicationException ( "Configuration
error, XSL file does not exist. " + configFile ) );
}
}

//
// rest of the function code here

}



Dan,


我不会使用Mutex。我只是让方法同步,比如

所以:


[MethodImpl(MethodImplOptions.Synchronized)]

公共覆盖字符串MapMessage(string messageSource)

{

//如果需要,创建并加载一个新的转换

mut.WaitOne();


if(xslt == null)

{

if(File.Exists(configFile))

{

xslt = new XslTransform();

xslt.Load(configFile);

}

}

else

{

throw(新的ApplicationException("配置错误,XSL文件

不存在。) ; + configFile));

}


//这里的其余功能代码

}


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


Dan Bass < danielbass [at] postmaster [dot] co [dot] uk>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
Dan,

I wouldn''t use a Mutex. I would just make the method synchronized, like
so:

[MethodImpl(MethodImplOptions.Synchronized)]
public override string MapMessage ( string messageSource )
{
// create and load up a new transform if required
mut.WaitOne();

if ( xslt == null )
{
if ( File.Exists(configFile) )
{
xslt = new XslTransform();
xslt.Load( configFile );
}
}
else
{
throw ( new ApplicationException ( "Configuration error, XSL file
does not exist. " + configFile));
}

// rest of the function code here
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
我知道那是错的!刚刚意识到它可以给我的问题......
这是怎么回事?

XslTransform xslt = null;
Mutex mut = new Mutex();

public override string MapMessage(string messageSource)
//
//如果需要,创建并加载一个新的转换

mut。 WaitOne();

if(xslt == null)
{
if(File.Exists(configFile))


xslt = new XslTransform();
xslt.Load(configFile);

}

{
mut.ReleaseMutex();
throw(new ApplicationException(" Configuration
错误,XSL文件不存在。)+ configFile));
}


mut.ReleaseMutex( );

//
//其余的功能代码在这里

}

Dan Bass < danielbass [at] postmaster [dot] co [dot] uk>在消息中写道
新闻:eT ************** @ TK2MSFTNGP14.phx.gbl ...
I know that is wrong! Just realised the problem it could give me...
What about this?
XslTransform xslt = null;
Mutex mut = new Mutex();

public override string MapMessage ( string messageSource )
{

//
// create and load up a new transform if required

mut.WaitOne();

if ( xslt == null )
{
if ( File.Exists(configFile) )
{

xslt = new XslTransform();
xslt.Load( configFile );

}
else
{
mut.ReleaseMutex();
throw ( new ApplicationException ( "Configuration
error, XSL file does not exist. " + configFile ) );
}
}

mut.ReleaseMutex();

//
// rest of the function code here

}

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:eT**************@TK2MSFTNGP14.phx.gbl...

我知道XslTransform'根据MSDN,转换是线程安全的,而Load不是。因此,我已经将这个简单的Mutex应用于它,并且只想确认这是可以的。

XslTransform xslt = null;
Mutex mut = new Mutex();

公共覆盖字符串MapMessage(string messageSource)
{

//
//如果需要,创建并加载新的转换

if(xslt == null)
{
if(File.Exists(configFile))
{
mut.WaitOne();

xslt = new XslTransform();
xslt.Load(configFile);

mut.ReleaseMutex();
}

{
throw(新的ApplicationException("配置
错误,XSL文件不存在。)+ configFile));
}
}
// //
//其余的功能代码

}

I know that XslTransform''s Transform is thread safe according to the
MSDN, and that Load is not. I''ve therefore applied this simply Mutex to
it and would just like to confirm this is okay.
XslTransform xslt = null;
Mutex mut = new Mutex();

public override string MapMessage ( string messageSource )
{

//
// create and load up a new transform if required

if ( xslt == null )
{
if ( File.Exists(configFile) )
{
mut.WaitOne();

xslt = new XslTransform();
xslt.Load( configFile );

mut.ReleaseMutex();
}
else
{
throw ( new ApplicationException ( "Configuration
error, XSL file does not exist. " + configFile ) );
}
}

//
// rest of the function code here

}




嗨丹,

我不会使用Mutex。我建议使用Monitor。有区域情侣

选项。最常见的是使用C#lock语句,但这有一些

的缺点。最值得注意的是,它相当于一个带有

无限超时的Monitor.TryEnter。因此,许多聪明人建议使用

Monitor.TryEnter而不是有限超时。


你可能想查看文章和实用程序库作者:Jon Skeet:
http:// www .yoda.arachsys.com / csharp / ... ernative.shtml

和Ian G撰写的相关博客文章:
http://www.interact-sw.co.uk/iangblo ... retimedlocking
http://www.interact-sw.co.uk/iangblo.../03/23/locking


此外,如果你在你的代码上编码自己使用Monitor.TryEnter,确保你获得显示器后立即启动try块,然后在finally块中退出

监视器。


Th上面的链接将为您提供完整的详细信息,甚至一些准备好使用

使用代码。


问候,

Mountain


" Dan Bass" < danielbass [at] postmaster [dot] co [dot] uk>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
Hi Dan,
I wouldn''t use a Mutex. I would recommend using Monitor. There area couple
options. The most common is to use the C# lock statement, but this has some
drawbacks. Most notably, it is equivalent to a Monitor.TryEnter with an
infinite timeout. Therefore, a lot of the smart guys recommend using
Monitor.TryEnter with a finite timeout instead.

You may want to check out the articles and utility library by Jon Skeet at:
http://www.yoda.arachsys.com/csharp/...ernative.shtml

and a related blog article by Ian G at:
http://www.interact-sw.co.uk/iangblo...retimedlocking
http://www.interact-sw.co.uk/iangblo.../03/23/locking

Also, if you code this on your own using Monitor.TryEnter, make sure you
start a try block immediately after you get the monitor and then exit the
monitor in a finally block.

The links above will give you full details on this and even some ready to
use code.

Regards,
Mountain

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
我知道那是错的!刚刚意识到它可以给我的问题......
这是怎么回事?

XslTransform xslt = null;
Mutex mut = new Mutex();

public override string MapMessage(string messageSource)
//
//如果需要,创建并加载一个新的转换

mut。 WaitOne();

if(xslt == null)
{
if(File.Exists(configFile))


xslt = new XslTransform();
xslt.Load(configFile);

}

{
mut.ReleaseMutex();
throw(new ApplicationException(" Configuration
error,XSL file not exists。" + configFile));
}


mut.ReleaseMutex( );
/> //
//其余的功能代码在这里

}

Dan Bass < danielbass [at] postmaster [dot] co [dot] uk>在消息中写道
新闻:eT ************** @ TK2MSFTNGP14.phx.gbl ...
I know that is wrong! Just realised the problem it could give me...
What about this?
XslTransform xslt = null;
Mutex mut = new Mutex();

public override string MapMessage ( string messageSource )
{

//
// create and load up a new transform if required

mut.WaitOne();

if ( xslt == null )
{
if ( File.Exists(configFile) )
{

xslt = new XslTransform();
xslt.Load( configFile );

}
else
{
mut.ReleaseMutex();
throw ( new ApplicationException ( "Configuration error, XSL file does not exist. " + configFile ) );
}
}

mut.ReleaseMutex();

//
// rest of the function code here

}

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:eT**************@TK2MSFTNGP14.phx.gbl...

我知道XslTransform'根据
MSDN,转换是线程安全的,而Load不是。因此我将这个简单的Mutex应用于它并且
只想确认这是可以的。

XslTransform xslt = null;
Mutex mut = new Mutex();

公共覆盖字符串MapMessage(string messageSource)
{

//
//如果需要,创建并加载新的转换

if(xslt == null)
{
if(File.Exists(configFile))
{
mut.WaitOne();

xslt = new XslTransform();
xslt.Load(configFile);

mut.ReleaseMutex();
}

{
throw(新的ApplicationException("配置
错误,XSL文件不存在。)+ configFile));
}
}
// //
//这里的其余功能代码

}

I know that XslTransform''s Transform is thread safe according to the MSDN, and that Load is not. I''ve therefore applied this simply Mutex to it and
would just like to confirm this is okay.
XslTransform xslt = null;
Mutex mut = new Mutex();

public override string MapMessage ( string messageSource )
{

//
// create and load up a new transform if required

if ( xslt == null )
{
if ( File.Exists(configFile) )
{
mut.WaitOne();

xslt = new XslTransform();
xslt.Load( configFile );

mut.ReleaseMutex();
}
else
{
throw ( new ApplicationException ( "Configuration
error, XSL file does not exist. " + configFile ) );
}
}

//
// rest of the function code here

}




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

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