mfc2.afxbeginthread函式建立和初始化cwinthread物件,啟動並返回位址。
3.pthread = new cwinthread();
pthread->m_bautodelete = false;
pthread = afxbeginthread(startandclosethreadproc, null);
4.掛起執行緒
pthread->suspendthread();
重新啟動執行緒
pthread->resumethread();
5.執行緒的同步
11.事件物件
#include "stdafx.h"
#include #include #include #include "process.h"
dword winapi threadproc1(lpvoid lpparam);
dword winapi threadproc2(lpvoid lpparam);
int igolbalcount=0;
int imax = 12;
handle hevent;
int main(int argc, char* argv)
setevent(hevent);
hthread1=createthread(null,0,threadproc1,null,0,null);
hthread2=createthread(null,0,threadproc2,null,0,null);
sleep(40000);
closehandle(hevent); //關閉事件物件控制代碼
printf("主線程結束!\n");
system("pause");
return 0;
}dword winapi threadproc1(lpvoid lpparam)
else
sleep(10);
} return 0;
}dword winapi threadproc2(lpvoid lpparameter)
else
sleep(10);
} return 0;
}22.利用臨界區
dword winapi threadproc1(lpvoid lpparam);
dword winapi threadproc2(lpvoid lpparam);
int igolbalcount=0;
int imax = 12;
critical_section cs;
int main(int argc, char* argv)
dword winapi threadproc1(lpvoid lpparam)
else
sleep(10);
} return 0;
}33.利用訊號量
dword winapi threadproc1(lpvoid lpparam);
dword winapi threadproc2(lpvoid lpparam);
int igolbalcount=0;
int imax = 12;
int cmax = 1;
handle hsemaphore;
int main(int argc, char* argv)
hthread1=createthread(null,0,threadproc1,null,0,null);
hthread2=createthread(null,0,threadproc2,null,0,null);
sleep(5000);
printf("主線程結束!");
system("pause");
return 0;
}dword winapi threadproc1(lpvoid lpparam)
else
}sleep(10);
} return 0;
}44.利用互斥物件
dword winapi threadproc1(lpvoid lpparam);
dword winapi threadproc2(lpvoid lpparam);
int igolbalcount=0;
int imax = 12;
handle hmutex;
int main(int argc, char* argv)
waitforsingleobject(hmutex,infinite);
releasemutex(hmutex);
releasemutex(hmutex);
sleep(5000);
printf("主線程結束!\n");
return 0;
}dword winapi threadproc1(lpvoid lpparam)
return 0;
}
vc 多執行緒程式設計
vc 中的多執行緒程式設計 createamutexwithnoinitialowner.必須要建立乙個控制代碼 hmutex createmutex null,nosecurityattributes false,initiallynotowned mutextoprotectdatabase n...
VC 多執行緒程式設計入門示例
程式分析 建立乙個子執行緒,當主線程開始執行的時候,子執行緒同時開始執行,由於資源有限 如本程式中的螢幕列印 子執行緒和主線程執行速度不同,因此會隨機交替執行。如果希望子執行緒和主線程有序的交替執行的話,可以加乙個互斥鎖。threadtest.cpp 定義控制台應用程式的入口點。include st...
vc 多執行緒程式設計 SOCKET通訊
vc 多執行緒程式設計 socket通訊 include stdafx.h include 伺服器端程式 dword winapi answerthread lpvoid lparam int main sockaddr in service service.sin family af inet s...