”CreateThread()之后又马上CloseHandle()的问题“ 及 一些注意点

void main()
{
	// Create worker threads
	for( i=0; i < 2; i++ )
	{
		aThread[i] = CreateThread( 
			NULL,
			0,
			(LPTHREAD_START_ROUTINE) WriteToDatabase, 
			&i,       // 注意:这里是给线程回调函数传参数
			0,          
			&ThreadID);
	}
}

DWORD WINAPI WriteToDatabase( LPVOID lpParam )
{ 
	// 注意:这里不会得到0,1, 而是总是得到2。。
	// 因为主函数那里已经不断的循环,最终i的值已经被修改为2 后,这个
	// 线程函数才执行起来。  所以,诸如此类问题需要多多注意。
	int i = *(int*)lpParam; 
	
	return TRUE; 
}



此外,CreateThread()之后又马上CloseHandle()的问题, 可以参考http://blog.csdn.net/kofandlizi/article/details/6458011。

// 本段代码摘自 MSDN, 随便找WaitForSingleObject 函数就能找到这段代码了。
// 这里引用,目的在于做学习笔记。。
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>

#define THREADCOUNT 2

HANDLE ghMutex; 

DWORD WINAPI WriteToDatabase( LPVOID );

void main()
{
	HANDLE aThread[THREADCOUNT];
	DWORD ThreadID;
	int i;

	// Create a mutex with no initial owner

	ghMutex = CreateMutex( 
		NULL,              // default security attributes
		FALSE,             // initially not owned
		NULL);             // unnamed mutex

	if (ghMutex == NULL) 
	{
		printf("CreateMutex error: %d\n", GetLastError());
		return;
	}

	// Create worker threads

	for( i=0; i < THREADCOUNT; i++ )
	{
		aThread[i] = CreateThread( 
			NULL,       // default security attributes
			0,          // default stack size
			(LPTHREAD_START_ROUTINE) WriteToDatabase, 
			NULL,       // no thread function arguments
			0,          // default creation flags
			&ThreadID); // receive thread identifier

		if( aThread[i] == NULL )
		{
			printf("CreateThread error: %d\n", GetLastError());
			return;
		}
	}

	
	// 注意,CloseHandle 调用不能在这句前,因为这句都还需要 线程句柄呢。
	//
	// MSDN 有如下描述:
	// Return Value
	// If the function succeeds, the return value is a handle to the new thread. 
	// When you have finished using the handle, close it by calling the CloseHandle function. 
	// 所以如果不是因为 WaitForMultipleObjects 还需要用到线程句柄的话, 你在其他工程代码中看到
	// 类似如下代码(即调用完CreateThread后马上调用CloseHandle)也不会感到奇怪了。。
	// HANDLE hThread = CreateThread(NULL, 1024 * 1024 * 20, ReadFromDeviceThreadProc, this, 0, &dwThreadId);
	// if (hThread != NULL)
	// {
	// 	CloseHandle(hThread);
	// }
	//
	WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE); // Wait for all threads to terminate

	// Close thread and mutex handles
	for( i=0; i < THREADCOUNT; i++ )
		CloseHandle(aThread[i]);

	CloseHandle(ghMutex);
}

DWORD WINAPI WriteToDatabase( LPVOID lpParam )
{ 
	DWORD dwCount=0, dwWaitResult; 

	// Request ownership of mutex.

	while( dwCount < 20 )
	{ 
		dwWaitResult = WaitForSingleObject( 
			ghMutex,    // handle to mutex
			INFINITE);  // no time-out interval

		switch (dwWaitResult) 
		{
			// The thread got ownership of the mutex
		case WAIT_OBJECT_0: 
			__try { 
				// TODO: Write to the database
				printf("Thread %d writing to database...\n", 
					GetCurrentThreadId());
				dwCount++;
			} 

			__finally { 
				// Release ownership of the mutex object
				if (! ReleaseMutex(ghMutex)) 
				{ 
					// Deal with error.
				} 
			} 
			break; 

			// The thread got ownership of an abandoned mutex
		case WAIT_ABANDONED: 
			return FALSE; 
		}
	}
	return TRUE; 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值