hi,
I am creating a ThreadPool. I know that thread pool is collection of live threads. Its main purpose is to assign task to any of available thread. I am not going through any complex tutorial available on net. So I am just creating threadpool by my own.
For this I have created a class with following property

ThreadPoolMgr.h

class CThreadPoolMgr
{
public:
	CThreadPoolMgr(void);
public:
	~CThreadPoolMgr(void);

private:
	HANDLE	m_hThreadArray[5];
	DWORD	m_ThreadID[5];
	HANDLE	m_hMutex;	// Handle to mutex object

public:
	void Run(void); // Function which will get executed by thread
	static DWORD WINAPI ThreadProc(LPVOID Param); // Thread procedure
	void Initialize(int nThread);
	HANDLE GetMutex(void);

};

ThreadPoolMgr.cpp

CThreadPoolMgr::CThreadPoolMgr(void)
{
	m_hMutex = NULL;

	int cnt = 0;
	while(cnt <= 4)
	{
		// Initialize members
		m_hThreadArray[cnt] = NULL;
		m_ThreadID[cnt] = 0;

		cnt++;
	}

}

CThreadPoolMgr::~CThreadPoolMgr(void)
{
}

void CThreadPoolMgr::Run(void)
{	
	cout << "Thread " << GetCurrentThreadId() <<" is Running.\n";	
}


DWORD WINAPI CThreadPoolMgr::ThreadProc(LPVOID Param)
{
	// Create object of ThreadPoolMgr
	CThreadPoolMgr* objPool = (CThreadPoolMgr*)Param;
	
	//prevent to call Run function from all thread at a same time
	DWORD dwWaitResult = WaitForSingleObject( objPool->GetMutex(), INFINITE);
	
	switch( dwWaitResult )
	{
	case WAIT_OBJECT_0:
		
		// Call the Run function
		objPool->Run();		
		
		// Now Release Mutex
		if (! ReleaseMutex( objPool->GetMutex() )) 
		{
			cout << "ReleaseMutex Error: " << GetLastError() << "\n";
		}       
		break;
	}	
	return 0;
}

void CThreadPoolMgr::Initialize(int nThread)
{
	int nCounter = 0;
	int nThreadCount = nThread - 1;
	
	m_hMutex = CreateMutex(NULL, FALSE, L"MyMutex");

	while( nCounter <= nThreadCount )
	{
		m_hThreadArray[nCounter] = CreateThread(NULL,NULL,ThreadProc,this,0,&m_ThreadID[nCounter]);
		// Increment the counter
		nCounter++;
	}
	
	DWORD dwWaitResult = WaitForMultipleObjects( nThread, m_hThreadArray, TRUE, INFINITE);
	
	switch(dwWaitResult)
	{
	case WAIT_OBJECT_0:
		cout << "All threads are ended.\n";
		break;

	default:
		cout << "Wait Error: " << GetLastError() << "\n";
	}

}


HANDLE CThreadPoolMgr::GetMutex( void )
{
	return m_hMutex;
}

Though it is not completed fully. I have some doubt
1. I need to keep thread alive means in threadproc control should not reach on return 0; statement. Here i need your guidance. I think here i need a while loop to prevent control to reach at return 0. Or should i use event to notify shutdown. But i dont know how to use event here.

2. Where i should place mutex - In threadproc or in Run function.

I am not going through any complex tutorial available on net.

[.....]

Though it is not completed fully. I have some doubt
1......

2. ....

If you had read any "complex tutorial" you'd probably know these things. Now it sounds like you're saying: "Reading stuff takes to long, just show me how it works already.".
Let me reply with another question:
What do you think a mutex does?

niek_e actually i know concept. I haven't implemented it. so i am just trying to implement it. I know most of the api's and function used in multithreading. But threadpooling is new concept for me. Hence i have this silly questions.

Let me reply with another question:
What do you think a mutex does?
It is the synchronization object. Thread takes the ownership of mutex can only access shared code. If i am doing wrong suggest me, where to place mutex?

. If i am doing wrong suggest me, where to place mutex?

Depends on what you want:
1. You want none of the functionality in the run() function to run simultaneously: place the mutex in ThreadProc.
For example if your run() contains:

void CThreadPoolMgr::Run(void) {
     var_x++; 
     int bar = getStufFromDatabase(); 
     if (net_workline == 'free') 
         SendStuffOverNetwork ();
}

2. You want a small piece of the run() function to be protected: place it in the run
Example:

void CThreadPoolMgr::Run(void) {
    int counter = 0; // local
    std::cout << "blabla whatever"; 
    Singleton * single = Singleton::get(); // don't care about singletons
    // start mutex
    getDatabaseStuff (); 
    // release
}

[edit]

anyone else

Don't bump your post, it's very rude..

thanks for reply niek_e.
One doubt is clear. Sorry if I hurt you :(. I will take care of that.

For my first doubt i do try myself. If i stuck then i will take hint from you.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.