线程同步问题

vincentmars 2002-03-19 11:31:56
对于下列程序,当我输入“ 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;
}

...全文
32 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zbde00 2002-04-02
  • 打赏
  • 举报
回复

另外在WM_DESTROY消息中,应该加入下面这一句DeleteCriticalSection(&g_cs);

强烈建议在WM_CREATE中初始化线程
footway 2002-03-20
  • 打赏
  • 举报
回复
g_cs被初始化两次。
TangDL 2002-03-20
  • 打赏
  • 举报
回复
我没看你的源程序,理论上临界区一般在主调线程里初始化,在被调线程里调用。不要在被调线程里初始化,不同的线程有各自的堆栈,会发生访问错误的。当然也不需要在每个线程里初始化了(只要Enter和Leave就行了)
delphihero 2002-03-20
  • 打赏
  • 举报
回复
也不要在线程里初始化,谁知道会出现多少次
neccui 2002-03-19
  • 打赏
  • 举报
回复
InitializeCriticalSection只需要调用一次就够了,你调用了两次。

1,660

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 非技术类
社区管理员
  • 非技术类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧