windows_34_Thread_Base 线程基础
// windows_34_Thread_Base.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <conio.h>
//1、定义线程处理函数
//ThreadProc
DWORD WINAPI ThreadProc1( LPVOID pParam)
{
DWORD nValue = (DWORD)pParam;
int i = 0;
while (i<10)
{
i++;
printf( "Thread Proc1---------%d\n", nValue );
Sleep( 1000 );
}
return 0;
}
DWORD WINAPI ThreadProc2( LPVOID pParam )
{
DWORD nValue = (DWORD)pParam;
while (1)
{
printf( "%d---------Thread Proc2\n", nValue );
Sleep( 1000 );
}
}
void Create( )
{
DWORD nValue = 100;
//2、创建线程
//CreateThread
DWORD nThreadID = 0;
HANDLE hThread = CreateThread( NULL, 0, ThreadProc1, (LPVOID)nValue, CREATE_SUSPENDED, &nThreadID );
//3、使用线程
printf( "Thread 1 ID:%d\n", nThreadID );
printf( "Thread 1 Handle:%p\n", hThread );
//执行线程
ResumeThread( hThread );
//等候线程结束
WaitForSingleObject( hThread, INFINITE );
//4、结束线程
hThread = CreateThread( NULL, 0, ThreadProc2, (LPVOID)nValue, 0, &nThreadID );
printf( "Thread 2 ID:%d\n", nThreadID );
printf( "Thread 2 Handle:%p\n", hThread );
//挂起线程
SuspendThread( hThread );
ResumeThread( hThread );
CloseHandle( hThread );
}
int _tmain(int argc, _TCHAR* argv[])
{
Create( );
getchar( );
return 0;
}
时间: 2024-10-09 17:01:35