
标题: 子线程循环10次,然后主线程循环100次,然后返回到子线程循环10次,然后再次返回主线程循环100次,因此循环50次,尝试写代码p>
子线程和主线程必须具有条件(标志== num),不满足该条件的线程无法获取unique_lock(将在等待中释放),只有满足条件的线程才能获取锁并执行程序<
mutex m;//保护条件的互斥访问 condition_variable cond;//条件变量 int flag = 10;//条件 void fun(int num) { for (int i = 0; i<50; i++) { unique_lock<mutex> lk(m);//A unique lock is an object that manages a mutex object with unique ownership in both states: locked and unlocked. while (flag != num) cond.wait(lk);//在调用wait时会执行lk.unlock() for (int j = 0; j<num; j++) cout << j << " "; cout << endl; flag = (num == 10) ? 100 : 10; cond.notify_one();//被阻塞的线程唤醒后lk.lock()恢复在调用wait前的状态 } } int main() { thread child(fun, 10); fun(100); child.join(); system("pause"); return 0; }
标题: 编写一个程序并启动三个线程. 这三个线程的ID是A,B和C. 每个线程在屏幕上打印其ID 10次. 输出必须是ABC顺序. 显示;例如: ABCABC ....递归.

mutex m; condition_variable cond; int loop = 10; int flag = 0; void func(int id) { for (int i = 0; i < loop; ++i) { unique_lock<mutex> lk(m); while (flag != id) cond.wait(lk); cout << static_cast<char>('A' + id) << " "; flag = (flag + 1) % 3; cond.notify_all(); } } void main() { thread A(func, 0); thread B(func, 1); func(2); cout << endl; A.join(); B.join(); system("pause"); }
问题(google书面问题): 有四个线程1、2、3、4. 线程1的功能是输出1,线程2的功能是输出2,依此类推...现在有有四个文件ABCD. 最初都是空的. 现在,使这四个文件具有以下格式:
A: 1 2 3 4 1 2 ....
B: 2 3 4 1 2 3 ....

C: 3 4 1 2 3 4 ....
D: 4 1 2 3 4 1 ....
mutex m; condition_variable cond; int loop = 10; int flag; void func(int num) { for (int i = 0; i < loop; ++i) { unique_lock<mutex> lk(m); while (num != flag) cond.wait(lk); cout << num + 1 << " "; flag = (flag + 1) % 4; cond.notify_all(); } } void main(int argc,char *argv[]) { flag = atoi(argv[1]); thread one(func, 1); thread two(func, 2); thread three(func, 3); func(0); one.join(); two.join(); three.join(); cout << endl; system("pause"); }
读者作家问题

这也是一个非常经典的多线程主题. 主题如下: 一个写程序的读者很多,并且多个读者可以同时读取该文件,但是写程序不允许写程序的读者在读取文件时读取该文件. 时间作家无法写作.
class rwlock { private: mutex _lock; condition_variable _wcon, _rcon; unsigned _writer, _reader; int _active; public: void read_lock() { unique_lock<mutex> lock(_lock); ++_reader; while (_active < 0 || _writer > 0) _rcon.wait(lock); --_reader; ++_active; } void write_lock() { unique_lock<mutex> lock(_lock); ++_writer; while (_active != 0) _wcon.wait(lock); --_writer; _active = -1; } void unlock() { unique_lock<mutex> lock(_lock); if (_active > 0) { --_active; if (_active == 0) _wcon.notify_one(); } else { _active = 0; if (_writer > 0) _wcon.notify_one(); else if (_reader > 0) _rcon.notify_all(); } } rwlock() :_writer(0), _reader(0), _active(0) { } }; void t1(rwlock* rwl) { while (1) { cout << "I want to write." << endl; rwl->write_lock(); cout << "writing..." << endl; this_thread::sleep_for(chrono::seconds(5)); rwl->unlock(); this_thread::sleep_for(chrono::seconds(5)); } } void t2(rwlock* rwl) { while (1) { cout << "t2-I want to read." << endl; rwl->read_lock(); cout << "t2-reading..." << endl; this_thread::sleep_for(chrono::seconds(1)); rwl->unlock(); } } void t3(rwlock* rwl) { while (1) { cout << "t3-I want to read." << endl; rwl->read_lock(); cout << "t3-reading..." << endl; this_thread::sleep_for(chrono::seconds(1)); rwl->unlock(); } } int main() { rwlock* rwl = new rwlock(); thread th1(t1, rwl); thread th2(t2, rwl); thread th3(t3, rwl); th1.join(); th2.join(); th3.join(); system("pause"); return 0; }
线程安全队列
STL中的队列不是线程安全的. 组合操作: front(); pop()首先读取head元素c 多线程面试题,然后删除head元素. 如果多个线程执行此组合操作,则可能会发生执行. 该序列将交替执行,从而导致某些意外行为. 因此c 多线程面试题,需要重新设计线程安全队列接口.

template<typename T> class threadsafe_queue { private: mutable std::mutex mut; std::queue<T> data_queue; std::condition_variable data_cond; public: threadsafe_queue() {} threadsafe_queue(threadsafe_queue const& other) { std::lock_guard<std::mutex> lk(other.mut); data_queue = other.data_queue; } void push(T new_value)//入队操作 { std::lock_guard<std::mutex> lk(mut); data_queue.push(new_value); data_cond.notify_one(); } void wait_and_pop(T& value)//直到有元素可以删除为止 { std::unique_lock<std::mutex> lk(mut); data_cond.wait(lk, [this] {return !data_queue.empty(); }); value = data_queue.front(); data_queue.pop(); } std::shared_ptr<T> wait_and_pop() { std::unique_lock<std::mutex> lk(mut); data_cond.wait(lk, [this] {return !data_queue.empty(); }); std::shared_ptr<T> res(std::make_shared<T>(data_queue.front())); data_queue.pop(); return res; } bool try_pop(T& value)//不管有没有队首元素直接返回 { std::lock_guard<std::mutex> lk(mut); if (data_queue.empty()) return false; value = data_queue.front(); data_queue.pop(); return true; } std::shared_ptr<T> try_pop() { std::lock_guard<std::mutex> lk(mut); if (data_queue.empty()) return std::shared_ptr<T>(); std::shared_ptr<T> res(std::make_shared<T>(data_queue.front())); data_queue.pop(); return res; } bool empty() const { std::lock_guard<std::mutex> lk(mut); return data_queue.empty(); } };
标题: 编写程序以完成以下功能:
1)有一个int类型的全局变量g_Flag,其初始值为0
2)在主线程中启动线程1,打印“ this is thread1”,并将g_Flag设置为1
3)在主线程中启动线程2,打印“ this is thread2”,并将g_Flag设置为2
4)程2退出之后,行程序1需要退出
5)主线程在检测到g_Flag从1变为2或从2变为1时退出.
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-226439-1.html
得了吧
值得提倡
主权都没了