只是一段模擬多個(gè)售票點(diǎn)同時(shí)賣(mài)票的演示程序,代碼如下class TicketShop{char name;static int remains;mutex myMutex;public:TicketShop(char n){ name = n; }void sell();};int TicketShop::remains = 100;void TicketShop::sell(){while (1){myMutex.lock();if (remains > 0){remains--;cout << remains << "\t" << name << endl; }else{myMutex.unlock();break;}myMutex.unlock();}}結(jié)果編譯時(shí)報(bào)錯(cuò),說(shuō)error C2280: 'std::mutex::mutex(const std::mutex &)' : attempting to reference a deleted function發(fā)現(xiàn)如果把第五行mutex myMutex 的生命移到類(lèi)外,即改成全局變量,就沒(méi)有這個(gè)問(wèn)題。
請(qǐng)問(wèn)這里把mutex作為類(lèi)的成員變量為什么就不行呢?應(yīng)當(dāng)如何修改?
慕標(biāo)琳琳
2023-03-03 14:10:48