由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - job handler Qs
相关主题
请教个C++编程思路凌晨的飞机,第一个travel的onsite
HR Director needed -- Santa Clara, CATwo C++ questions from Bloomberg on-site
问几道老题C++ Q33: typedef (B4_20)
Char x[] = "abc"; 是在heap还是stack上? (转载)C++ Q48: illegal operation (C33)
为什么C++的constructor出错可以抛出异常,而destructor出错C++ Q66: reverse a string -- is it efficient
A malloc/free question using C/C++Two classic C++ question, how to answer
A problem about Heap and Stack.Bloomberg面经+个人找工作小感
问个C++题Goldman Sachs IT Position at Jersey City, New Jersey (转载)
相关话题的讨论汇总
话题: queue话题: request话题: step话题: handler话题: job
进入JobHunting版参与讨论
1 (共1页)
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.
1 (共1页)
进入JobHunting版参与讨论
相关主题
Goldman Sachs IT Position at Jersey City, New Jersey (转载)为什么C++的constructor出错可以抛出异常,而destructor出错
问一个C的简单问题A malloc/free question using C/C++
问个static的问题 (转载)A problem about Heap and Stack.
c++ grill - how to dynamically allocate memory on stack?问个C++题
请教个C++编程思路凌晨的飞机,第一个travel的onsite
HR Director needed -- Santa Clara, CATwo C++ questions from Bloomberg on-site
问几道老题C++ Q33: typedef (B4_20)
Char x[] = "abc"; 是在heap还是stack上? (转载)C++ Q48: illegal operation (C33)
相关话题的讨论汇总
话题: queue话题: request话题: step话题: handler话题: job