voida_mother(void *arg) { aLock.lock(); int delay = 0;
printf("mother: start to make cheese burger, there are %d cheese burger now\n", cheese_burger); // make 10 cheese_burger cheese_burger += 10;
printf("mother: oh, I have to hang clothes out.\n"); // hanging clothes out delay = 0xfffffff; while (delay) --delay; // done
printf("mother: Oh, Jesus! There are %d cheese burgers\n", cheese_burger); aLock.unlock(); }
voida_naughty_boy(void *arg) { aLock.lock(); printf("boy : Look what I found!\n"); // eat all cheese_burgers out secretly cheese_burger -= 10; // run away as fast as possible aLock.unlock(); }
使用SpinLock复现结果:
信号量复现
首先声明信号量:
1 2 3 4 5 6 7 8 9 10 11 12 13
classSemaphore { private: uint32 counter; List waiting; SpinLock semLock;
voida_mother(void *arg) { semaphore.P(); int delay = 0;
printf("mother: start to make cheese burger, there are %d cheese burger now\n", cheese_burger); // make 10 cheese_burger cheese_burger += 10;
printf("mother: oh, I have to hang clothes out.\n"); // hanging clothes out delay = 0xfffffff; while (delay) --delay; // done
printf("mother: Oh, Jesus! There are %d cheese burgers\n", cheese_burger); semaphore.V(); }
voida_naughty_boy(void *arg) { semaphore.P(); printf("boy : Look what I found!\n"); // eat all cheese_burgers out secretly cheese_burger -= 10; // run away as fast as possible semaphore.V(); }
voidphilosopher(int i) { int meals = 0; // 用于计数和限制用餐次数,避免无限循环 do { printf("philosopher %d is thinking\n", i); //同时只能有4人上桌 printf("philosopher %d tries to sit at the table\n", i); table.P(); printf("philosopher %d sits at the table\n", i); printf("philosopher %d tries to pick up left chopstick\n", i); semaphore[i].P(); printf("philosopher %d picks up left chopstick\n", i); // thinking int delay = 0x1FFFFFF; while (delay) --delay; printf("philosopher %d tries to pick up right chopstick\n", i); semaphore[(i + 1) % 5].P(); printf("philosopher %d picks up right chopstick\n", i); // 进餐 printf("philosopher %d is eating...\n", i); delay = 0x3FFFFFF; while (delay) --delay; // 放下筷子 semaphore[i].V(); printf("philosopher %d puts down left chopstick\n", i); semaphore[(i + 1) % 5].V(); printf("philosopher %d puts down right chopstick\n", i); // 离开餐桌,让其他哲学家有机会 table.V(); printf("philosopher %d leaves the table\n", i); meals++; // 增加用餐计数 } while (meals < 3); // 限制每个哲学家用餐3次 printf("philosopher %d finish eating\n", i); }
printf("mother: start to make cheese burger, there are %d cheese burger now\n", cheese_burger); // make 10 cheese_burger cheese_burger += 10;
printf("mother: oh, I have to hang clothes out.\n"); // hanging clothes out delay = 0xfffffff; while (delay) --delay; // done
printf("mother: Oh, Jesus! There are %d cheese burgers\n", cheese_burger); }
voida_naughty_boy(void *arg) { printf("boy : Look what I found!\n"); // eat all cheese_burgers out secretly cheese_burger -= 10; // run away as fast as possible }
voidfirst_thread(void *arg) { // 第1个线程不可以返回 stdio.moveCursor(0); for (int i = 0; i < 25 * 80; ++i) { stdio.print(' '); } stdio.moveCursor(0);