Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C++
#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;
}
Posted
Updated 29-Jun-15 20:35pm
v2

1 solution

You may retrieve the thread return value using the GetExitCodeThread[^] function.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Jun-15 2:53am    
Sure, a 5.
—SA
CPallini 30-Jun-15 3:23am    
Thank you, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900