多线程创建小例

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

学习windows下多线程的创建
使用WINAPI的CreateThread
和C/C++的_beginthread/_beginthreadex
和pthread的pthread_create
win7
mingw
/*
purpose:
	练习windowAPI的线程函数CreateThread,
	和标准C库的线程函数_beginthread/_beginthreadex, 
	和mingw的线程函数pthread_create
author:looyer@sina.com
date:2016/4/20
*/

#include <windows.h>
#include <process.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <pthread.h> //需要-lpthread

using namespace std;

//使用_beginthread创建的线程函数,对线程控制较弱,使用_endthread(void)结束线程
void __cdecl threadA(void *p)
{
	for(int i = 0; i < 5; i++) {
		cout << "thread_A\n";
		Sleep(500);
	}
}

//使用_beginthreadex创建的线程函数
unsigned int __stdcall threadB(void *p)
{
	for(int i = 0; i < 5; i++) {
		cout << "thread B\n";
		Sleep(500);
	}
}

//使用WinAPI的CreateThread创建的线程函数
DWORD WINAPI ThreadC(LPVOID param)
{
	for(int i = 0; i < 5; i++) {
		cout << "thread C\n";
		Sleep(300);
	}
	return 0;
}

//使用pthread_create创建的线程函数
void* thread_D(void *param)
{
	for(int i = 0; i < 5; i++) {
		cout << "thread D\n";
		Sleep(400);
	}
	return 0;
}

int main(int argc, char *argv[])
{
	_beginthread(&threadA, 0, 0);
	HANDLE hEx = (HANDLE)_beginthreadex(0, 0, &threadB, 0, 0, 0);

	HANDLE hThread = CreateThread(0, 0, ThreadC, 0, 0, 0);

	pthread_t pid;
	pthread_create(&pid, 0, thread_D, 0);

	WaitForSingleObject(hThread, INFINITE);
	CloseHandle(hThread);

	WaitForSingleObject(hEx, INFINITE);
	CloseHandle(hEx);

	
	pthread_join(pid, 0); //等待thread_D执行完成

	system("pause");
	return 0;
}
NAME = multi_thread_create

$(NAME): $(NAME).o 
	g++ -g -o $(NAME) $(NAME).o -lpthread -luser32 -lkernel32

$(NAME).o: $(NAME).cpp
	g++ -g -c $(NAME).cpp
make -f makefile.txt
cmd