题意理解
两个线程交替打印两个方法n次,每次打印先后次序一样
问题分析
其他
链接
class FooBar {
private:
int n;
pthread_mutex_t one;
pthread_mutex_t two;
public:
FooBar(int n) {
this->n = n;
pthread_mutex_init(&one,NULL);
pthread_mutex_init(&two,NULL);
pthread_mutex_lock(&two);
}
void foo(function<void()> printFoo) {
for (int i = 0; i < n; i++) {
// printFoo() outputs "foo". Do not change or remove this line.
pthread_mutex_lock(&one);
printFoo();
pthread_mutex_unlock(&two);
}
}
void bar(function<void()> printBar) {
for (int i = 0; i < n; i++) {
// printBar() outputs "bar". Do not change or remove this line.
pthread_mutex_lock(&two);
printBar();
pthread_mutex_unlock(&one);
}
}
};