赛码网大家用过吗
完美世界2018校招编程练习,多线程协作打印。
#include<bits/stdc++.h>using namespace std;mutex mtx;intactive =0;intn;voidprint(intthread_idx,intk ,charc){inti =0;cout << thread_idx << endl;while(i < k) {mtx.lock();if(active == thread_idx){cout << c ;active = (active++)%n;i++;}mtx.unlock();}}intmain(){cin >> n;string s;cin >> s;vector<thread> vth;for(inti =0; i < s.size(); i++){vth.push_back(move(thread(print, i, n, s[i])));}for(auto &t : vth)t.join();return0;}
本地运行的结果正常,为啥在赛码网上就错误呢?如下图
跪求各位大佬指点!!感谢
#完美世界##笔试题目#