summaryrefslogtreecommitdiffhomepage
path: root/misc
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-06-06 01:36:54 +0200
committerTyge Løvset <[email protected]>2023-06-06 01:36:54 +0200
commitf2d90c87590133547e474da4ea9d5dd1b834043e (patch)
treedde7bcfbaf2b5d4ec4a84cf8d68f6a7ccc90e6c0 /misc
parent4b8f8d2a97ca1411ad6dccdeac6195574edac852 (diff)
downloadSTC-modified-f2d90c87590133547e474da4ea9d5dd1b834043e.tar.gz
STC-modified-f2d90c87590133547e474da4ea9d5dd1b834043e.zip
Switched to double for times in cco_timer and cco_time, etc.
Reverted to just use Sleep on win32 - same effect.
Diffstat (limited to 'misc')
-rw-r--r--misc/examples/dining_philosophers.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/misc/examples/dining_philosophers.c b/misc/examples/dining_philosophers.c
index cc6e5fd2..57fcef56 100644
--- a/misc/examples/dining_philosophers.c
+++ b/misc/examples/dining_philosophers.c
@@ -32,17 +32,17 @@ void philosopher(struct Philosopher* p)
{
cco_routine(p) {
while (1) {
- int duration = (int)(1000 + crand() % 2000); // 1-3 seconds
- printf("Philosopher %d is thinking for %d minutes...\n", p->id, duration/100);
- cco_timer_await(&p->tm, duration*1000);
+ double duration = 1.0 + crandf()*2.0;
+ printf("Philosopher %d is thinking for %.0f minutes...\n", p->id, duration*10);
+ cco_timer_await(&p->tm, duration);
printf("Philosopher %d is hungry...\n", p->id);
cco_sem_await(p->left_fork);
cco_sem_await(p->right_fork);
- duration = (int)(500 + crand() % 1000);
- printf("Philosopher %d is eating for %d minutes...\n", p->id, duration/100);
- cco_timer_await(&p->tm, duration*1000);
+ duration = 0.5 + crandf();
+ printf("Philosopher %d is eating for %.0f minutes...\n", p->id, duration*10);
+ cco_timer_await(&p->tm, duration);
cco_sem_release(p->left_fork);
cco_sem_release(p->right_fork);
@@ -84,18 +84,20 @@ void dining(struct Dining* d)
}
}
-
int main()
{
struct Dining dine;
cco_reset(&dine);
- cco_timer tm = cco_timer_from(10*1000000); // microseconds
+ int n=0;
+ cco_timer tm = cco_timer_from(10.0); // seconds
csrand((uint64_t)time(NULL));
while (!cco_done(&dine)) {
if (cco_timer_expired(&tm))
cco_stop(&dine);
dining(&dine);
- cco_usleep(100);
+ cco_sleep(0.001);
+ ++n;
}
+ printf("n=%d\n", n);
}