Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Can we able to call three different functions(using thread) from the main program.

for Ex,

Calling A -------------> A Result

Calling B -------------> B Result

Calling C -------------> C Result

Waiting for the Result of all threads

Calling another function ..

----Can any one please help me out in giving syntax or coding ....
Posted
Comments
Resmi Anna 23-Apr-12 5:05am    
Are you using MFC?
Pixel86 23-Apr-12 19:31pm    
Yes, i am using MFC..
Prasad_Kulkarni 24-Apr-12 0:01am    
Glad it helps!
Thank You for accepting solution.

Please refer following thread:
Multithreading Tutorial[^]
Multithread Example[^]
C Multithreading tutorial/[^]
Hope you get some help.
 
Share this answer
 
Well the idea of the threads is to make different things at the same time . Normally you can call three different threads A,B,C and wait they to be done with their work and then to call some other function . But there is a catch. You know that every program has resources. For example files which you open ,close ,read from , write to . The problem is when you want to access to some resource from two or more threads at the same time . This means when you use the resource in one of the threads and before the thread is done to try access the same resource from other thread. For solving this kind of problem you need Mutex, Events , Semaphores and many other things. For writing threads and managing them you have many choices. You can use afx library, mfc, and other stuff . I recommend you to look at these tutorials and choose what best suits you.

Simple Multithreaded Application in pure C, Win32 and MFC
Creating a C++ Thread Class
CThread - a Worker Thread wrapper class
Introduction to Multi-threaded Code
Synchronization in Multithreaded Applications with MFC

I hope this helps you :)
 
Share this answer
 
Yes, you may do that. Check out the threading documentation of your favourite platform (threads are platform-dependent) for the technical details.
You may also find many tutorials on the web, for instance, if you're using POSIX threads then have a look at "Example: Pthread Joining"[^].
 
Share this answer
 
C++
#include <process.h>
:
HANDLE g_hEvent[3];
void ThreadFunA( LPVOID lpParam)
{
	for( int i = 0; i< 100; i++ )
	{
		// Do something
	}
	SetEvent( g_hEvent[0] );
	return;
}
void ThreadFunB( LPVOID lpParam)
{
	for( int i = 0; i< 100; i++ )
	{
		// Do something;
	}
	SetEvent( g_hEvent[1] );
	return;
}
void ThreadFunC( LPVOID lpParam)
{
	for( int i = 0; i< 100; i++ )
	{
		// Do something
	}
	SetEvent( g_hEvent[2] );
	return;
}
int _tmain(int argc, _TCHAR* argv[])
{
	g_hEvent[0] = CreateEvent( NULL, TRUE, FALSE, NULL ); 
	g_hEvent[1] = CreateEvent( NULL, TRUE, FALSE, NULL ); 
	g_hEvent[2] = CreateEvent( NULL, TRUE, FALSE, NULL ); 

	ResetEvent( g_hEvent[0] );
	ResetEvent( g_hEvent[1] );
	ResetEvent( g_hEvent[2] );

	
	HANDLE handle1 = (HANDLE) _beginthread( ThreadFunA, 0, NULL );
	HANDLE handle2 = (HANDLE) _beginthread( ThreadFunB, 0, NULL );
	HANDLE handle3 = (HANDLE) _beginthread( ThreadFunC, 0, NULL );
	

	DWORD dwResult = WaitForMultipleObjects( 3, g_hEvent, true, INFINITE );
	switch( dwResult )
	{
	case WAIT_OBJECT_0:
		// Print your Result
		break;
	case WAIT_TIMEOUT:
		break;
	case WAIT_FAILED:
		break;
	}
	getchar();
	return 0;
}

to understand the code better google for the topic "Thread synchronization in c++"
 
Share this answer
 
v2

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