线程同步问题
对于下列程序,当我输入“ InitializeCriticalSection(&g_cs);”时,程序运行时为什么会产生
内存访问错误?
下列程序在两个Static控件中的输出结果为”9999,1165020372“(不同的实例,结果不一样,反正
结果相差很大)这是否因为当”EnterCriticalSection(&g_cs);“将其中的一个线程置于等待
状态时,此线程会很长时间都不会被调度,而导致这样的结果。
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
const int MAX=10000;
long gNumber=0,data=0;
DWORD gTimes[MAX];
CRITICAL_SECTION g_cs;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
DWORD WINAPI Thread1(PVOID pParam);
DWORD WINAPI Thread2(PVOID pParam);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_THREAD, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_THREAD);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_THREAD);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_THREAD;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
TCHAR szBuffer1[20],szBuffer2[20];
switch (message)
{
case WM_INITDIALOG:
CreateThread(NULL,NULL,Thread2,(PVOID)hDlg,NULL,NULL);
CreateThread(NULL,NULL,Thread1,(PVOID)hDlg,NULL,NULL);
return TRUE;
case WM_COMMAND:
int wID=LOWORD(wParam);
switch(wID)
{
case IDOK:
case IDCANCEL:
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
DWORD WINAPI Thread1(PVOID pParam)
{
TCHAR szBuffer1[10];
HWND hDlg=(HWND)pParam;
InitializeCriticalSection(&g_cs);
while(gNumber<MAX)
{
EnterCriticalSection(&g_cs);
gTimes[gNumber]=data++;
wsprintf(szBuffer1,"%d",gTimes[gNumber]);
SetDlgItemText(hDlg,IDC_STATIC1,szBuffer1);//将值显示在Static 控件
gNumber++;
LeaveCriticalSection(&g_cs);
}
return 0;
}
DWORD WINAPI Thread2(PVOID pParam)
{
TCHAR szBuffer2[10];
HWND hDlg=(HWND)pParam;
InitializeCriticalSection(&g_cs);
while(gNumber<MAX)
{
EnterCriticalSection(&g_cs);
gNumber++;
gTimes[gNumber-1]=data++;
wsprintf(szBuffer2,"%d",gTimes[gNumber]);
SetDlgItemText(hDlg,IDC_STATIC2,szBuffer2); //将值显示在Static 控件
LeaveCriticalSection(&g_cs);
}
return 0;
}