说future实现隐式同步是什么意思?
What does it mean by saying that future achieves implicit synchronization?
来自Programming Language Pragmatics, by Scott
13.4.5 Implicit Synchronization
In several shared-memory languages, the operations that threads can
perform on shared data are restricted in such a way that
synchronization can be implicit in the operations themselves, rather
than appearing as separate, explicit operations.
We have seen one example of implicit synchronization already: the
forall loop of HPF and Fortran 95 (Example 13.10). Separate iterations
of a forall loop proceed concurrently, semantically in lock-step with
each other: each iteration reads all data used in its instance of the
first assignment statement before any iteration updates its instance of
the left-hand side. The left-hand side updates in turn occur before
any iteration reads the data used in its instance of the second
assignment statement, and so on. Compilation of forall loops for
vector machines, while far from trivial, is more or less
straightforward. On a more conventional multiprocessor, however, good
performance usually depends on high-quality dependence analysis,
which allows the compiler to identify situations in which statements
within a loop do not in fact depend on one another, and can proceed
without synchronization.
Futures
Implicit synchronization can also be achieved without compiler analysis. ...
Using C#’s Task Parallel Library (TPL), we might write
var description = Task.Factory.StartNew(() => GetDescription());
var numberInStock = Task.Factory.StartNew(() => GetInventory());
...
Console.WriteLine("We have " + numberInStock.Result
+ " copies of " + description.Result + " in stock");
Static library class Task.Factory
is used to generate futures, known
as “tasks” in C#. The Create method supports generic type inference,
allowing us to pass a delegate compatible with Func<T>
(function
returning T
), for any T
. We’ve specified the delegates here as
lambda expressions. If GetDescription
returns a String
,
description will be of type Task<String>
; if GetInventory
returns
an int
, numberInStock
will be of type Task<int>
.
本书在隐式同步部分列出了 future,其中还包含 for 循环和 future 中的独立迭代。
"implicit synchronization"是不需要同步吗?
future(如C#的task)实现隐式同步是什么意思?
在 C# 中,执行任务并启动它的主程序继续
彼此之间不需要同步?
Is a future 只有在计算future和
创造未来的主程序不需要同步
彼此之间?
谢谢。
在此上下文中的同步意味着(引自wikipedia):
Process synchronization refers to the idea that multiple processes are
to join up or handshake at a certain point, in order to reach an
agreement or commit to a certain sequence of action
Task
(作为 "future" 概念的示例)提供了一种启动并发进程并在未来获取其结果的方法,通过访问 Result
属性(它不会总是 启动并发进程,但这里没有关系)。因此,通过调用 Result
,您可以实现当前执行线程与 Task
表示的并发执行线程之间的同步(因为您的线程可能会等待任务完成)。这是隐含的,因为您只是访问常规 属性 而不是使用特殊的同步结构。将其与使用 ManualResetEvent
的信号进行比较 - 您实现了相同的目标,但这次是明确的。
来自Programming Language Pragmatics, by Scott
13.4.5 Implicit Synchronization
In several shared-memory languages, the operations that threads can perform on shared data are restricted in such a way that synchronization can be implicit in the operations themselves, rather than appearing as separate, explicit operations.
We have seen one example of implicit synchronization already: the forall loop of HPF and Fortran 95 (Example 13.10). Separate iterations of a forall loop proceed concurrently, semantically in lock-step with each other: each iteration reads all data used in its instance of the first assignment statement before any iteration updates its instance of the left-hand side. The left-hand side updates in turn occur before any iteration reads the data used in its instance of the second assignment statement, and so on. Compilation of forall loops for vector machines, while far from trivial, is more or less straightforward. On a more conventional multiprocessor, however, good performance usually depends on high-quality dependence analysis, which allows the compiler to identify situations in which statements within a loop do not in fact depend on one another, and can proceed without synchronization.
Futures
Implicit synchronization can also be achieved without compiler analysis. ...
Using C#’s Task Parallel Library (TPL), we might write
var description = Task.Factory.StartNew(() => GetDescription()); var numberInStock = Task.Factory.StartNew(() => GetInventory()); ... Console.WriteLine("We have " + numberInStock.Result + " copies of " + description.Result + " in stock");
Static library class
Task.Factory
is used to generate futures, known as “tasks” in C#. The Create method supports generic type inference, allowing us to pass a delegate compatible withFunc<T>
(function returningT
), for anyT
. We’ve specified the delegates here as lambda expressions. IfGetDescription
returns aString
, description will be of typeTask<String>
; ifGetInventory
returns anint
,numberInStock
will be of typeTask<int>
.
本书在隐式同步部分列出了 future,其中还包含 for 循环和 future 中的独立迭代。
"implicit synchronization"是不需要同步吗?
future(如C#的task)实现隐式同步是什么意思?
在 C# 中,执行任务并启动它的主程序继续 彼此之间不需要同步?
Is a future 只有在计算future和 创造未来的主程序不需要同步 彼此之间?
谢谢。
在此上下文中的同步意味着(引自wikipedia):
Process synchronization refers to the idea that multiple processes are to join up or handshake at a certain point, in order to reach an agreement or commit to a certain sequence of action
Task
(作为 "future" 概念的示例)提供了一种启动并发进程并在未来获取其结果的方法,通过访问 Result
属性(它不会总是 启动并发进程,但这里没有关系)。因此,通过调用 Result
,您可以实现当前执行线程与 Task
表示的并发执行线程之间的同步(因为您的线程可能会等待任务完成)。这是隐含的,因为您只是访问常规 属性 而不是使用特殊的同步结构。将其与使用 ManualResetEvent
的信号进行比较 - 您实现了相同的目标,但这次是明确的。