本文介绍了如何保存ThreadOne的返回值,即0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<windows.h>
#include<process.h>     // needed for _beginthread()


CRITICAL_SECTION m_cs;


int ThreadOne(void *Param){
	int i;
	//Lock the critical section
	EnterCriticalSection(&m_cs);
	for (i=0; i<10; i++){
		printf("Function 1 : %d\n", i);
	}
	//Release the Critical section
	LeaveCriticalSection(&m_cs);

	//Return the thread
	return 0;
}

int ThreadTwo(void *Param){
	int i;
	//Lock the critical section
	EnterCriticalSection(&m_cs);
	
	for (i=0; i<10; i++){
		printf("Function 2 : %d\n", i);
	}
	//Release the Critical section
	LeaveCriticalSection(&m_cs);

	//Return the thread
	return 0;
}

int main()
{    
	// Create the array of Handle
	HANDLE hThrd[2];
	
	//Initialize the critical section
	InitializeCriticalSection(&m_cs);
	
	// Create threads use CreateThread function with NULL Security
	hThrd[0] = _beginthread(ThreadOne, 0, (void *)0);
	hThrd[1] = _beginthread(ThreadTwo, 0, (void *)0);
		
	// Wait for the main thread 
	WaitForMultipleObjects(2,hThrd,TRUE,INFINITE);

	// Delete critical Section
	DeleteCriticalSection(&m_cs);

	getchar();
	return 0;
}

推荐答案


这篇关于如何保存ThreadOne的返回值,即0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 08:57