Start the same thread more than once with different param! <URGENT>
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Start the same thread more than once with different param! <URGENT>

  1. #1
    Join Date
    Feb 2000
    Posts
    277

    Exclamation Start the same thread more than once with different param! <URGENT>

    Hi Everybody!

    I'm making an MFC application that simulate a routing algorithm.

    I have some nodes that are called clusterheads that have the control all the nodes in the cluster (members).

    Struct:

    p->ch bool: is or not a CH?
    p->nCH int: number of the members of this CH
    p->CM[] int[]: array with all the ID the members of the cluster.


    my code is:

    Code:
    void CSimDlg::StartSS(void)
    {
    	CString msg;
    	
    	//scan all nodes untill you find a ClusterHead
    
    
    	for (int i=0; i<m_vecNodes.size(); i++)
    	{
    		NODE* p = m_vecNodes[i];
    		
    		if(p->ch){
    			
    			//Cluster Head found
    
    			inttopass=i;
    
    			_beginthread(StaticClusterStart, 0, this);
    
    			break;
    		}
    	}
    
    
    }
    
    void CSimDlg::StaticClusterStart(void * param)
    {
    	CSimDlg * pThis = (CSimDlg *)param;
    	ASSERT(pThis != NULL);
    	pThis->ClusterStart();
    }
    
    void CSimDlg::ClusterStart(void)
    {
    	NODE* p = m_vecNodes[inttopass];
    
    	// for the ClusterHead "inttopass" activate one node each SST/p->nCM seconds
    
    	if(p->ch){
    
    		for(int j=0; j<p->nCM; j++){
    		ActivateNode(p->CM[j]);
    		Sleep(SST/p->nCM);
    	}
    	
    	_endthread();
    }
    Now:
    If I run this code everything is fine, and thanks to the "break" after "_beginthread(StaticClusterStart, 0, this);" I take the first Cluster head in the list and I start a thread that activate each node of it.

    The problem is that I need to start the same funcion ClusterStart(void) for each Cluster head, at the same time.
    so the best thing witll be to do something like this:

    Code:
    for (int i=0; i<m_vecNodes.size(); i++)
    	{
    		NODE* p = m_vecNodes[i];
    		
    		if(p->ch){
    			
    			//Cluster Head found
    
    			inttopass=i;
    
    			BEGIN a thread for CH i; 
    		}
    	}
    Plus the number of CH is variable each time i run the program so I cannot make different function like:
    _beginthread(StaticClusterStart1, 0, this);
    _beginthread(StaticClusterStart2, 0, this);
    ...
    _beginthread(StaticClusterStartN, 0, this);


    Do you have any ideas how can I solve this problem??? Any suggestion or any comment to the code?

    One more thing: how can I pass the int "intopass" from void CSimDlg::StartSS(void) to void CSimDlg::ClusterStart(void) ???


    I hate to say that it's urgent but it really is...
    Thankyou in advance!
    blp

  2. #2
    Join Date
    Feb 2002
    Posts
    5,758

    Re: Start the same thread more than once with different param! <URGENT>

    What's the question?

    Kuphryn

  3. #3
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Start the same thread more than once with different param! <URGENT>

    Add a struct
    Code:
    struct ThreadParam
    {
       CSimDlg* m_pDialog;
        int   m_nNodeIndex;
    };
    Where you beginthread, do this:
    Code:
    if(p->ch)
    {
       //Cluster Head found
       ThreadParam* pParam = new ThreadParam;
       pParam->m_pDialog = this;
       pParam->m_nNodeInxex = i;
       _beginthread(StaticClusterStart, 0, (void*)pParam);
       break;
    }
    In your thread proc:
    Code:
    void CSimDlg::StaticClusterStart(void * param)
    {
    	ThreadParam* pParam = (ThreadParam* pParam)param;
    	pParam->m_pDialog->ClusterStart(pParam->m_nNodeIndex);
            delete pParam;
    }
    Modify ClusterStart as:
    Code:
    void CSimDlg::ClusterStart(int nNodeIndex)
    {
    	NODE* p = m_vecNodes[nNodeIndex];
    	if(p->ch)
           {
             for(int j=0; j<p->nCM; j++){
    	 ActivateNode(p->CM[j]);
    	 Sleep(SST/p->nCM);
           }
    }

  4. #4
    Join Date
    Feb 2000
    Posts
    277

    Re: Start the same thread more than once with different param! <URGENT>

    ThankYou for Your reply, It works!!!

    one more thing how can I make my app wait for all the thread to finish?

    ThankYou again
    blp

  5. #5
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Start the same thread more than once with different param! <URGENT>

    You will need to store the return value of _beginthread which is a thread handle.

    So,
    HANDLE hThread = (HANDLE)_beginthread(....);

    And then, when you want to wait for it to end..

    WaitForSingleObject(hThread,INFINITE);

    This function will wait till the handle is signalled and in case of thread handles, they are signalled when the thread terminates.

    Please search on these APIs and you will find enough info. Just providing you the starting point as to where to look.

    Also, you may want to look at _beginthreadex function.

    There are times when you want to use this instead of _beginthread when you are using crt calls. Please read thru this also on MSDN.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,485

    Re: Start the same thread more than once with different param! <URGENT>

    Quote Originally Posted by kirants
    Where you beginthread, do this:
    Code:
    if(p->ch)
    {
      //Cluster Head found
      ThreadParam* pParam = new ThreadParam;
      pParam->m_pDialog = this;
      pParam->m_nNodeInxex = i;
      _beginthread(StaticClusterStart, 0, (void*)pParam);
      break;
    }
    Shouldn't the ThreadParam* pParam be move into class scope to avoid the memory leak?

    Arjay

  7. #7
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Start the same thread more than once with different param! <URGENT>

    Not sure. The OP is spawning threads from the class. So, if it were to be moved to class scope, he'd have to have an array of ThreadParam structures, right ?

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,485

    Re: Start the same thread more than once with different param! <URGENT>

    Kirant, I missed where you delete the pParam in the thread function - my bad.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center