什么是编组?当某些东西被“编组”时会发生什么?

发布于 2024-10-31 09:58:37 字数 298 浏览 9 评论 0原文

我知道这个问题至少已经被问过这里< /a>.

但没有一个令人满意的答案,至少对我来说不是。关于与非托管代码互操作的编组有很多讨论,但是从一个线程编组到另一个线程又如何,就像我们有时在 .NET 中必须做的那样。

这让我问,编组到底是什么?当您给出编组的定义时,您将如何定义它,以便它解释互操作性的情况,以及在线程之间“编组”的情况?

I know this question has been asked, at least here.

But there wasn't a satisfactory answer, at least not to me. There is a lot of talk about marshalling as regards interoperating with unmanaged code, but what about marshalling from one thread to another, as we have to do in .NET sometimes.

This makes me ask, what is marshalling, really? When you give a definition of marshalling, how would you define it so that it is explaining the case of interoperability, as well as the cases where you are "marshalling" between threads?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风吹过旳痕迹 2024-11-07 09:58:37

计算通常需要将数据从一个站点移动到另一个站点,并且没有任何共享内存。因此,一个计算将包含数据的消息发送给另一个计算。

如果该数据任意复杂,应该如何在消息中发送?

编组是将数据字段或整组相关结构转换为可以在消息中发送的序列化字符串的过程。编组二进制文件
如果消息格式必须是文本,则可以将其转换为十六进制数字字符串。如果消息将携带二进制数据,则二进制数可能会转换为 4 个小端标准化二进制字节并以这种方式发送。指针比较难;人们经常必须将它们转换为独立于实际内存位置的抽象引用(例如,“节点号”)。

当然,如果你“编组”数据,最终必须“解组”,这是读取串行流并重建传输的数据(结构)的过程。

库中通常有用于实现此目的的(取消)编组例程,有时甚至有一些工具可以生成(取消)编组例程上发送/接收数据所需的所有调用。

Computations often need to move data from one site to another, and don't have any shared memory. So one computation sends a message containing the data to the other.

How should that data, if it is arbitrarily complicated, be sent in a message?

Marshalling is the process of converting a data field, or an entire set of related structures, into a serialized string that can be sent in a message. To marshall a binary
number, one might convert it to hexadecimal digit string, if the message format must be text. If the message will carry binary data, the binary number might be converted into 4 little-endian normalized binary bytes and sent that way. Pointers are harder; one often has to convert them into an abstract reference (e.g., a "node number") that is independent of the actual memory locations.

Of course, if you "marshall" data, you must eventually "unmarshall", which is the process of reading the serial stream and reconstructing the transmitted data (structure).

Often there are (un)marshalling routines in a library that are used to accomplish this purpose, and sometimes there are even tools that will manufacture all the calls needed on the (un)marshalling routines to send/recieve the data.

何以畏孤独 2024-11-07 09:58:37

编组正在获取某种形式的数据,并将其转换为单独的形式。这是一个非常通用的术语,用在很多地方,但含义上有细微的差别。

例如,在 .NET 中,当您使用本机类型时,互操作层会将 .NET 类型中的数据“编组”为适当的形式以调用本机方法,然后将结果“编组”回来。

至于线程之间的“编组” - 通常,您需要让代码在与当前线程不同的线程上运行。例如,如果您使用的是 Windows 窗体,则无法更改线程池线程上的 UI 元素,因此您需要将回调“封送”回 UI 线程。这是通过创建委托并通过 Control.Invoke 将委托传递回用户界面线程(它使用相当复杂的系统将其发送回正确的同步上下文)来完成的,后者又在用户界面上运行委托给你的线程。

Marshalling is taking data, of some form, and translating it into a separate form. It's a very generic term, and used in many places with subtle differences in meaning.

For example, in .NET, the interop layer when you're working with native types "marshals" your data from the .NET type into the appropriate form to call the native method, then "marshals" the results back.

As for "marshalling" between threads - Often, you'll need to have code to run on a different thread than the current one. For example, if you're using Windows Forms, you can't change a UI element on a threadpool thread, so you'll need to "marshal" the call back to the UI thread. This is done by creating a delegate, and passing the delegate back to the user interface thread via Control.Invoke (which uses a rather complex system to post this back to the proper synchronization context), which in turn runs the delegate on the user interface thread for you.

情绪 2024-11-07 09:58:37

维基百科的定义实际上相当不错。

编组的总体概念与“序列化”相同:从内存中表示(在某种程度上,就像根本没有表示一样——当某些东西在内存中时,它只是“存在”)到“硬拷贝” “表示,无论是 XML 还是二进制流或其他东西。但是,根据您正在执行的操作,它也可能意味着对目标格式的某种转换或翻译。

对于进程编组:一个线程并不简单地“调用”另一个线程 - 必须将数据打包并从一个线程“发送”到另一个线程。编组是打包数据(例如,有关要调用的方法及其参数的数据)的过程。

如果您按照互操作性进行编组,则将方法调用及其参数打包到可以发送到运行 COM 组件的进程/线程的数据结构中。该包需要采用 COM 组件可以理解的格式。

Wikipedia's definition is actually pretty good.

The overall concept of marshalling is the same as "serialization:" moving from an in-memory representation (which, in a way, is like no representation at all - when something is in memory it simply "exists") to a "hard copy" representation, whether that's XML or maybe a binary stream or something. However, depending on what you're doing, it can also imply some kind of transformation or translation to a target format.

For process marshalling: one thread doesn't simply "call" another - data has to be packaged up and "sent" from one thread to another. Marshalling is the process of packaging that data (for example, data about the method you want to call, and its parameters).

If you're marshalling in terms of interop, you are packaging up a method call and its parameters into a data structure that can be sent to a process/thread running the COM component. That package needs to be in a format that the COM component can understand.

情绪操控生活 2024-11-07 09:58:37

来自维基百科 - 编组(计算机科学)

编组(类似于序列化)是将对象的内存表示形式转换为适合存储或传输的数据格式的过程。当数据必须在计算机程序的不同部分之间或从一个程序移动到另一个程序时,通常会使用它。

在从 .NET 调用非托管函数的情况下,编组用于将 .NET 的数据转换为非托管函数可以使用的数据。例如,System.String 是基于 Unicode 的,但该字符串可能需要转换为 ANSI 字符串才能传递到非托管 C 函数中。

对于线程,编组通常是指将某些数据的所有权从一个线程转移到另一个线程。例如,一个程序有两个线程。第一个线程从网络读取数据,第二个线程计算该数据。网络线程读取一些数据后,它将数据传输(即“编组”)到计算线程进行处理。它可以通过将数据写入两个线程之间共享的队列来实现这一点。

线程中的编组几乎总是涉及所编组数据的同步。

From Wikipedia - Marshalling (computer science):

Marshalling (similar to serialization) is the process of transforming the memory representation of an object to a data format suitable for storage or transmission. It is typically used when data must be moved between different parts of a computer program or from one program to another.

In the case of calling an unmanaged function from .NET, marshalling is used to convert .NET's data into data that the unmanaged function can consume. For instance, System.String is Unicode based, but that string might need to be converted to an ANSI string to be passed into a unmanaged C function.

For threading, marshalling typically refers to transfer of ownership of some data from one thread to another thread. For example, a program has two threads. The first thread reads data from the network, and the second thread computes that data. After the network thread reads some data it transfers (i.e., "marshals") the data over to computation thread to process. It might do this by writing the data to a queue shared between the two threads.

Marshalling in threading almost always involves synchronization of the data being marshalled.

静谧 2024-11-07 09:58:37

我对编组的理解是,它为您提供了一种在各种操作环境中以一致的方式传输数据的方法。

在将数据从托管代码编组到非托管代码的上下文中,它或多或少是相同的。

我有一些数据,例如整数数组或我选择的任何数据类型,并且我希望在 C++ 代码对其执行一些操作后,可以在我的 C# 代码中使用它。

我不能只是对 C# 代码说“嘿,这就是数组所在的位置,做你想做的事”。 C++ 中整型数组的存储方式可能与 C# 中不同。编组让我们以独立于环境的方式传输这些数据,以便双方都能以相同的方式看到数据。

另一个例子是网络。您通常不会将此称为封送,但如果您想通过网络传输它,则通常必须以这样的方式传输它:无论接收者都以与您相同的方式解释数据。您的计算机可以以小端顺序表示数据,而另一台计算机可以以大端顺序表示数据。

tl;dr:封送处理为您提供了一种在各种操作环境中一致表示数据的方法

The way I understand marshaling is that it provides a way for you to transfer data in a consistent manner across various operating environments.

In the context of marshaling data from managed to unmanaged code, it's more or less the same.

I have some data, say an array of integers or any data type of my choosing, and I want to make it available for use within my C# code after my C++ code does some operations on it.

I can't just say "Hey, this is where the array is, do what you want" to the C# code. An array of ints in C++ may not be stored the same way as in C#. Marshaling let's us transmit this data in an environment independent manner so that either side sees the data the same exact way.

Another example would be in networking. You usually don't call this marshaling, but if you want to transmit it over the network, you have to typically transmit it in such a way that whoever receives it interprets the data the same way you do. Your computer could represent data in little endian order, and the other could represent it in big endian order.

tl;dr: Marshaling provides you a way to consistently represent data across various operating environments

他夏了夏天 2024-11-07 09:58:37

它通常在“以 XML 格式编写”的上下文中使用,但它可以编组为任何格式。

2.  To arrange, place, or set in methodical order.
   (from American Heritage® Dictionary of the English Language)

所以这意味着您正在按照您想要的有条理的顺序/格式排列数据。通常是 XML 格式。

It's usually used in the context of "written in an XML format" but it could be marshalled to any format.

2.  To arrange, place, or set in methodical order.
   (from American Heritage® Dictionary of the English Language)

So it means you're arranging the data in the methodical order/format you want. Often this is in XML format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文