如何解决寻找在我的应用程序中使用关键部分的潜在危害,我在 SO 中阅读了一些答案,这给了我灵感
我写了下面的代码,背后的道理很简单;
- 线程一直运行,直到它在应用程序关闭时终止。
- Add(request1,request2,host,port) 用于向 std::queue 添加元素
- 确保调度请求之间存在时间延迟
- 检查队列的大小,如果大小 > 0 则执行此工作
- 对创建单个线程的 TThreadComponent 的 DispatchRequest。
- 移除(弹出)队列元素
我已经运行了代码,它似乎运行良好并符合预期。
主应用程序在此线程运行时保持响应。
我的问题是,谁能看到我应该注意的潜在问题。
在没有更具体的代码来使用这个线程的情况下,我希望有足够的线程经验的人能够识别潜在的危险并提供改进技巧。
干杯
void __fastcall TSentinal::Execute()
{
//---- Place thread code here -------------------
//
// using std::queue to request reading a modbus
// device via a thread component,component
// creates the reading thread which terminates
// after read,//-----------------------------------------------
if (!InitializeCriticalSectionAndSpinCount(&gateKeeper,0x00000400) )
return;
Priority = tpNormal;
FreeOnTerminate = true;
span.start = clock();
while(!Terminated)
{
//---------------------------------------------------
// wait for millisec_delay between DispatchRequests
// to ensure time for other thread to complete its
// task...
//---------------------------------------------------
millisec_delay = 500;
while(!span.dispatch(millisec_delay))
continue;
if((size_t)q.size() > 0)
{
span.start = clock();
EnterCriticalSection(&gateKeeper);
//-----------------------------------------------
// ensure that request is completed before
// continuing,//
// DispatchRequest() creates thread to read modbus
// device via serial server,on completion,thread
// is terminated,//-----------------------------------------------
FTc->DispatchRequest(false,q.c[0]);
//-----------------------------------------------
// remove from queue
//-----------------------------------------------
q.pop();
LeaveCriticalSection(&gateKeeper);
}
}
DeleteCriticalSection(&gateKeeper);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。