你颁布的法典存在几个严重问题。 在你做任何事情之前,他们必须固定下来。
The worker thread is a busy loop . It will consume all the available CPU cycles and make anything else, such as running a GUI, impossible slow and unresponsive. I suggest adding a call to Sleep() into the while loop, as a quick fix.
主要的透镜和工人都试图同时获得对应变量。 你走了这么长的时间,而且会失事。 你们应当保护反.或等同。
让我们稍作重新设计,并去种植牛肉。 我们需要
A. 导 言 一位工人在校服,第二次(大约)10次,在休息时间睡觉,以便我们能够更新《两性平等倡议》。
B. A GUI thread which checks and displays the counter value twice per second
C. 出席情况 如果q升,GUI透镜还将监测关键板,停止反面。
D. 出席情况 我们将使用wxThreadHelper。 • 管理深层的班子。
主要执行细节如下:
2. 界定主要框架,包括所有轮 the良好
class MyFrame : public wxFrame, public wxThreadHelper
{
public:
MyFrame(const wxString& title);
// worker thread entry point
wxThread::ExitCode Entry();
// keyboard monitor
void OnChar(wxKeyEvent& event );
// display updater
void OnTimer(wxTimerEvent& event);
private:
// the counter
unsigned int mCounter;
// protect the counter from access by both threads at once
wxCriticalSection mCS;
// display update timer
wxTimer * mTimer;
DECLARE_EVENT_TABLE()
};
观察主要板和时间活动
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_CHAR(MyFrame::OnChar)
EVT_TIMER(-1,MyFrame::OnTimer)
END_EVENT_TABLE()
构筑框架
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
, mCounter( 0 )
{
// set the frame icon
SetIcon(wxICON(sample));
CreateStatusBar(2);
SetStatusText("Welcome to Asynchronous Counter!");
// Create thread and start it going
CreateThread();
GetThread()->Run();
// Create update timer and start it going
mTimer = new wxTimer(this);
mTimer->Start(500);
}
工人读写能力每秒10倍
wxThread::ExitCode MyFrame::Entry()
{
// loop, so long as we haven t been asked to quit
while ( ! GetThread()->TestDestroy()) {
{
// increment counter, to a maximum value
wxCriticalSectionLocker lock( mCS );
if( mCounter >= 0xFFFFFFFF )
break;
++mCounter;
}
// Let other things happen for 1/10 second
Sleep(100);
}
// Counter is finished, or we have been asked to stop
return (wxThread::ExitCode)0;
}
发出要求的关键监测板
void MyFrame::OnChar(wxKeyEvent& event )
{
event.Skip();
if( event.GetKeyCode() == (int) q )
GetThread()->Delete();
}
B. 时间火力时的最新显示
void MyFrame::OnTimer(wxTimerEvent& )
{
unsigned int current_counter_value;
{
wxCriticalSectionLocker lock( mCS );
current_counter_value = mCounter;
}
SetStatusText(wxString::Format("Counter = %d",current_counter_value));
}