多线程应用程序中的静态函数 [英] Static Functions in a Multi Threaded App

查看:70
本文介绍了多线程应用程序中的静态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多线程应用程序中创建静态函数是否安全

?作为一个例子,我有一个记录错误,异常的功能和

一些信息数据

public static void Log(....)

{

//

}


此应用程序中有多个线程可以调用此函数。

如果每个线程创建一个

类的实例(我会删除静态)并使用它,是否有任何优势?函数本身确实没有访问任何实例变量。如果它是静态的话,是否会有任何线程保证

问题。 ?


谢谢


SRLoka

Is it safe to create a static function in a multi threaded application
? As an example, I have a function that logs errors ,exceptions and
some informational data
public static void Log(....)
{
//
}

There are multiple threads in this application that call this function.
Is there any advantage if each thread creates an instance of the
class(I would remove the static) and uses it ? The function itself does
not access any instance variables. Will there be any thread saefty
issues if it is static. ?

Thank You

SRLoka

推荐答案

SRLoka,

这完全取决于你在函数中做了什么。仅仅因为你没有访问字段(静态或实例),并不意味着你不会在线程问题上运行




最好的办法是将MethodImpl属性放在方法上,

将MethodImplOptions.Synchronized值传递给构造函数。这个

将确保对Log方法的访问是同步的。


如果你在每个线程上创建了一个单独的类实例,它就不会/>
必然有效,因为他们可能都访问同一个文件,你可以

最终在彼此之间交织文件的写入(如果你这样做了) b $ b每次调用Log多次写入。


希望这会有所帮助。

-

- Nicholas Paldino [。 NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" Seenu" <锶***************** @ gmail.com>在消息中写道

news:11 ********************** @ g43g2000cwa.googlegr oups.com ...
SRLoka,

It all depends on what you are doing in the function. Just because you
arent accessing fields (static or instance), doesn''t mean that you won''t run
into threading issues.

Your best bet would be to place the MethodImpl attribute on the method,
passing the MethodImplOptions.Synchronized value into the constructor. This
will ensure that access to the Log method is synchronized.

If you created a separate class instance on each thread, it wouldn''t
necessarily work, since they might all access the same file, and you could
end up interweaving the writes to the file in between each other (if you did
multiple writes per call to Log).

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

"Seenu" <Sr*****************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
在多线程应用程序中创建静态函数是否安全?
作为一个例子,我有一个记录错误,异常和一些信息数据的函数
public static void Log(....)


这个应用程序中有多个线程来调用这个函数。
如果每个线程都创建了一个
类的实例(我会删除静态),有什么好处吗?使用它?函数本身不会访问任何实例变量。如果它是静态的,是否会出现任何线索问题。 ?

谢谢

SRLoka
Is it safe to create a static function in a multi threaded application
? As an example, I have a function that logs errors ,exceptions and
some informational data
public static void Log(....)
{
//
}

There are multiple threads in this application that call this function.
Is there any advantage if each thread creates an instance of the
class(I would remove the static) and uses it ? The function itself does
not access any instance variables. Will there be any thread saefty
issues if it is static. ?

Thank You

SRLoka



我试图想象静态函数将如何在
运行时执行。如果我使用一个实例并且他们都可以访问同一个文件,我可以理解这个问题。但就我而言,我正在登录数据库。

如何仍然会产生线程问题?顺便说一句,这个应用程序运行在一台

多CPU机器上。


谢谢

I am trying to visualize how a static function would be executed at
runtime. I can understand the problem if I use an instance and they all
access the same file. But in my case, I am logging to a database. How
would that still create threading issues ? BTW, this app runs on a
multi cpu machine.

Thanks


Seenu,


嗯,取决于你如何分配连接(你有一个静态级别的连接吗?或者你是
)每次创建一个

迭代函数的时间)。另外,你在方法中执行多个命令

?这些是你必须担心的事情。


基本上,如果你写入数据库,你将创建你的

命令,然后执行它们。由于所有内容都是在堆栈上创建的(至少是对你所做对象的引用),而不是静态字段,

你应该没问题。我只是确保你在交易中包含多个

语句,这样才能保持一致。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Seenu" <锶***************** @ gmail.com>在消息中写道

news:11 ********************** @ g43g2000cwa.googlegr oups.com ...
Seenu,

Well, depending on how you are allocating the connection (do you have
one connection that you hold on a static level, or are you creating one each
time the function is iterated). Also, do you execute more than one command
in the method? These are the things you have to worry about.

Basically, if you are writing to a database, you would create your
commands and then execute them. Since everything is created on the stack
(at least the references to the objects you make), and not to static fields,
you should have no problem. I would just make sure that you wrap multiple
statements in a transaction so that it is consistent.

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

"Seenu" <Sr*****************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
我试图想象一下如何在
运行时执行静态函数。如果我使用实例并且他们都访问同一个文件,我可以理解这个问题。但就我而言,我正在登录数据库。
如何仍然会产生线程问题?顺便说一句,这个应用程序在一台多CPU机器上运行。

谢谢
I am trying to visualize how a static function would be executed at
runtime. I can understand the problem if I use an instance and they all
access the same file. But in my case, I am logging to a database. How
would that still create threading issues ? BTW, this app runs on a
multi cpu machine.

Thanks



这篇关于多线程应用程序中的静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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