std::launch

std::launch

Defined in header <future> - 定义于头文件 <future>

launch [lɔːntʃ]:v. 发射 (导弹、火箭等),发起,发动,使...下水,开始,起飞 n. 发射,发行,投放市场,下水,汽艇

1. std::launch

enum class launch;

since C++11

enum class launch : /* unspecified */ {
    async =    /* unspecified */,
    deferred = /* unspecified */,
    /* implementation-defined */
};

Launching policy for async - 启动异步策略

This enum class type is a bitmask type that defines the launching policy in calls to async.
此枚举类类型是位掩码类型,用于定义异步调用中的启动策略。

It can be any combination of the following values:
它可以是以下值的任意组合:

launch label int value description
std::launch::async unspecified Asynchronous: The function is called asynchronously by a new thread and synchronizes its return with the point of access to the shared state.
std::launch::deferred unspecified Deferred: The function is called at the point of access to the shared state.

Asynchronous (异步): 该函数由新线程异步调用,通过共享状态的访问点的返回值进行同步。
Deferred (延迟): 该函数在访问共享状态时被调用。

Specifies the launch policy for a task executed by the std::async function. std::launch is an enumeration used as BitmaskType.
指定 std::async 所指定的任务的的运行策略。std::launch 是用作位掩码类型 (BitmaskType) 的枚举。

std::launch::async - a new thread is launched to execute the task asynchronously (运行新线程,以异步执行任务)
std::launch::deferred - the task is executed on the calling thread the first time its result is requested (lazy evaluation)
(首次请求其结果时在调用线程上执行任务 (惰性求值))

In addition, implementations are allowed to:
另外,允许实现:

define additional bits and bitmasks to specify restrictions on task interactions applicable to a subset of launch policies, and enable those additional bitmasks for the first (default) overload of std::async.
定义额外的位和位掩码,以指定可应用于运行策略子集的任务交互上的限制,并对 std::async 的首个 (默认) 重载启用这些额外位掩码。

The unspecified equivalent int values shall denote individual bits, allowing several labels to be combined in a single bitmask. When multiple values of this type are combined, the function automatically selects one (depending on their particular library implementation).
未指定的等效 int 值应表示单个位,从而允许将多个标签组合在单个位掩码中。当多个此类型的值组合在一起时,该函数将自动选择一个 (取决于它们的特定库实现)。

Library implementations may define additional labels of this type for different launching policies that may describe restrictions on task interaction by different calls.
库实现可以为不同的启动策略定义此类型的其他标签,这些标签可以描述不同调用对任务交互的限制。

conform [kənˈfɔːm]:vi. 符合,遵照,适应环境 vt. 使遵守,使一致,使顺从 adj. 一致的,顺从的
dereference [ˌdiːˈrefrəns]:v. 间接引用,间接访问,解引用
reallocate [ˌriːˈæləkeɪt]:v. 重新分配,再指派

4. Examples

4.1 std::launch

//============================================================================
// Name        : std::async
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

// launch::async vs launch::deferred
#include <iostream>     // std::cout
#include <future>       // std::async, std::future, std::launch
#include <chrono>       // std::chrono::milliseconds
#include <thread>       // std::this_thread::sleep_for

void print_ten(char c, int ms)
{
	for (int i = 0; i < 10; ++i)
	{
		std::this_thread::sleep_for(std::chrono::milliseconds(ms));
		std::cout << c;
	}
}

int main()
{
	std::cout << "with launch::async:\n";
	std::future<void> foo = std::async(std::launch::async, print_ten, '*', 100);
	std::future<void> bar = std::async(std::launch::async, print_ten, '@', 200);

	// async "get" (wait for foo and bar to be ready):
	foo.get();
	bar.get();
	std::cout << "\n\n";

	std::cout << "with launch::deferred:\n";
	foo = std::async(std::launch::deferred, print_ten, '*', 100);
	bar = std::async(std::launch::deferred, print_ten, '@', 200);

	// deferred "get" (perform the actual calls):
	foo.get();
	bar.get();
	std::cout << '\n';

	return 0;
}

scramble ['skræmb(ə)l]:n. 争夺,争抢,爬,抢占 v. 争夺,争抢,(迅速而吃力地) 爬,抢占
with launch::async:
*@**@**@**@**@*@@@@@

with launch::deferred:
**********@@@@@@@@@@

4.2 std::launch



destruction [dɪˈstrʌkʃn]:n. 破坏,毁灭,摧毁
trivially [ˈtrɪviəli]:adv. 琐细地,平凡地,无能地
destructible [dɪ'strʌktɪb(ə)l]:adj. 可破坏的,易损坏的
assignment [ə'saɪnmənt]:n. 任务,布置,赋值

References

http://www.cplusplus.com/reference/future/launch/

发布了454 篇原创文章 · 获赞 1733 · 访问量 103万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104426739
std