多核程序设计样例.docx
《多核程序设计样例.docx》由会员分享,可在线阅读,更多相关《多核程序设计样例.docx(68页珍藏版)》请在冰豆网上搜索。
多核程序设计样例
1线程创建实例1
#include"stdafx.h"
#include
#include
usingnamespacestd;
DWORDWINAPIFunOne(LPVOIDparam){
while
(1){
Sleep(1000);
cout<<"ThisisFunOne"<}
return1;
}
DWORDWINAPIFunTwo(LPVOIDparam){
while
(1){
Sleep(1000);
cout<<"ThisisFunTwo"<}
return1;
}
intmain(intargc,char*argv[])
{
DWORDlp1=0,lp2=0;
HANDLEhand1=CreateThread(NULL,0,FunOne,NULL,CREATE_SUSPENDED,&lp1);
HANDLEhand2=CreateThread(NULL,0,FunTwo,NULL,CREATE_SUSPENDED,&lp2);
system("pause");
ResumeThread(hand1);
ResumeThread(hand2);
system("pause");
return0;
}
2线程创建实例2
#include"stdafx.h"
#include
#include
#include
#include
usingnamespacestd;
voidThreadFunc1(PVOIDparam)
{
Sleep(10000);
cout<<"ThisisThreadFunc1"<}
voidThreadFunc2(PVOIDparam)
{
Sleep(10000);
cout<<"ThisisThreadFunc2"<}
voidThreadFunc3(PVOIDparam)
{
Sleep(10000);
cout<<"ThisisThreadFunc2"<}
intmain()
{
inti=0;
_beginthread(ThreadFunc1,0,NULL);
_beginthread(ThreadFunc2,0,NULL);
Sleep(3000);
cout<<"end"<return0;
}
3线程管理实例
#include"stdafx.h"
#include
#include
usingnamespacestd;
DWORDWINAPIFunOne(LPVOIDparam){
while(true)
{
Sleep(1000);
cout<<"hello!
";
}
return0;
}
DWORDWINAPIFunTwo(LPVOIDparam){
while(true)
{
Sleep(1000);
cout<<"world!
";
}
return0;
}
intmain(intargc,char*argv[])
{
intinput=0;
DWORDlp1=0,lp2=0;
HANDLEhand1=CreateThread(NULL,0,FunOne,(void*)&input,CREATE_SUSPENDED,&lp1);
HANDLEhand2=CreateThread(NULL,0,FunTwo,(void*)&input,CREATE_SUSPENDED,&lp2);
while(true){
cin>>input;
if(input==1)
{
ResumeThread(hand1);
ResumeThread(hand2);
}
if(input==2)
{
SuspendThread(hand1);
SuspendThread(hand2);
}
if(input==0)
{
TerminateThread(hand1,1);
TerminateThread(hand2,1);
}
if(input==9)
return0;
};
return0;
}
4同步——全局变量
#include"stdafx.h"
#include
#include
usingnamespacestd;
intglobalvar=false;
DWORDWINAPIThreadFunc(LPVOIDpParam)
{
cout<<"ThreadFunc"<Sleep(200);
globalvar=true;
return0;
}
intmain()
{
HANDLEhthread=CreateThread(NULL,0,ThreadFunc,NULL,0,NULL);
if(!
hthread)
{
cout<<"ThreadCreateError!
"<CloseHandle(hthread);
}
while(!
globalvar)
cout<<"Threadwhile"<cout<<"Threadexit"<return0;
}
5事件机制应用实例
#include"stdafx.h"
#include
#include
#include
#include
usingnamespacestd;
HANDLEevRead,evFinish;
voidReadThread(LPVOIDparam)
{
WaitForSingleObject(evRead,INFINITE);
cout<<"Reading"<SetEvent(evFinish);
}
voidWriteThread(LPVOIDparam)
{
cout<<"Writing"<SetEvent(evRead);
}
intmain(intargc,char*argv[])
{
evRead=CreateEvent(NULL,FALSE,FALSE,NULL);
evFinish=CreateEvent(NULL,FALSE,FALSE,NULL);
_beginthread(ReadThread,0,NULL);
_beginthread(WriteThread,0,NULL);
WaitForSingleObject(evFinish,INFINITE);
cout<<"TheProgramisEnd"<return0;
}
6临界区同步机制实例
#include"stdafx.h"
#include
#include
#include
#include
usingnamespacestd;
inttotal=100;
HANDLEevFin[2];
CRITICAL_SECTIONcs;
voidWithdrawThread1(LPVOIDparam)
{
EnterCriticalSection(&cs);
if(total-90>=0)
{
total-=90;
cout<<"Youwithdraw90"<}
else
cout<<"Youdonothavethatmuchmoney"<LeaveCriticalSection(&cs);
SetEvent(evFin[0]);
}
voidWithdrawThread2(LPVOIDparam)
{
EnterCriticalSection(&cs);
if(total-20>=0)
{
total-=20;
cout<<"Youwithdraw20"<}
else
cout<<"Youdonothavethatmuchmoney"<LeaveCriticalSection(&cs);
SetEvent(evFin[1]);
}
intmain(intargc,char*argv[])
{
evFin[0]=CreateEvent(NULL,FALSE,FALSE,NULL);
evFin[1]=CreateEvent(NULL,FALSE,FALSE,NULL);
InitializeCriticalSection(&cs);
_beginthread(WithdrawThread1,0,NULL);
_beginthread(WithdrawThread2,0,NULL);
WaitForMultipleObjects(2,evFin,TRUE,INFINITE);
DeleteCriticalSection(&cs);
cout<return0;
}
7互斥量同步机制实例
#include"stdafx.h"
#include
#include
#defineTHREAD_INSTANCE_NUMBER3
usingnamespacestd;
LONGg_fResourceInUse=FALSE;
LONGg_lCounter=0;
DWORDThreadProc(void*pData){
intThreadNumberTemp=(*(int*)pData);
HANDLEhMutex;
if((hMutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"Mutex.Test"))==NULL){
cout<<"OpenMutexerror!
"<}
WaitForSingleObject(hMutex,INFINITE);
cout<<"ThreadProc:
"<"<cout<<"ThreadProc"<ReleaseMutex(hMutex);
CloseHandle(hMutex);
return0;
}
intmain(intargc,char*argv[])
{
inti;
DWORDID[THREAD_INSTANCE_NUMBER];
HANDLEh[THREAD_INSTANCE_NUMBER];
HANDLEhMutex;
if((hMutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"Mutex.Test"))==NULL){
if((hMutex=CreateMutex(NULL,FALSE,"Mutex.Test"))==NULL){
cout<<"CreateMutexerror!
"<return0;
}
}
for(i=0;i{
WaitForSingleObject(hMutex,INFINITE);
h[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,(void*)&ID[i],0,&(ID[i]));
if(h[i]==NULL)
cout<<"CreateThreaderror"<else
cout<<"CreateThread:
"<ReleaseMutex(hMutex);
}
WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,h,TRUE,INFINITE);
cout<<"ClosetheMutexHandle!
"<CloseHandle(hMutex);
system("pause");
return0;
}
8信号量同步机制实例1
#include"stdafx.h"
#include
#include
usingnamespacestd;
#defineTHREAD_INSTANCE_NUMBER3
DWORDfoo(void*pData){
intThreadNumberTemp=(*(int*)pData);
HANDLEhSemaphore;
if((hSemaphore=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"Semaphore.Test"))==NULL){
cout<<"OpenSemaphoreerror!
"<}
WaitForSingleObject(
hSemaphore,//handletosemaphore
INFINITE);//zero-secondtime-outinterval
cout<<"foo:
"<"<cout<<"foo"<ReleaseSemaphore(hSemaphore,1,NULL);
CloseHandle(hSemaphore);
return0;
}
intmain(intargc,char*argv[])
{
inti;
DWORDThreadID[THREAD_INSTANCE_NUMBER];
HANDLEhThread[THREAD_INSTANCE_NUMBER];
HANDLEhSemaphore;
if((hSemaphore=CreateSemaphore(NULL,1,1,"Semaphore.Test"))==NULL){
cout<<"CreateSemaphoreerror!
"<return0;
}
for(i=0;i{
WaitForSingleObject(
hSemaphore,//handletosemaphore
INFINITE);//zero-secondtime-outinterval
hThread[i]=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)foo,
(void*)&ThreadID[i],
0,
&(ThreadID[i]));
if(hThread[i]==NULL)
cout<<"CreateThreaderror"<else
cout<<"CreateThread:
"<ReleaseSemaphore(hSemaphore,1,NULL);
}
WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,hThread,TRUE,INFINITE);
cout<<"ClosetheSemaphoreHandle!
"<CloseHandle(hSemaphore);
system("pause");
return0;
}
信号量同步机制实例2
#include"stdafx.h"
#include
#include
usingnamespacestd;
intseats=9;
CRITICAL_SECTIONcs;
#defineTHREAD_INSTANCE_NUMBER50
DWORDfoo(void*pData){
intThreadNumberTemp=(*(int*)pData);
HANDLEhSemaphore;
if((hSemaphore=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"Semaphore.Test"))==NULL){
cout<<"OpenSemaphoreerror!
"<}
WaitForSingleObject(
hSemaphore,//handletosemaphore
INFINITE);//zero-secondtime-outinterval
EnterCriticalSection(&cs);
seats--;
cout<LeaveCriticalSection(&cs);
Sleep(2060);
EnterCriticalSection(&cs);
seats++;
cout<LeaveCriticalSection(&cs);
ReleaseSemaphore(hSemaphore,1,NULL);
CloseHandle(hSemaphore);
return0;
}
intmain(intargc,char*argv[])
{
inti;
DWORDThreadID[THREAD_INSTANCE_NUMBER];
HANDLEhThread[THREAD_INSTANCE_NUMBER];
HANDLEhSemaphore;
if((hSemaphore=CreateSemaphore(NULL,9,9,"Semaphore.Test"))==NULL){
cout<<"CreateSemaphoreerror!
"<return0;
}
InitializeCriticalSection(&cs);
for(i=0;i{
//WaitForSingleObject(
//hSemaphore,//handletosemaphore
//INFINITE);//zero-secondtime-outinterval
hThread[i]=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)foo,
(void*)&ThreadID[i],
0,
&(ThreadID[i]));
//if(hThread[i]==NULL)
//cout<<"CreateThreaderror"<//else
//cout<<"CreateThread:
"<//ReleaseSemaphore(hSemaphore,1,NULL);
}
WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,hThread,TRUE,INFINITE);
//cout<<"ClosetheSemaphoreHandle!
"<CloseHandle(hSemaphore);
DeleteCriticalSection(&cs);
system("pause");
return0;
}
MFC多线程程序设计(主要代码)
#include"stdafx.h"
#include"exa8.h"
#include"MainFrm.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
HANDLEg_hBusy;
HWNDhwnd;
intk=0;
longdata1[]={12,32,47,763,75,43,86,42};
longdata2[]={432,68,36,84,47,73,732,46};
longdata3[]={435,754,37,765,48,785,326,78};
longdata4[]={54,76,93,457,456,34,94,50};
/////////////////////////////////////////////////////////////////////////////
//CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame,CFrameW