tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
50
Task/Nautical-bell/D/nautical-bell.d
Normal file
50
Task/Nautical-bell/D/nautical-bell.d
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import std.stdio, core.thread, std.datetime;
|
||||
|
||||
class NauticalBell : Thread {
|
||||
private shared bool stopped;
|
||||
|
||||
this() {
|
||||
super(&run);
|
||||
}
|
||||
|
||||
void run() {
|
||||
uint numBells;
|
||||
auto time = cast(TimeOfDay)Clock.currTime();
|
||||
auto next = TimeOfDay();
|
||||
|
||||
void setNextBellTime() {
|
||||
next += minutes(30);
|
||||
numBells = 1 + (numBells % 8);
|
||||
}
|
||||
|
||||
while (next < time)
|
||||
setNextBellTime();
|
||||
|
||||
while (!this.stopped) {
|
||||
time = cast(TimeOfDay)Clock.currTime();
|
||||
if (next.minute == time.minute &&
|
||||
next.hour == time.hour) {
|
||||
immutable bells = numBells == 1 ? "bell" : "bells";
|
||||
writefln("%s : %d %s", time, numBells, bells);
|
||||
setNextBellTime();
|
||||
}
|
||||
sleep(dur!"msecs"(100));
|
||||
yield();
|
||||
}
|
||||
}
|
||||
|
||||
void stop() {
|
||||
this.stopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto bells = new NauticalBell();
|
||||
bells.isDaemon(true);
|
||||
bells.start();
|
||||
try {
|
||||
bells.join();
|
||||
} catch (ThreadException e) {
|
||||
writeln(e.msg);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue