Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For dynamic thread array in my MFC application
The no of child thread depends on runtime.

CWinThread **Thread;
UINT ParentTh()
{
    ......
    int block;
    cnt=0;

    Thread[cnt]=(CWinThread *)malloc(sizeof(CWinThread));//gives access vilation
    .....
    while(block>0)
    {
        Thread[cnt]=AfxBeginThread(Child1...);
        cnt++;
        Thread[cnt]=AfxBeginThread(child2...);
        cnt++;
    }
    ....


Plz help...
for unbounded thread allocation

Thread[cnt]=(CWinThread*)malloc(sizeof(CWinThread));//dy memory for 1 thread


and gradually the cnt increases upto block*2
Posted
Updated 15-Apr-10 0:16am
v3

1 solution

You could try something like this :) :
class CThreadPair
{
  CWinThread* m_pcThread1,
              m_pcThread2;
public:
  CThreadPair();
  ~CThreadPair();
  
  StartThreads();
  StopThreads();
}

{
  CPtrArray m_cThreadPairsArray;
  
  int iPairsCount(12);
  while (iPairsCount--) {
    CThreadPair* pcPair = new CThreadPair();
    if (pcPair) {
      pcPair->StartThreads();
      m_cThreadPairsArray.Add(pcPair);
    }
  }
  
  Sleep(20000);
  
  for (int i = 0; i < cThreadPairsArray.GetCount(); i++) {
    CThreadPair* pcPair = (CThreadPair*) cThreadPairsArray[i];
    if (pcPair) {
      pcPair->StopThreads();
      delete pcPair;
      pcPair = NULL;
    }
  }
  
  cThreadPairsArray.RemoveAll();
}
 
Share this answer
 

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