我通常使用一个名为Simulation的类来前进时间。我没有使用C++,但是我已经在Java中进行了线程化,这可以加快时间并根据时间表(或计划时间的随机事件)激活事件。您可以将其转化为C++或用于查看面向对象的实现是如何实现的。
package adventure;
public class Simulation extends Thread {
private PriorityQueue prioQueue;
Simulation() {
prioQueue = new PriorityQueue();
start();
}
public void wakeMeAfter(Wakeable SleepingObject, double time) {
prioQueue.enqueue(SleepingObject, System.currentTimeMillis() + time);
}
public void run() {
while (true) {
try {
sleep(5);
if (prioQueue.getFirstTime() <= System.currentTimeMillis()) {
((Wakeable) prioQueue.getFirst()).wakeup();
prioQueue.dequeue();
}
} catch (InterruptedException e) {
}
}
}
}
要使用它,你只需实例并添加你的对象:
` Simulation sim = new Simulation();
// Load images to be used as appearance-parameter for persons
Image studAppearance = loadPicture("Person.gif");
// --- Add new persons here ---
new WalkingPerson(sim, this, "Peter", studAppearance);
http://www.cplusplus.com/reference/ctime/有什么问题? – thiagoh 2013-02-25 04:35:20
它会完成这项工作。但是如果你采取这种方法,它会让你对熟悉互斥体的人产生一些伤心。 – 2013-02-25 04:36:05
为什么有这样的线程比替代方案更好? – 2013-02-25 04:40:08