-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpmt1.swift
64 lines (56 loc) · 1.54 KB
/
mpmt1.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// mpmt1.swift: A stupid simple example of Swift threading (async function)
//
// License:
// Apache License, Version 2.0
// History:
// * 2024/05/22 v0.1 Initial version
// Author:
// Masanori Itoh <[email protected]>
// TODO:
// * Add HELP messages.
// References:
// * https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency
// * https://stackoverflow.com/questions/27521832/create-dynamic-size-array-swift
//
import Foundation
func busyWorker(_ duration: Int!) async {
let start_ts = Date().timeIntervalSince1970
while true {
let now_ts = Date().timeIntervalSince1970
if (now_ts - start_ts) > CGFloat(duration) {
print("Expired.")
return
}
}
}
// default values
var num_context:Int? = 3
var duration:Int? = 5
// parse arguments and update parameters
for idx in 0..<CommandLine.arguments.count {
switch idx {
case 1:
num_context = Int(CommandLine.arguments[idx])
break
case 2:
duration = Int(CommandLine.arguments[idx])
break
default:
break
}
}
print("NUM_CONTEXT: \(num_context!) DURATION: \(duration!)")
let task = Task {
try await withThrowingTaskGroup(of: Int.self) { group in
for idx in 1...num_context! {
group.addTask {
print("Creating \(idx) th worker.")
//print(idx)
await busyWorker(duration)
return 0
}
}
try await group.waitForAll()
}
}
_ = await task.result