|
|
|
|
|
|
d*******u 发帖数: 186 | 1 Here are 3 programs I would like you to write in C++. Each one builds on the
last.
Step 1: Write a Job Handler class that can handle multiple Requests that are
trying to access a single Resource. Each Resource can only be allocated by
one Request at a time. The requests should be handled in the order in which
they are received by the Job Handler.
Step 2: Extend this class to handle multiple Resources.
Step 3: Do not allow the same Requestor to submit a Request for two
Resources simultaneously. | i*******h 发帖数: 216 | 2 写个简单的意思意思:
class JobHandler
{
public:
JobHandler(ResourceManager*);
~JobHandler();
void Receive(Request&); //receive request and insert into queue, atomic
void Run();
private:
queue m_queue;
ResourceManager* m_ResMgr;
}
//Thread1 function
//This is the main loop of processing thread
void JobHandler::Run()
{
while(true)
{
lock_queue();
if(m_queue.empty())
{
//better to optimize here so that we don't frequently lock/
unlock.
unlock_queue();
continue;
}
Request& req = m_queue.front();
m_queue.pop_front();
unlock_queue();
m_ResMgr->Handle(req);
}
}
//Thread2 function
//call this in receiver thread's main loop.
void JobHandler::Receive(Request& r)
{
//assuming queue won't full. better to handle this.
lock_queue();
m_queue.push_back(r);
unlock_queue();
}
class ResourceManager doesn't need to be multi-threading safe because calls
to it have already been serialized. |
|
|
|
|
|