0
我在Visual C++ 2010中有一個項目,我必須繪製一些圓圈和線條。圓的座標取決於兩個全局變量。全局變量由兩個函數修改,每個函數都在自己的線程中運行。 Boost用於多線程。但是,一旦我運行線程,我的主線程就會被阻塞,從而阻止我繪製形狀並使用全局變量。我怎樣才能解決這個問題?我最終要實現的是,在自己的線程中運行兩個不同的功能修改全局變量,並同時使用上述全球varibales從線程函數修改全局變量並仍然運行主線程以使用全局變量
global_variable_1
global_variable_2
void function_1()
{
while(true)
{
//modifies global_variable_1
}
}
void function_2()
{
while(true)
{
//modifies global_variable_2
}
}
void MyOnPaint(HDC hdc)
{
Graphics graphics(hdc);
Pen pen(Color::Blue);
/* Uses global_variable_1 & global_variable_2 to
draw circles */
}
int APIENTRY _tWinMain(......)
{
/* Some initial code */
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GAZEPOINTEVALUATION));
/*Start threads*/
using namespace boost;
thread thread_1(function_1);
thread thread_2(function_2);
//Start threads
thread_1.join()
thread_2.join()
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
/* Some code */
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
/*CALL MY DRAWING METHOD*/
MyOnPaint(hdc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
/* Some code */
default:
/* Some code */
}
return 0;
}
因爲線程問題,我不介意讀全局變量的髒值。忘了在提問中提到這一點。 – SParanagama 2013-03-11 15:02:00