跪求多线程简单c程序

hechuang1111 2011-08-13 09:46:20
用4个线程,其中2个生成100个随机数,另2个求这些随机数的和。
...全文
133 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackyjkchen 2011-08-28
  • 打赏
  • 举报
回复
吐槽一下楼主,你的这种简单需求用多线程反而会更慢……一个循环来做,用CryptoAPI产生随机数,恐怕根本快到测不出时间……
Tsy040501 2011-08-28
  • 打赏
  • 举报
回复

#include <windows.h>
#include <process.h>
#include <math.h>

#define WM_CLIENT_COUNT (WM_USER + 1)
#define WM_CLIENT_PLAINT (WM_USER + 2)
#define NUM 100

typedef struct
{
HWND hwnd ;
//HANDLE hEvent ;
int inum[NUM] ;
}
PARAMS, *PPARAMS ;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ChildProc1(HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ChildProc2(HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR
lpCmdLine,int nShowCmd)
{
WNDCLASS wndclass ;
HWND hwnd ;
MSG msg ;
TCHAR szAppName[] = TEXT("FThread") ;

wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH) ;
wndclass.hCursor = LoadCursor(NULL, IDI_APPLICATION) ;
wndclass.hIcon = LoadIcon(NULL, IDC_ARROW) ;
wndclass.hInstance = hInstance ;
wndclass.lpfnWndProc = WndProc ;
wndclass.lpszClassName = szAppName ;
wndclass.lpszMenuName = NULL ;
wndclass.style = CS_VREDRAW | CS_HREDRAW ;

RegisterClass(&wndclass) ;

hwnd = CreateWindow(szAppName, TEXT("FourThred-Result"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;

ShowWindow(hwnd, nShowCmd) ;
UpdateWindow(hwnd) ;

while(GetMessage(&msg, NULL, NULL, NULL))
{
TranslateMessage(&msg) ;
DispatchMessage(&msg) ;
}

return msg.wParam ;
}

//Sum
VOID Thread3(PVOID pvoid)
{
int i, sum ;
volatile PPARAMS pparams ;

pparams = (PPARAMS) pvoid ;

sum = pparams->inum[0] ;
for(i = 1;i < NUM; i ++)
{ sum += pparams->inum[i] ; }
SendMessage(pparams->hwnd, WM_CLIENT_PLAINT, (WPARAM) sum, 0) ;

_endthread() ;
}

VOID Thread4(PVOID pvoid)
{
int i, sum ;
volatile PPARAMS pparams ;

pparams = (PPARAMS) pvoid ;

sum = pparams->inum[0] ;
for(i = 1;i < NUM; i ++)
{ sum += pparams->inum[i] ; }
SendMessage(pparams->hwnd, WM_CLIENT_PLAINT, (WPARAM) sum, 0) ;

_endthread() ;
}

//Fist ChildProc
VOID Thread1(PVOID pvoid)
{
int i ;
volatile PPARAMS pparams ;

pparams = (PPARAMS) pvoid ;

for(i = 0 ;i < NUM; i++)
{ pparams->inum[i] = rand() % 1000 ; }
SendMessage(pparams->hwnd, WM_CLIENT_COUNT, 0, 0) ;

_endthread() ;
}
LRESULT CALLBACK ChildProc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static PARAMS params ;
static int cxClient, cyClient ;
TCHAR szBuffer[10] ;
HDC hdc ;

switch(message)
{
case WM_CREATE :
params.hwnd = hwnd ;

_beginthread(Thread1, 0, ¶ms) ;
return 0 ;

case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;

case WM_CLIENT_COUNT :
_beginthread(Thread3, 0, ¶ms) ;
return 0 ;

case WM_CLIENT_PLAINT :
hdc = GetDC(hwnd) ;
TextOut(hdc, cxClient / 2, cyClient / 2, szBuffer,
wsprintf(szBuffer, TEXT("%d"), (int) wParam)) ;
ReleaseDC(hwnd, hdc) ;
return 0 ;
}
return DefWindowProc(hwnd, message, wParam, lParam) ;
}

//Second Thread
/**/VOID Thread2(PVOID pvoid)
{
int i ;
volatile PPARAMS pparams ;

pparams = (PPARAMS) pvoid ;

for(i = 0 ;i < NUM; i++)
{ pparams->inum[i] = rand() % 1051 ; }
SendMessage(pparams->hwnd, WM_CLIENT_COUNT, 0, 0) ;

_endthread() ;
}
LRESULT CALLBACK ChildProc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static PARAMS params ;
static int cxClient, cyClient ;
HDC hdc ;
TCHAR szBuffer[10] ;

switch(message)
{
case WM_CREATE :
params.hwnd = hwnd ;

_beginthread(Thread2, 0, ¶ms) ;
return 0 ;
case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;

case WM_CLIENT_COUNT :
_beginthread(Thread4, 0, ¶ms) ;
return 0 ;

case WM_CLIENT_PLAINT :
hdc = GetDC(hwnd) ;
TextOut(hdc, cxClient / 2, cyClient / 2, szBuffer,
wsprintf(szBuffer, TEXT("%d"), (int) wParam)) ;
ReleaseDC(hwnd, hdc) ;
return 0 ;
}
return DefWindowProc(hwnd, message, wParam, lParam) ;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndChild[2] ;
static TCHAR * szChildName[] = {TEXT("Child1"),
TEXT("Child2")} ;
static WNDPROC ChildProc[] = {ChildProc1, ChildProc2} ;
WNDCLASS wndclass ;
HINSTANCE hInstance ;
int i, cxClient, cyClient ;

switch(message)
{
case WM_CREATE :
hInstance = (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE) ;

wndclass.cbClsExtra = NULL ;
wndclass.cbWndExtra = NULL ;
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH) ;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW) ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = NULL ;
wndclass.lpszMenuName = NULL ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;

for(i = 0 ;i < 2 ;i ++)
{
wndclass.lpfnWndProc = ChildProc[i] ;
wndclass.lpszClassName = szChildName[i] ;

RegisterClass(&wndclass) ;

hwndChild[i] = CreateWindow(szChildName[i], NULL,
WS_CHILDWINDOW | WS_BORDER | WS_VISIBLE,
0, 0, 0, 0,
hwnd, (HMENU) i, hInstance, NULL) ;
}
return 0 ;

case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;

MoveWindow(hwndChild[0], 0, 0, cxClient / 2, cyClient, FALSE) ;
MoveWindow(hwndChild[1], cxClient / 2, 0, cxClient / 2, cyClient, FALSE) ;
return 0 ;

case WM_DESTROY :
PostQuitMessage(0) ;
return 0 ;
}
return DefWindowProc(hwnd, message, wParam, lParam) ;
}
Tsy040501 2011-08-28
  • 打赏
  • 举报
回复

#include <windows.h>
#include <process.h>
#include <math.h>

#define WM_CLIENT_COUNT (WM_USER + 1)
#define WM_CLIENT_PLAINT (WM_USER + 2)
#define NUM 100

typedef struct
{
HWND hwnd ;
//HANDLE hEvent ;
int inum[NUM] ;
}
PARAMS, *PPARAMS ;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ChildProc1(HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ChildProc2(HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR
lpCmdLine,int nShowCmd)
{
WNDCLASS wndclass ;
HWND hwnd ;
MSG msg ;
TCHAR szAppName[] = TEXT("FThread") ;

wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH) ;
wndclass.hCursor = LoadCursor(NULL, IDI_APPLICATION) ;
wndclass.hIcon = LoadIcon(NULL, IDC_ARROW) ;
wndclass.hInstance = hInstance ;
wndclass.lpfnWndProc = WndProc ;
wndclass.lpszClassName = szAppName ;
wndclass.lpszMenuName = NULL ;
wndclass.style = CS_VREDRAW | CS_HREDRAW ;

RegisterClass(&wndclass) ;

hwnd = CreateWindow(szAppName, TEXT("FourThred-Result"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;

ShowWindow(hwnd, nShowCmd) ;
UpdateWindow(hwnd) ;

while(GetMessage(&msg, NULL, NULL, NULL))
{
TranslateMessage(&msg) ;
DispatchMessage(&msg) ;
}

return msg.wParam ;
}

//Sum
VOID Thread3(PVOID pvoid)
{
int i, sum ;
volatile PPARAMS pparams ;

pparams = (PPARAMS) pvoid ;

sum = pparams->inum[0] ;
for(i = 1;i < NUM; i ++)
{ sum += pparams->inum[i] ; }
SendMessage(pparams->hwnd, WM_CLIENT_PLAINT, (WPARAM) sum, 0) ;

_endthread() ;
}

VOID Thread4(PVOID pvoid)
{
int i, sum ;
volatile PPARAMS pparams ;

pparams = (PPARAMS) pvoid ;

sum = pparams->inum[0] ;
for(i = 1;i < NUM; i ++)
{ sum += pparams->inum[i] ; }
SendMessage(pparams->hwnd, WM_CLIENT_PLAINT, (WPARAM) sum, 0) ;

_endthread() ;
}

//Fist ChildProc
VOID Thread1(PVOID pvoid)
{
int i ;
volatile PPARAMS pparams ;

pparams = (PPARAMS) pvoid ;

for(i = 0 ;i < NUM; i++)
{ pparams->inum[i] = rand() % 1000 ; }
SendMessage(pparams->hwnd, WM_CLIENT_COUNT, 0, 0) ;

_endthread() ;
}
LRESULT CALLBACK ChildProc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static PARAMS params ;
static int cxClient, cyClient ;
TCHAR szBuffer[10] ;
HDC hdc ;

switch(message)
{
case WM_CREATE :
params.hwnd = hwnd ;

_beginthread(Thread1, 0, ¶ms) ;
return 0 ;

case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;

case WM_CLIENT_COUNT :
_beginthread(Thread3, 0, ¶ms) ;
return 0 ;

case WM_CLIENT_PLAINT :
hdc = GetDC(hwnd) ;
TextOut(hdc, cxClient / 2, cyClient / 2, szBuffer,
wsprintf(szBuffer, TEXT("%d"), (int) wParam)) ;
ReleaseDC(hwnd, hdc) ;
return 0 ;
}
return DefWindowProc(hwnd, message, wParam, lParam) ;
}

//Second Thread
/**/VOID Thread2(PVOID pvoid)
{
int i ;
volatile PPARAMS pparams ;

pparams = (PPARAMS) pvoid ;

for(i = 0 ;i < NUM; i++)
{ pparams->inum[i] = rand() % 1051 ; }
SendMessage(pparams->hwnd, WM_CLIENT_COUNT, 0, 0) ;

_endthread() ;
}
LRESULT CALLBACK ChildProc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static PARAMS params ;
static int cxClient, cyClient ;
HDC hdc ;
TCHAR szBuffer[10] ;

switch(message)
{
case WM_CREATE :
params.hwnd = hwnd ;

_beginthread(Thread2, 0, ¶ms) ;
return 0 ;
case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;

case WM_CLIENT_COUNT :
_beginthread(Thread4, 0, ¶ms) ;
return 0 ;

case WM_CLIENT_PLAINT :
hdc = GetDC(hwnd) ;
TextOut(hdc, cxClient / 2, cyClient / 2, szBuffer,
wsprintf(szBuffer, TEXT("%d"), (int) wParam)) ;
ReleaseDC(hwnd, hdc) ;
return 0 ;
}
return DefWindowProc(hwnd, message, wParam, lParam) ;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndChild[2] ;
static TCHAR * szChildName[] = {TEXT("Child1"),
TEXT("Child2")} ;
static WNDPROC ChildProc[] = {ChildProc1, ChildProc2} ;
WNDCLASS wndclass ;
HINSTANCE hInstance ;
int i, cxClient, cyClient ;

switch(message)
{
case WM_CREATE :
hInstance = (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE) ;

wndclass.cbClsExtra = NULL ;
wndclass.cbWndExtra = NULL ;
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH) ;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW) ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = NULL ;
wndclass.lpszMenuName = NULL ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;

for(i = 0 ;i < 2 ;i ++)
{
wndclass.lpfnWndProc = ChildProc[i] ;
wndclass.lpszClassName = szChildName[i] ;

RegisterClass(&wndclass) ;

hwndChild[i] = CreateWindow(szChildName[i], NULL,
WS_CHILDWINDOW | WS_BORDER | WS_VISIBLE,
0, 0, 0, 0,
hwnd, (HMENU) i, hInstance, NULL) ;
}
return 0 ;

case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;

MoveWindow(hwndChild[0], 0, 0, cxClient / 2, cyClient, FALSE) ;
MoveWindow(hwndChild[1], cxClient / 2, 0, cxClient / 2, cyClient, FALSE) ;
return 0 ;

case WM_DESTROY :
PostQuitMessage(0) ;
return 0 ;
}
return DefWindowProc(hwnd, message, wParam, lParam) ;
}
不知道这样会不会好看一点!各位大牛,多多指教~
mql0127 2011-08-23
  • 打赏
  • 举报
回复
拿分的
hechuang1111 2011-08-13
  • 打赏
  • 举报
回复
是用vc6.0写
帅得不敢出门 2011-08-13
  • 打赏
  • 举报
回复
你的啥系统, 不同系统线程 api是不一样的.
linux
pthread_create

随机数用srand rand
jy03187487 2011-08-13
  • 打赏
  • 举报
回复
.............
braveyly 2011-08-13
  • 打赏
  • 举报
回复
还有很重要点:生成了随机数后,要通知另外个线程进行计算。

windows下推荐同步方式为:event + waitforsingleobject
linux下推荐:condition variable 和 mutex
5t4rk 2011-08-13
  • 打赏
  • 举报
回复
不会 表示关注 来学习

jernymy 2011-08-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zmlovelx 的回复:]
你的啥系统, 不同系统线程 api是不一样的.
linux
pthread_create

随机数用srand rand
[/Quote]
windows用CreateThread

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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