__out_opt 表示是輸出
HANDLE
WINAPI
CreateThread(
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, 安全屬性,一般爲NULL表示默認
__in SIZE_T dwStackSize, 線程棧的空間,一般使用0來表示默認值
__in LPTHREAD_START_ROUTINE lpStartAddress, 線程函數地址
__in_opt LPVOID lpParameter, 線程函數參數
__in DWORD dwCreationFlags, 創建線程時,指定啥標誌 0 表示立即開始執行,CREATE_SUSPENDED 表示暫停
__out_opt LPDWORD lpThreadId 返回線程ID,不需要時,可以傳遞NULL,表示不關心
);
#include <iostream>
#include <vector>
#include <map>
#include <Windows.h>
#include <tchar.h>
#include <process.h>
#include <Strsafe.h>
HANDLE ghthreadCurrent = NULL;
unsigned int WINAPI ThreadFunc2(LPVOID lp);
unsigned int WINAPI ThreadFunc1(LPVOID lp)
{
HANDLE hThread = GetCurrentThread();
ghthreadCurrent =(HANDLE) _beginthreadex(NULL, 0, ThreadFunc2, hThread, 0, NULL);
while(1)
{
printf("111\n");
Sleep(1000);
}
return 0;
}
unsigned int WINAPI ThreadFunc2(LPVOID lp)
{
HANDLE hThread = (HANDLE)lp;
TerminateThread(hThread,0);
return 0;
}
int main()
{
HANDLE hThread1 =(HANDLE) _beginthreadex(NULL, 0, ThreadFunc1, NULL, 0, NULL );
WaitForSingleObject(hThread1,INFINITE);
return 0;
}
#include <iostream>
#include <vector>
#include <map>
#include <Windows.h>
#include <tchar.h>
#include <process.h>
#include <Strsafe.h>
unsigned int WINAPI ThreadFunc2(LPVOID lp);
unsigned int WINAPI ThreadFunc1(LPVOID lp)
{
HANDLE hThread = GetCurrentThread();
HANDLE hTagrealThread = NULL;
BOOL bOk = DuplicateHandle(GetCurrentProcess(),hThread,GetCurrentProcess(),
&hTagrealThread,0,FALSE,DUPLICATE_SAME_ACCESS);
(HANDLE) _beginthreadex(NULL, 0, ThreadFunc2, hTagrealThread, 0, NULL);
while(1)
{
printf("111\n");
Sleep(1000);
}
return 0;
}
unsigned int WINAPI ThreadFunc2(LPVOID lp)
{
HANDLE hThread = (HANDLE)lp;
Sleep(5000);
TerminateThread(hThread,0);
//必須要關閉句柄
CloseHandle(hThread);
return 0;
}
int main()
{
HANDLE hThread1 =(HANDLE) _beginthreadex(NULL, 0, ThreadFunc1, NULL, 0, NULL );
WaitForSingleObject(hThread1,INFINITE);
CloseHandle(hThread1);
printf("main exit\n");
system("pause");
return 0;
}
線程2函數,殺死的是線程1