在YC++中编写多线程代码
在运行程序时,很多费时的操作或计算(如通过socket读网络文件, 解一个微分方程等等),都有可能使界面失去反应,这种现象叫做阻塞。如何解决阻塞问题呢?最好的办法就是使用多线程。
在YC++编程中,多线程程序是很容易编写的。编写多线程程序最重要的一条就是尽量使用局部变量,
而不要使用全局变量(只读的除外)。因为使用全局变量容易出错。
/*****************************************************************************************************************/
下面这个例子用Win32函数CreateThread创建线程, 其用法是:
将下列代码存入名字任取的文件, 如: th.cpp
在yc++中, 用
<文件 打开或创建cpp源程序>
调入th.cpp, 再用
<工具 执行>
运行th.cpp
或在dos中, 用 ycc th.cpp 生成 th.exe, 再运行th.exe
在vc++中, 用 cl th.cpp 生成 th.exe, 再运行th.exe
#ifndef YCC
#include
#include "include/ycapi.h"
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "yxbapi.lib")
#endif
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
/创建主窗口. 用YC++的API函数创建, 它不需要注册.
int WINAPI MainWndProc(HWND hwnd,UINT iMessage,UINT wParam,LONG lParam,void *pUserData);
HWND hwnd = YXB_Window(MainWndProc,NULL,0,
WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_SYSMENU,
"多线程",110,50,800,600,NULL,WT_WIN);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
int WINAPI MainWndProc(HWND hwnd,UINT iMessage,UINT wParam,LONG lParam,void *pUserData)
{
static HANDLE linep_thread_handle,rectp_thread_handle;
switch(iMessage)
{
case WM_DESTROY:
TerminateThread(linep_thread_handle,0);
CloseHandle(linep_thread_handle);
TerminateThread(rectp_thread_handle,0);
CloseHandle(rectp_thread_handle);
PostQuitMessage(0);
return FALSE;
case WM_CREATE:
DWORD linep_thread_ret, rectp_thread_ret;
void draw_linep(LPVOID *my_buf), draw_rectp(LPVOID *my_buf);
linep_thread_handle = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)draw_linep,hwnd,0,&linep_thread_ret);
rectp_thread_handle = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)draw_rectp,hwnd,0,&rectp_thread_ret);
return FALSE;
case WM_PAINT:
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
YIMG yimg;
memset(&yimg,0,sizeof YIMG);
yimg.hdc = ps.hdc; //通过这个设置, 可以将窗口客户区当作一幅图象来操作
RECT mRect;
GetClientRect(hwnd, &mRect);
YXB_ImageFill(&yimg, 0, 0, mRect.right, mRect.bottom, RGB(130,150,160));
EndPaint(hwnd, &ps);
return FALSE;
}
return DefWindowProc(hwnd,iMessage,wParam,lParam);
}
const int mycolor[]={0xff7766,0xcc88ff,0x6688aa,0xcc9977,0x88ff11,0x779911,0xdd88aa,0x55ccff};
void draw_linep(LPVOID *my_buf)
{
static int xx0=388,yy0=200;
HWND hwnd = (HWND)my_buf;
HDC hdc = GetDC(hwnd);
RECT mRect;
GetClientRect(hwnd, &mRect);
MoveToEx(hdc,0,0,NULL);
for(int ii=0; ; ii++)
{
HPEN hPen = CreatePen(PS_SOLID, 3, mycolor[ii%8]);
HPEN hpenSave = (HPEN)SelectObject (hdc, hPen);
LineTo(hdc,xx0,yy0);
xx0 = rand()*mRect.right/2/RAND_MAX;
yy0 = rand()*mRect.bottom/RAND_MAX;
SelectObject (hdc, hpenSave);
DeleteObject(hPen);
Sleep(10);
}
ReleaseDC(hwnd,hdc);
}
void draw_rectp(LPVOID *my_buf)
{
static int xx0=0,yy0=0;
HWND hwnd = (HWND)my_buf;
HDC hdc = GetDC(hwnd);
RECT mRect;
GetClientRect(hwnd, &mRect);
for(int ii=0; ; ii++)
{
HBRUSH hbr = (HBRUSH)SelectObject(hdc,CreateSolidBrush(mycolor[ii%8]));
PatBlt(hdc,xx0,yy0,30,30,PATCOPY);
DeleteObject(SelectObject(hdc,hbr));
xx0 = rand()*mRect.right/2/RAND_MAX + mRect.right/2;
yy0 = rand()*mRect.bottom/RAND_MAX;
Sleep(10);
}
ReleaseDC(hwnd,hdc);
}