哪位大神能帮忙把下边ACE的代码改成非阻塞的呢,但是必须保证map表里边有玩家数据超过一条,多谢,在线急等!

#include "ace/Thread.h"
#include "ace/synch.h"
#include <iostream>
#include <map>
using namespace std;
ACE_Thread_Mutex mutex;
ACE_Condition<ACE_Thread_Mutex> cond(mutex);
struct SPlayerData
{
UINT32 playerID;
UINT32 playerLevel;
};
map<UINT32, SPlayerData>test_map;
SPlayerData player[5];
void* worker(void*arg)
{
ACE_OS::sleep(2);
mutex.acquire();
ACE_OS::sleep(1);

player[0].playerID = 10001;
player[0].playerLevel = 1;
player[1].playerID = 10002;
player[1].playerLevel = 1;
player[2].playerID = 10003;
player[2].playerLevel = 2;
player[3].playerID = 10004;
player[3].playerLevel = 2;
player[4].playerID = 10005;
player[4].playerLevel = 3;

for (int i = 0; i < 5; ++i)
{
test_map[player[i].playerID] = player[i];
}

if (test_map.size() > 10)
{
cout << "produce" << endl;
cond.signal();
}
mutex.release();
return NULL;
}

void* eater(void*arg)
{
mutex.acquire();
cond.wait();
for (int i = 0; i < 5; ++i)
{
test_map[player[i].playerID] = player[i];
cout << "playerID:"<<player[i].playerID << " " << endl;
cout << "playerLevel:" << player[i].playerLevel << " " << endl;
}
mutex.release();
return NULL;
}

int main(int argc, char*argv)
{
ACE_Thread::spawn((ACE_THR_FUNC)worker);
ACE_OS::sleep(1);
ACE_Thread::spawn((ACE_THR_FUNC)eater);
while (true)
ACE_OS::sleep(10);
return 0;
}[/i][/i][/i][/i]
已邀请:

要回复问题请先登录注册