初次尝试C++调用WINAPI创建多线程(内容较乱,细心读懂便能初步理解多线程)

// test1.cpp : 定义控制台应用程序的入口点。
//
//
//#include "stdafx.h"
//
//
//int main()
//{
//    return 0;
//}
//

#include "stdafx.h"
#include 
#include 
#include 
#include 
#include 
using namespace std;
void sleepdelay(float s);
DWORD WINAPI test_one(LPVOID pM);
DWORD WINAPI test_two(LPVOID pM);

int main()
{
	HANDLE handle_one = CreateThread(NULL, 0, test_one, NULL, 0, NULL);
	//WaitForSingleObject(handle_one, INFINITE);//挂起当前线程,等待线程handle_one返回信号,第二个参数是等待时间最大值,INFNITE是指无限等待;
	CloseHandle(handle_one);
	HANDLE handle_two = CreateThread(NULL, 0, test_two, NULL, 0, NULL);
	WaitForSingleObject(handle_two, INFINITE);

	for (int i = 0; i < 10; i++)
	{
		cout << "main thread!" << endl;
		sleepdelay(0.5);
	}
	return 0;
}

//DWORD WINAPI ThreadFun(LPVOID pM)
//{
//	printf("子线程的线程ID号为:%d\n子线程输出Hello World\n", GetCurrentThreadId());
//	return 0;
//}
////主函数,所谓主函数就是主线程执行的函数
//int main()
//{
//	printf("  最简单的创建多线程实例\n");
//	HANDLE handle = CreateThread(NULL, 0, ThreadFun, NULL, 0, NULL);
//	WaitForSingleObject(handle, INFINITE);
//	return 0;
//}
void sleepdelay(float s)
{
	clock_t delay = s * CLOCKS_PER_SEC;
	clock_t start = clock();
	while (clock() - start < delay);
}
DWORD WINAPI test_one(LPVOID pM)
{
	cout << "Enter the delay time in seconds:";
	float secs;
	//cin >> secs;
	secs = 2;
	clock_t delay = secs*CLOCKS_PER_SEC;
	cout << "starting\a\n";         //\a指警告声音
	clock_t start = clock();
	int i = 1;
	while (clock() - start < delay)
	{
		if (i> word;
	word = "word";
	int i = 0;
	while (1)
	{
		if (i >= word.length())//sizeof(word)
		{
			break;
		}
		cout << "_"; sleepdelay(0.5); cout << "\b"; sleepdelay(0.5); cout << word[i]; i++;
	}
	cout <

你可能感兴趣的:(编码知识)