什么"线程安全"真正的意思......在实践中 [英] What "thread safe" really means...In Practical terms

查看:118
本文介绍了什么"线程安全"真正的意思......在实践中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,请忍受我的新手问题。

Dear all, please bear with my newbie questions..

我试图用ghostscript的将PDF转换为PNG,与ASP.NET和C#。不过,我也读了ghostscript的不是线程安全的。所以我的问题是:

I was trying to convert PDF to PNG using ghostscript, with ASP.NET and C#. However, I also read that ghostscript is not thread safe. So my questions are:

1)到底是什么的ghostscript不是线程安全的意思是在实践中?什么样的影响它有,如果我用它在现场ASP.NET(C ++)网络与许多并发用户的应用程序在同一时间访问它?

1) What exactly does "ghostscript is not thread safe" mean in practical terms? What impact does it have if I use it in a live ASP.NET(aspx) web application with many concurrent users accessing it at the same time?

2)我也是从其他网站读到的ghostscript的主要特点版本。 8.63是多线程渲染。这是否意味着我们的线程安全问题现在解决了吗?是ghostscript的线程安全的吧?

2) I also read from another site that the major feature of ghostscript ver. 8.63 is multithreaded rendering. Does this mean our thread safe issue is now resolved? Is ghostscript thread safe now?

3)我也是评估来自PDFTron,这应该是线程安全的PDF2Image。但是,每个CPU的许可证并不便宜。难道是值得付出额外的钱用于线程安全与不安全?

3) I am also evaluating PDF2Image from PDFTron, which is supposed to be thread safe. But the per CPU license doesn't come cheap. Is it worth paying the extra money for "thread safe" vs "not safe"?

大大AP preciate您的指教,谢谢。

Greatly appreciate your advise, thank you.

推荐答案

由于集合,例如,是不是threasafe:

Given that a Collection, for instance, is not threasafe:

var myDic = new Dictionary<string, string>();

在一个multhread环境中,这将引发:

In a multhread environment, this will throw:

string s = null;
if (!myDic.TryGetValue("keyName", out s)) {
    s = new string('#', 10);
    myDic.Add("keyName", s);
}

作为一个线程正在试图将KeyValuePair添加到字典myDic,另一个可TryGetValue()。由于集合不能被读取,并在同一时间写的,就会发生异常。

As one thread is working trying to add the KeyValuePair to the dictionary myDic, another one may TryGetValue(). As Collections can't be read and written at the same time, an Exception will occur.

然而,在另一方面,如果你试试这个:

However, on the other hand, if you try this:

// Other threads will wait here until the variable myDic gets unlocked from the preceding thread that has locked it.
lock (myDic) {
    string s = null;
    if (!myDic.TryGetValue("keyName", out s)) {
        s = new string('#', 10);
        myDic.Add("keyName", s);
    }
} // The first thread that locked the myDic variable will now release the lock so that other threads will be able to work with the variable.

突然,第二个线程试图获得相同的是keyName的键值不会将其添加到字典中的第一个线程已经添加了。

Then suddenly, the second thread trying to get the same "keyName" key value will not have to add it to the dictionary as the first thread already added it.

因此​​,在短期,线程意味着一个对象支持正在使用多个线程在同一时间,还是会适当锁定线程你,没有你不必担心threadsafety。

So in short, threadsafe means that an object supports being used by multiple threads at the same time, or will lock the threads appropriately for you, without you having to worry about threadsafety.

2 我不认为GhostScript的现在是线程安全的。这是majorly使用多线程来执行它的任务,所以这使得它提供一个更高的性能,这就是全部。

2. I don't think GhostScript is now threadsafe. It is majorly using multiple threads to perform its tasks, so this makes it deliver a greater performance, that's all.

3。根据自己的预算和需求,它可能是值得的。但是,如果你围绕包装,你也许可以只锁定(),其中可以很方便地做到这一点,或者如果你不使用多线程自己,这显然是不值得支付threadsafety。这仅仅意味着如果应用程序使用多线程,那么你不会吃亏的不是线程库的后果。除非你真的multihread,这是不值得付出一个线程库。

3. Depending on your budget and your requirements, it may be worthy. But if you build around wrapper, you could perhaps only lock() where it is convenient to do so, or if you do not use multithreading yourself, it is definitely not worth to pay for threadsafety. This means only that if YOUR application uses multithreading, then you will not suffer the consequences of a library not being threadsafe. Unless you really multihread, it is not worth paying for a threadsafe library.

这篇关于什么&QUOT;线程安全&QUOT;真正的意思......在实践中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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