首页 > 编程语言 > 详细

一步一步学习多线程编程之CreateThread

时间:2015-04-06 11:31:22      阅读:182      评论:0      收藏:0      [点我收藏+]

CreatThread函数如下图所示

技术分享


在这里我们只用到了第三个和第四个参数,第三个参数传递了一个函数的地址,也是我们要指定的新的线程。第四个参数是传给新线程的参数指针。

// ThreadCreateTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<windows.h>

int time = 0;
void ThreadProc1()
{

	int hour = 0;
	int mins = 0;
	while(1)
	{
		hour = time/60;
		mins = time%60;
		
		int day = hour/24;
		
		if (day)
		{
			hour -=day*24;
		}
		time++;
		
		printf("线程1 %d:%d\n",hour,mins);
		Sleep(1000);
	   // system("cls");
	}

}

void ThreadProc2()
{
	int hour = 0;
	int mins = 0;
	while(1)
	{
		
		hour = time/60;
	    mins = time%60;
		int day = hour/24;//超过24个小时的话

		if (day)
		{
			hour -=day*24;
		}
		time++;
		printf("线程2 %d:%d\n",hour,mins);
		Sleep(1000);
		//system("cls");

	}
}

int main()
{

	HANDLE hThread1  = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc1,NULL,0,0);
	CloseHandle(hThread1);
	HANDLE hThread2 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc2,NULL,0,0);
	CloseHandle(hThread2);
	
	getchar();
	return 0;
	
	
}
        尽管上面程序看上去是并发进行,但是有时候也不一定按照线程1:线程2的输出。这涉及到多线程的同步问题。对于一个资源被多个线程共用会导致程序的混乱,我们的解决方法是只允许一个线程拥有对共享资源的独占,这样就能够解决上面的问题了。

HANDLE CreateMutex

 LPSECURITY_ATTRIBUTESlpMutexAttributes,

 BOOLbInitialOwner

 LPCTSTRlpName );

Parameters

lpMutexAttributes
[in] Ignored. Must be NULL.
bInitialOwner
[in] Boolean that specifies the initial owner of the mutex object. If this value is TRUE and the caller created the mutex, the calling thread obtains ownership of the mutex object. Otherwise, the calling thread does not obtain ownership of the mutex. To determine if the caller created the mutex, see the Return Values section.
lpName
[in] Long pointer to a null-terminated string specifying the name of the mutex object. The name is limited to MAX_PATH characters and can contain any character except the backslash path-separator character (\). Name comparison is case sensitive.

If lpName matches the name of an existing named mutex object, the bInitialOwner parameter is ignored because it has already been set by the creation process.

If lpName is NULL, the mutex object is created without a name.

If lpName matches the name of an existing event, semaphore, or file-mapping object, the function fails and theGetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space. 

该函数用于创造一个独占资源,第一个参数我们没有使用,可以设为NULL,第二个参数指定该资源初始是否归属创建它的进程,第三个参数指定资源的名称。

技术分享

技术分享

HANDLE hMutex;
void ThreadProc1()
{
	int hour = 0;
	int mins = 0;
	while(1)
	{
		WaitForSingleObject(hMutex, INFINITE);
		hour = time/60;
		mins = time%60;
		
		int day = hour/24;
		
		if (day)
		{
			hour -=day*24;
		}
		time++;
		
		printf("线程1 %d:%d\n",hour,mins);
		Sleep(1000);
		ReleaseMutex(hMutex);
	   // system("cls");
	}

}



一步一步学习多线程编程之CreateThread

原文:http://blog.csdn.net/xy010902100449/article/details/44899917

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!