写个并行计算的双线程要用到哪些API 我要写个双线程并行计算的程序。最多要用到哪些API函数? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 CreateThread();WaintForSingleObject(); 最多就是你看完MSDN中关于线程和同步的所有API。不少于二十个。 #include <windows.h>#include <math.h>#include <process.h>#define REP 1000000#define STATUS_READY 0#define STATUS_WORKING 1#define STATUS_DONE 2#define WM_CALC_DONE (WM_USER + 0)#define WM_CALC_ABORTED (WM_USER + 1)typedef struct{ HWND hwnd ; BOOL bContinue ;}PARAMS, *PPARAMS ;LRESULT APIENTRY WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, szCmdLine, int iCmdShow){ static TCHAR szAppName[] = TEXT ("BigJob1") ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass)) { MessageBox ( NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow ( szAppName, TEXT ("Multithreading Demo"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ;}void Thread (PVOID pvoid){ double A = 1.0 ; INT i ; LONG lTime ; volatile PPARAMS pparams ; pparams = (PPARAMS) pvoid ; lTime = GetCurrentTime () ; for (i = 0 ; i < REP && pparams->bContinue ; i++) A = tan (atan (exp (log (sqrt (A * A))))) + 1.0 ; if (i == REP) { lTime = GetCurrentTime () - lTime ; SendMessage (pparams->hwnd, WM_CALC_DONE, 0, lTime) ; } else SendMessage (pparams->hwnd, WM_CALC_ABORTED, 0, 0) ; _endthread () ;}LRESULT CALLBACK WndProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ static INT iStatus ; static LONG lTime ; static PARAMS params ; static TCHAR * szMessage[] = { TEXT ("Ready (left mouse button begins)"), TEXT ("Working (right mouse button ends)"), TEXT ("%d repetitions in %ld msec") } ; HDC hdc ; PAINTSTRUCT ps ; RECT rect ; TCHAR szBuffer[64] ; switch (message) { case WM_LBUTTONDOWN: if (iStatus == STATUS_WORKING) { MessageBeep (0) ; return 0 ; } iStatus = STATUS_WORKING ; params.hwnd = hwnd ; params.bContinue = TRUE ; _beginthread (Thread, 0, ¶ms) ; InvalidateRect (hwnd, NULL, TRUE) ; return 0 ; case WM_RBUTTONDOWN: params.bContinue = FALSE ; return 0 ; case WM_CALC_DONE: lTime = lParam ; iStatus = STATUS_DONE ; InvalidateRect (hwnd, NULL, TRUE) ; return 0 ; case WM_CALC_ABORTED: iStatus = STATUS_READY ; InvalidateRect (hwnd, NULL, TRUE) ; return 0 ; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect) ; wsprintf (szBuffer, szMessage[iStatus], REP, lTime) ; DrawText (hdc, szBuffer, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ;} 如果仅仅实现多线程,CreateThread 函数就足够了,如果需要同步,则可以使用 InitializeCriticalSection、EnterCriticalSection、LeaveCriticalSection、DeleteCriticalSection 函数。 还有 createevent等创建信号量的还有用来共享数据例如创建共享内存和内存映射文件的等待还有 传送小量数据的消息WM_COPYDATA 两个线程放到两个CPU上执行:CWinThread * pThread1 = NULL;CWinThread * pThread2 = NULL;// AfxBeginThread的第二个和第三个参数仅作为参考pThread1 = AfxBeginThread(Thread1, NULL, THREAD_PRIORITY_TIME_CRITICAL, 0, CREATE_SUSPENDED, NULL);pThread1 = AfxBeginThread(Thread2, NULL, THREAD_PRIORITY_TIME_CRITICAL, 0, CREATE_SUSPENDED, NULL);if (pThread1 && pThread2){ SetThreadAffinityMask(pThread1, 1); // 第一个线程放在第一个CPU上执行 SetThreadAffinityMask(pThread2, 2); // 第二个线程放在第二个CPU上执行 pThread1->ResumeThread(); pThread2->ResumeThread();} 并行计算已经有一些现成的标准和程序库,例如MPI和OpenMP MFC日历控件的问题,急~~~~ MFC中在线程使用recvfrom问题(分结完了就剩 50) fat32 BPB结构中的算法问题 CSpinButtonCtrl如何实现增量为10? 资源文件的问题,请大位来看一看. CEdit 中怎么响应回车事件? 如何动态调整控件的大小? 如何实现在在对话框中点击一个按钮A,然后在父窗口上面显示A,并且窗口要是非模态的! 转让视频监控-人脸识别 高分救命:哪位大哥帮我写下线段与正方体的求交算法啊!! 急问dialog如何实现滚动 请问关于多国语言输入的问题
WaintForSingleObject();
不少于二十个。
#include <math.h>
#include <process.h>#define REP 1000000#define STATUS_READY 0
#define STATUS_WORKING 1
#define STATUS_DONE 2#define WM_CALC_DONE (WM_USER + 0)
#define WM_CALC_ABORTED (WM_USER + 1)typedef struct
{
HWND hwnd ;
BOOL bContinue ;
}
PARAMS, *PPARAMS ;
LRESULT APIENTRY WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("BigJob1") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow ( szAppName, TEXT ("Multithreading Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}void Thread (PVOID pvoid)
{
double A = 1.0 ;
INT i ;
LONG lTime ;
volatile PPARAMS pparams ;
pparams = (PPARAMS) pvoid ;
lTime = GetCurrentTime () ;
for (i = 0 ; i < REP && pparams->bContinue ; i++)
A = tan (atan (exp (log (sqrt (A * A))))) + 1.0 ;
if (i == REP)
{
lTime = GetCurrentTime () - lTime ;
SendMessage (pparams->hwnd, WM_CALC_DONE, 0, lTime) ;
}
else
SendMessage (pparams->hwnd, WM_CALC_ABORTED, 0, 0) ;
_endthread () ;
}LRESULT CALLBACK WndProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static INT iStatus ;
static LONG lTime ;
static PARAMS params ;
static TCHAR * szMessage[] = { TEXT ("Ready (left mouse button begins)"),
TEXT ("Working (right mouse button ends)"),
TEXT ("%d repetitions in %ld msec") } ;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
TCHAR szBuffer[64] ;
switch (message)
{
case WM_LBUTTONDOWN:
if (iStatus == STATUS_WORKING)
{
MessageBeep (0) ;
return 0 ;
}
iStatus = STATUS_WORKING ;
params.hwnd = hwnd ;
params.bContinue = TRUE ;
_beginthread (Thread, 0, ¶ms) ;
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case WM_RBUTTONDOWN:
params.bContinue = FALSE ;
return 0 ;
case WM_CALC_DONE:
lTime = lParam ;
iStatus = STATUS_DONE ;
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case WM_CALC_ABORTED:
iStatus = STATUS_READY ;
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
wsprintf (szBuffer, szMessage[iStatus], REP, lTime) ;
DrawText (hdc, szBuffer, -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
还有用来共享数据例如创建共享内存和内存映射文件的等待
还有 传送小量数据的消息WM_COPYDATA
CWinThread * pThread2 = NULL;// AfxBeginThread的第二个和第三个参数仅作为参考
pThread1 = AfxBeginThread(Thread1, NULL, THREAD_PRIORITY_TIME_CRITICAL, 0, CREATE_SUSPENDED, NULL);
pThread1 = AfxBeginThread(Thread2, NULL, THREAD_PRIORITY_TIME_CRITICAL, 0, CREATE_SUSPENDED, NULL);if (pThread1 && pThread2)
{
SetThreadAffinityMask(pThread1, 1); // 第一个线程放在第一个CPU上执行
SetThreadAffinityMask(pThread2, 2); // 第二个线程放在第二个CPU上执行
pThread1->ResumeThread();
pThread2->ResumeThread();
}