当前位置:网站首页>D multi production single consumption
D multi production single consumption
2022-07-28 02:35:00 【fqbqrr】
import std.stdio;
import std.concurrency;
import std.algorithm;
import std.range;
import std.exception;
import std.format;
import core.thread;
struct Progress {
Tid tid; // Identity of the reporting thread
size_t amount; // speed of progress
size_t total; // Overall progress
}
void display(Progress[Tid] progresses) {
const amount = progresses.byValue.map!(p => p.amount).sum;
const total = progresses.byValue.map!(p => p.total).sum;
writefln!"%6.2f%%"(100.0 * amount / total);
}
// The worker thread
void download(string url) {
writefln!"%s Thread downloaded %s."(thisTid, url);
enum total = 20;
foreach (i; 0 .. total) {
// Simulate progress
Thread.sleep(100.msecs);
// Report progress to owner
ownerTid.send(Progress(thisTid, i + 1, total));
}
}
void main() {
auto list = [ "dlang.org", "ddili.org" ];
auto downloaders = list.length.iota
.map!(i => spawnLinked(&download, list[i])).array;
Progress[Tid] progresses;
size_t finished = 0;
while (finished != list.length) {
receive(
(LinkTerminated arg) {
++finished;
// Exit or not
enforce((arg.tid in progresses) &&
(progresses[arg.tid].amount == progresses[arg.tid].total),
format!"%s The thread exited unexpectedly "(arg.tid));
},
(Progress progress) {
progresses[progress.tid] = progress;
progresses.display();
}
);
}
writeln(" Handling download files ");
}
边栏推荐
- Interviewer: what is the factory method mode?
- Plato Farm在Elephant Swap上铸造的ePLATO是什么?
- 第三章 队列
- Special network technology virtual host PHP version setting
- Day6 函数和模块的使用
- 【LeetCode】13. Linked List Cycle·环形链表
- With elephant & nbsp; Eplato created by swap, analysis of the high premium behind it
- [机缘参悟-53]:阳谋立身,阴谋防身
- Alipay applet authorization / obtaining user information
- Wechat campus bathroom reservation applet graduation design finished product (1) development outline
猜你喜欢

Hardware standard

When iPhone copies photos to the computer, the device connection often fails and the transmission is interrupted. Here's the way

【愚公系列】2022年07月 Tabby集成终端的使用

这个操作可能不值钱,但却值得学习 | 【图片批量裁剪】
![[advanced ROS] Lecture 9 robot model motion based on rviz and arbotix control](/img/7f/f0360210e8a9f7e45410d79635bfd9.png)
[advanced ROS] Lecture 9 robot model motion based on rviz and arbotix control

Two ways for wechat applet to realize dynamic horizontal step bar

MySQL锁系列之锁算法详解(荣耀典藏版)

LeetCode 热题 HOT 100 -> 1.两数之和
![[机缘参悟-53]:阳谋立身,阴谋防身](/img/93/2f61993770d93d9adc80a9fa89e71c.jpg)
[机缘参悟-53]:阳谋立身,阴谋防身

"The faster the code is written, the slower the program runs"
随机推荐
The virtual host website cannot access the self-test method
Canvas 从入门到劝朋友放弃(图解版)
Alipay applet authorization / obtaining user information
MySQL's way to solve deadlock - lock analysis of common SQL statements
实际工作中,我是如何使用 Postman 做接口测试?
学会这招再也不怕手误让代码崩掉
【软件测试】—— 自动化测试之unittest框架
How to put app on the app store?
Use try-with-resources or close this
0动态规划中等 LeetCode873. 最长的斐波那契子序列的长度
Achievements in science and Technology (XXVIII)
0 dynamic programming medium leetcode873. Length of the longest Fibonacci subsequence
程序里随处可见的interface,真的有用吗?真的用对了吗?
[机缘参悟-53]:阳谋立身,阴谋防身
Three core issues of concurrent programming (glory Collection Edition)
[Yugong series] July 2022 go teaching course 019 - for circular structure
JS event loop synchronous task, asynchronous task (micro task, macro task) problem analysis
Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
MySQL blocking monitoring script
Pytorch optimizer settings