Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I hav 3 dynamic threads created by AfxBeginThread()in my MFC dialog based application such as worker thread1 reads from file1 to shared buffer, thread2 do some modifications in buffer and thread3 write the modified buffer data to a file2.These threads are in while loop until block becomes 0.I m using critical section for thread synchronization.

while(block>0)
{
Thread[0]=AfxBeginThread(thread1);
Thread[1]=AfxBeginThread(thread2);
Thread[2]=AfxBeginThread(thread3);
block--;
}
file1 is divided in blocks and thread1 is reading the file1 in blocks and thread2 modifiy the block data and thread3 write the blocks in file2
until the file end.so while loop is used..every iteration a new block is read, modified and written.

The problem arise that the scheduling of the thread is not achieved sometimes the thread1 is not read the data while thread2 and thread3 done the processing?How to achieve thread scheduling?
Posted
Updated 29-Mar-10 22:59pm
v2

I think you can use events to notify thread when the buffer is ready for read/write. http://msdn.microsoft.com/en-us/library/ms682396(VS.85).aspx[^]
 
Share this answer
 
sksksksksksksks wrote:
while(block>0)
{
Thread[0]=AfxBeginThread(thread1);
Thread[1]=AfxBeginThread(thread2);
Thread[2]=AfxBeginThread(thread3);
block--;
}


The above code doesn't look sane...Why are you using the loop? You know, if, before entering the loop, block is greater than 1, then thread array is overwritten (and that might be the last of your problems).
:)
 
Share this answer
 
Try it :) :
// .h
...
class CYourThreadsOwner
{
  CCriticalSection m_cCritSection;
   
  CWinThread* m_pcThreads[3];
 
  bool m_bWorking;
 
  BYTE m_byBuffer[1024]; 
 
  static DWORD WINAPI thread1(CYourThreadsOwner*);
  static DWORD WINAPI thread2(CYourThreadsOwner*);
  static DWORD WINAPI thread3(CYourThreadsOwner*);
 
public:
  CYourThreadsOwner();
  ~CYourThreadsOwner();
};

// .cpp
...
CYourThreadsOwner::CYourThreadsOwner()
{
  memset(m_byBuffer, 0, sizeof(m_byBuffer));
 
  m_bWorking = true;
 
  m_pcThreads[0] = AfxBeginThread(thread1, this);
  m_pcThreads[1] = AfxBeginThread(thread2, this);
  m_pcThreads[2] = AfxBeginThread(thread3, this);
}

CYourThreadsOwner::~CYourThreadsOwner()
{
  m_bWorking = false;
 
  HANDLE ahTherads[3];
  ahTherads[0] = m_pcThreads[0]->m_hThread;
  ahTherads[1] = m_pcThreads[1]->m_hThread;
  ahTherads[2] = m_pcThreads[2]->m_hThread;
 
  WaitForMultipleObjects(3, ahTherads, TRUE, INFINITE);
}
 
/*static*/ DWORD WINAPI CYourThreadsOwner::thread1(CYourThreadsOwner* pcOwnner)
{
  while (pcOwnner && pcOwnner->m_bWorking) {
    CSingleLock cLock(pcOwnner->m_cCritSection);
    cLock.Lock();
    // read here using pcOwner->m_byBuffer;
    cLock.Unlock();
    Sleep(10);
  }
  return 0;
}
 
/*static*/ DWORD WINAPI CYourThreadsOwner::thread2(CYourThreadsOwner* pcOwnner)
{
  while (pcOwnner && pcOwnner->m_bWorking) {
    CSingleLock cLock(pcOwnner->m_cCritSection);
    cLock.Lock();
    // modify here using pcOwner->m_byBuffer;
    cLock.Unlock();
    Sleep(10);
  }
  return 0;
}
 
/*static*/ DWORD WINAPI CYourThreadsOwner::thread3(CYourThreadsOwner* pcOwnner)
{
  while (pcOwnner && pcOwnner->m_bWorking) {
    CSingleLock cLock(pcOwnner->m_cCritSection);
    cLock.Lock();
    // save here using pcOwner->m_byBuffer;
    cLock.Unlock();
    Sleep(10);
  }
  return 0;
}
 
Share this answer
 
<b>Alok:This is very nice and precious example and description</b>
 
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