我想在我的mmorpg中使用多线程在c ++中,我现在有5个线程,我想将另外一个分成两个,但我的mmorpg服务器包含大量的向量,并且因为向量没有线程安全写入,我做不好.
有没有替代使用跨线程的向量,或有没有办法使向量读/写多线程安全.
下面是我不想做的一个例子,尝试找到这样的替代方案:显然这不是实际的代码,我只是做一个例子.
//Thread1
//Load monster and send data to the player
globals::monstername[myid];//Myid = 1 for now -.-
senddata(globals::monstername[myid]);//Not the actual networking code, im just lazy.
//Thread2
//Create a monster and manage it
globals::monstername.push_back("FatBlobMonster");
//More managing code i cant be bothered inserting >.<
Run Code Online (Sandbox Code Playgroud)