”多线程多任务“是程序开发者和用户都需要的一个重要资产。从WindowsNT开始,完全支持32位程序的抢占式多任务。带领我们进入了”多线程多任务“时代。
基本概念
进程(processes)
从win32角度来看,进程含有内存和资源,被进程拥有的内存理论上可达2GB,资源包括内核对象(handle,mutex…),user资源(对话框,字符串…),GDI资源(cdc和brush…)。
内存(memory)
每个进程存储数据的地方,分为:
线程(thread)
执行任务的载体
为什么程序不用多进程?
多线程:
多进程:
Win32线程创建
不使用C Run-time Library的多线程示例
#include <windows.h>
#include <tchar.h>
#include <Strsafe.h>
#define MAX_THREADS 5//线程数
#define BUF_SIZE 1024
DWORD WINAPI ThreadFun(LPVOID lpParam)
{
TCHAR msgBuf[BUF_SIZE];
size_t msgLen;
DWORD dwChars;
//线程参数取得
int n = *(int*)lpParam;
//输出到stdout
HANDLE hStdOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
::StringCchPrintf(msgBuf, BUF_SIZE, TEXT("%d%d%d%d%d%d%d%d%d%d\n"),
n, n, n, n, n, n, n, n, n, n);
::StringCchLength(msgBuf, BUF_SIZE, &msgLen);
for (int i = 0; i < 5; i++)
{
::WriteConsole(hStdOut, msgBuf, msgLen, &dwChars, NULL);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThread[MAX_THREADS];
DWORD dwThreadId;
//线程创建
for (int i = 0; i < MAX_THREADS; i++)
{
hThread[i] = (HANDLE)::CreateThread(
NULL,
0,
ThreadFun,
&i,
0,
&dwThreadId);
if (hThread[i] != NULL)
{
printf("Thread launched %d\n", i);
}
}
//等待所有线程结束
::WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE);
//关闭结束线程句柄
for (int i = 0; i < MAX_THREADS; i++)
{
::CloseHandle(hThread[i]);
}
return 0;
}
使用C Run-time Library的多线程示例
#include <stdio.h>
#include <Windows.h>
#include <process.h>
//线程数
#define MAX_THREADS 5
unsigned int WINAPI ThreadFun(LPVOID lpParam)
{
int n = *(int*)lpParam;
for (int i = 0; i < 5; i++)
{
printf("%d%d%d%d%d%d%d%d%d%d\n", n, n, n, n, n, n, n, n, n, n);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThread[MAX_THREADS];
unsigned int dwThreadId;
//线程创建
for (int i = 0; i < MAX_THREADS; i++)
{
hThread[i] = (HANDLE)::_beginthreadex(
NULL,
0,
ThreadFun,
&i,
0,
&dwThreadId);
if (hThread[i] != NULL)
{
printf("Thread launched %d\n", i);
}
}
//等待所有线程结束
::WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE);
//关闭结束线程句柄
for (int i = 0; i < MAX_THREADS; i++)
{
::CloseHandle(hThread[i]);
}
return 0;
}
区别使用C Run-time Library:
以上使用C Run-time Library和未使用C Run-time Library并非很严格,因为程序的startup就是从C Run-time Library开始执行,所以严格不使用C Run-time Library是不存在。那么只要满足下面情况,我们就认为是使用了C Run-time Library
如何正确函数创建Win32线程?
未使用C Run-time Library,利用CreateThread()函数创建线程,使用了C Run-time Library,利用_beginthreadex()函数创建线程,MFC程序中使用CWinThread创建线程。
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/chen_jint/article/details/47171595