void AnimalSim::run() { auto duck = Item{"duck"}; // Create `Flyweight` objects that can be shared. This auto sheep = Item{"sheep"}; // should probably be done by a factory with a cache. // Create duck 0 duck_colors_.emplace(0, "red"); duck_speeds_.emplace(0, 150.0); // Create duck 1 - has no speed duck_colors_.emplace(0, "green"); size_t num_ducks = 2; // Create sheep 0 - has no color sheep_speeds_.emplace(0, 100.0); size_t num_sheep = 1; // Do something with all the ducks for(size_t i = 0; i != num_ducks; ++i) duck.someOperation(getDuckColor(i), getDuckSpeed(i)); // Do something with all the sheep for(size_t i = 0; i != num_sheep; ++i) sheep.someOperation(getSheepColor(i), getSheepSpeed(i)); }
static void *inc_thr(void *arg) { for(int i=0;i<N;++i) { pthread_mutex_lock(&mutex); int tmp = num; tmp = tmp + 1; int t = rand()%5; for(int j=0;j<t;++j) someOperation(); num = tmp; pthread_mutex_unlock(&mutex); } }