程序是什么“非线程安全”意思? [英] What does a program being "Not Thread Safe" mean?

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

问题描述

我需要使用以下功能:



[ ^ ]



但是,在备注部分中说,对于Windows版本较少比Windows 8它不是线程安全。这是否仅适用于具有该进程的线程,或者它是否也适用于另一个进程中的线程。我的意思是后者是这样的:如果我的应用程序的另一个实例被破坏,那么缺少线程安全是否适用。



我打算使用信号量来同步进程中的线程。我只是想知道是否需要将所有其他进程中的线程计入我的计划,以使用命名信号量来使用信号量。那么在我的上下文中,非线程安全的函数是什么意思?

I need to use the following function:

[^]

But , it is said int the remark section that for versions of windows less than Windows 8 "It Is Not Thread Safe". Does this apply to threads withing the process only or it also apply to threads in another process. What I mean by the later is this: will the lack of thread safety be applicable if another instance of my application is lunched.

I intend to use a semaphore to synchronize threads within the process. I was just wondering if I need to factor threads in all other process into my plan to use a semaphore by using named semaphore. So what does a function being "Not Thread Safe" mean in this my context?

推荐答案

你的问题不是很有效。这一切都取决于你的意思。线程安全是应用于任意数量线程的概念,无论它们是在同一进程中还是在不同进程中。但这一切都取决于不安全执行的原因。您只需了解这些原因并做出相应决定。某些操作对于不同的线程可能是不安全的,无论它们是什么,但是如果两个线程处于不同的进程中,则创建不安全情况的对象可能永远不会共享,因为这些进程是隔离的。你明白了吗?



首先,请仔细阅读MSDN帮助页面。它说流不是线程安全的,不是函数。我不认为我们在单个快速答案中有足够的空间来详细讨论线程安全的所有方面,但让我们考虑为什么内存流可能不是线程安全的。这是因为它为接口指针的用户提供了对相同内存区域的访问。您可以将相同的指针复制到进程中的另一个线程,这样两个线程就可以访问同一个内存。你必须解释为什么这是不安全的吗?



在这种特殊情况下,如果你只有一个线程使用这个内存,那么问题就不会出现了如果您运行同一个应用程序的另一个实例,则不会出现。您需要了解流程是什么。它们是隔离的,因此系统对象的所有内存地址和句柄都只具有对当前进程有意义的值。即使两个不同进程中的两个指针具有相同的数值,在不同的进程中它们仍然指向不同的物理内存。这是因为,首先,进程访问的内存是虚拟的,这是硬件支持的内存管理技术。当然,应用程序的两个不同实例是两个独立的隔离进程。所以,你不必担心。



同时,这并不意味着你永远不会遇到与缺乏线程安全相关的内存流问题即使你在不同的进程中使用内存。从理论上讲,如果在同一个共享内存对象上创建两个流,则可能会遇到问题:可以通过IPC在两个进程之间共享内存:

https://en.wikipedia.org/wiki/Inter-process_communication [ ^ ],

https://en.wikipedia.org/wiki/Shared_memory_(interprocess_communication) [ ^ ]。



我希望这不是你的情况,但你需要了解背景。







另外,请记住一个简单的线程同步原则:不是同步是最好的同步。有许多方法可以避免线程之间的冲突,而不是同步它们。例如,如果你在堆栈上进行所有计算,只使用函数参数中传递的堆栈内存和内存,而不进行共享,则永远不会在线程之间产生冲突。



它可能需要深入了解对象的存储和绑定方式,线程如何工作,类似的事情。



-SA
Your question is not quite valid. It all depends on what you mean. Thread safety is the concept applied to any number of threads, no matter if they are in the same process or different processes. But it all depends on the reason of unsafe execution. You simply need to understand these reasons and decide accordingly. Some operations can be unsafe for different thread, no matter what they are, but the objects creating unsafe situation might never be shared by two threads if they are in different processes, because the processes are isolated. Are you getting the idea?

First of all, read the MSDN help page thoroughly. It says that the stream is not thread safe, not the function. I don't think we have enough room in a single Quick Answer for detailed discussion of all aspects of thread safety, but let's consider why the memory stream can be not thread safe. This is because it provides the access to the same area of memory to the user of the interface pointer. You can copy the same pointer to another thread in the process, so two threads can access the same memory. Do you have to explain why this is unsafe?

In this particular case, the problem won't appear if you have only one thread working with this memory, and it would not appear if you run another instance of the same application. You need to understand what the processes are. They are isolated, so all memory addresses and handles to system objects have the values only meaningful to the current process. Even if two pointers in two different processes have the same numeric values, in different processes they still point to different physical memory. This is because, first of all, the memory accessed by a process is virtual, which is the hardware-supported memory management technique. And of course two different instances of your applications are two separate isolated processes. So, you should not have to worry.

At the same time, it does not mean that you never face problems with such memory stream related to the lack of thread safety, even if you use memory in different processes. Theoretically speaking, you can run into the problem if you create two stream on the same shared memory object: sharing memory between two processes is possible via IPC:
https://en.wikipedia.org/wiki/Inter-process_communication[^],
https://en.wikipedia.org/wiki/Shared_memory_(interprocess_communication)[^].

I hope this is not your case, but you need to understand the background.



Also, remember one simple principle of thread synchronization: "not synchronization is best synchronization". There are many ways to avoid conflicts between threads instead of synchronizing them. For example, if you do all calculation on stack, using only the stack memory and memory passed in the function arguments, without sharing, you never create conflicts between threads.

It may require deep understanding on how objects are stored and bound, how thread works, things like that.

—SA


线程安全的概念仅适用于单个进程中的线程。因此,即使它们运行相同的应用程序,您也不必关心其他进程中的线程。
The concept of thread-safety does only apply to threads within a single process. So you don't have to care about threads in other processes, even if they are running the same application.


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

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