我有一个异步方法RequestInternalAsync(),它向外部资源发出请求,并希望编写一个包装器方法,通过减少并行性来限制对方法的多个并发异步请求.
第一个选项,我想到的是TaskScheduler用有限的并发(LimitedConcurrencyLevelTaskScheduler,ConcurrentExclusiveSchedulerPair等等).
但是要使用自定义调度程序运行任务,我必须使用TaskFactory仅接受的任务启动任务Action<>,即我不能通过不阻止额外的线程来等待执行内部方法来实现.
第二种选择是SemaphoreSlim,它完成了它的工作,但在这种情况下,我正在实施自我限制,而不是使用TaskScheduler.
static void Main(string[] args)
{
// TESTING 1
var task1 = Task.WhenAll(Enumerable.Range(1, 10).Select(i => RequestAsyncBad()));
task1.Wait();
// TESTING 2
var task2 = Task.WhenAll(Enumerable.Range(1, 10).Select(i => RequestAsyncBetter()));
task2.Wait();
}
private static Task RequestInternalAsync()
{
return Task.Delay(500);
}
Run Code Online (Sandbox Code Playgroud)
解决方案#1:
private static readonly ConcurrentExclusiveSchedulerPair _concurrentPair
= new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, 2);
public static Task RequestAsyncBad()
{
// Dumb: Because TaskFactory doesn't provide an overload which …Run Code Online (Sandbox Code Playgroud) AAD 应用程序中访问图形 API -checkMemberGroups 所需的最低权限集是多少?
这是我尝试使用的权限列表。这个集合可以进一步减少吗?我是否缺少任何强制性权限?
委派权限 -
微软图形 API
阅读所有用户的完整资料
阅读所有组
以登录用户身份访问目录
登录并阅读用户资料
Windows Azure 活动目录
阅读所有组
以登录用户身份访问目录
登录并阅读用户资料
假设我有一个带有两个参数的函数,其中第一个参数是动态的,但第二个参数始终是编译时已知的常量:
uint8_t convert_bcd(uint8_t num, uint8_t mask) {
uint8_t result = mask & 0x0F & num;
if constexpr ((mask & 0xF0) != 0) // mask is known at compile time, can be optimized
result += 10 * ((mask & 0xF0 & num) >> 4);
return result;
}
Run Code Online (Sandbox Code Playgroud)
用法示例
uint8_t result1 = convert_bcd(data[0], 0x7F);
uint8_t result2 = convert_bcd(data[1], 0x3F);
Run Code Online (Sandbox Code Playgroud)
我想内联此函数(如果可能)并告诉编译器 if 条件(仅涉及始终恒定的第二个参数)可以在编译时解析。
我对inline//以及如何在我的场景中const应用constexpr它们以尽可能优化功能感到困惑。
在 C++ 中执行此操作的正确惯用方法是什么?