#include <iostream>
#include <windows.h>
DWORD WINAPI thread_func(LPVOID pN)
{
for (int i = 0; i < *((int*)pN); ++i) {
std::cout << i+1 << "\t";
}
std::cout << std::endl;
throw "ok.";
std::cout << "thread_func() done." << std::endl;
return 0;
}
int main(int argc, char* argv[])
{
int n = 5;
try{
thread_func((LPVOID)&n);
Sleep(2000);
}
catch (const char* s) {
std::cerr << s << std::endl;
exit(1);
}
std::cout << "main() done." << std::endl;
return 0;
}
鍙互鐪嬪埌錛屽嚱鏁皌hread_func()鍙互姝g‘鐨勬姏鍑哄紓甯稿茍琚玬ain()鐨刢atch鎹曟崏銆備絾鏄紝濡傛灉鐢ㄤ竴涓柊綰跨▼鏉ヨ繍琛宼hread_func()浼?xì)鍑虹庮C粈涔堟儏鍐靛憿錛?br>
#include <iostream>
#include <windows.h>
DWORD WINAPI thread_func(LPVOID pN)
{
for (int i = 0; i < *((int*)pN); ++i) {
std::cout << i+1 << "\t";
}
std::cout << std::endl;
throw "ok.";
std::cout << "thread_func() done." << std::endl;
return 0;
}
int main(int argc, char* argv[])
{
HANDLE hThrd;
DWORD thrdId;
int n = 5;
try{
hThrd = CreateThread( NULL,
0,
thread_func,
(LPVOID)&n,
0,
&thrdId);
Sleep(2000);
}
catch (const char* s) {
std::cerr << s << std::endl;
exit(1);
}
std::cout << "main() done." << std::endl;
return 0;
}
寰堜笉騫革紝榪欎釜紼嬪簭緙栬瘧鐨勬椂鍊欐槸鍙互閫氳繃鐨勶紝浣嗘槸榪愯鏃跺嚭閿欙細(xì)
1 2 3 4 5
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
璇鋒寜浠繪剰閿戶緇? . .
鑰屼笖鍚屾椂浼?xì)鏈変竴涓繍琛屾椂閿欒鐨勬彁紺恒備簨瀹炰笂錛岃繖涓敊璇彁紺烘剰鍛崇潃紼嬪簭鍦ㄦ病鏈夊彂鐜皌ry{}鐨勬椂鍊欑湅鍒頒簡(jiǎn)throw銆?br>閫氳繃璇曢獙錛屾垜鍙戠幇緋葷粺錛堣繖閲屾槸win32錛変笉鑳藉皢CreateThread()鎵浜х敓鐨勭嚎紼嬪綊緇撳埌try{}涓傛洿鍔犱弗閲嶇殑鎯呭喌鏄紝鍗充嬌鐢ㄤ竴涓嚱鏁板泭鎷簡(jiǎn)鏁翠釜紼嬪簭錛岀劧鍚巘ry榪欎釜鍑芥暟錛屽叾浠栫嚎紼嬩緷鐒惰劚紱諱簡(jiǎn)榪欎釜try銆?br>鎵浠ワ紝涓涓В鍐蟲柟娉曟槸錛屽嚒鏄亣鍒版柊鐨勭嚎紼嬶紝蹇呴』鍦ㄦ柊綰跨▼涓噸鏂板啓寮傚父澶勭悊銆備笉鐒?dòng)灱尀濡俫oogle浠g爜鏍囧噯閲屾墍璇寸殑閭f牱錛屼笉浣跨敤C++鐨勫紓甯告満鍒躲傛瘯绔烠++娌℃湁瀹氫箟澶氱嚎紼嬬殑鏍囧噯錛屾墍浠ヤ篃灝辨棤浠庤璧峰綰跨▼涓紓甯稿鐞嗙殑鏍囧噯銆?br>鏈鍚庨檮涓婂湪鏂扮嚎紼嬪啓寮傚父澶勭悊鐨勫弬鑰冿細(xì)
#include <iostream>
#include <windows.h>
DWORD WINAPI thread_func(LPVOID pN)
{
try{
for (int i = 0; i < *((int*)pN); ++i) {
std::cout << i+1 << "\t";
}
std::cout << std::endl;
throw "ok.";
}
catch (const char* s) {
std::cerr << s << std::endl;
exit(1);
}
std::cout << "thread_func() done." << std::endl;
return 0;
}
int main(int argc, char* argv[])
{
HANDLE hThrd;
DWORD thrdId;
int n = 5;
hThrd = CreateThread( NULL,
0,
thread_func,
(LPVOID)&n,
0,
&thrdId);
Sleep(2000);
std::cout << "main() done." << std::endl;
return 0;
}

]]>