线程同步-MFC vc ++中while循环中的AfxBeginThread() [英] Thread Synchronization-- AfxBeginThread() in while loop in MFC vc++??

查看:145
本文介绍了线程同步-MFC vc ++中while循环中的AfxBeginThread()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个由AfxBeginThread()在基于MFC对话框的应用程序中创建的动态线程,例如工作线程1从文件1读取到共享缓冲区,线程2在缓冲区中进行了一些修改,线程3将修改后的缓冲区数据写入到文件2中.在while循环中直到块变为0.我使用临界区进行线程同步.

while(block> 0)
{
线程[0] = AfxBeginThread(thread1);
线程[1] = AfxBeginThread(thread2);
线程[2] = AfxBeginThread(thread3);
块-;
}
file1分为块,线程1读取块中的file1,线程2修改块数据,线程3将块写入file2中
直到文件end.so while循环被使用..每次迭代都会读取,修改和写入一个新块.

出现的问题是,有时在线程2和线程3进行处理时,线程1不能读取数据,而线程1和线程3却没有实现?如何实现线程调度?

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?

推荐答案

我认为当缓冲区准备好进行读/写操作时,您可以使用事件来通知线程. http://msdn.microsoft.com/en-us/library/ms682396(VS .85).aspx [ ^ ]
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[^]


sksksksksksksksks写道:
sksksksksksksks wrote:

while(block> 0)
{
线程[0] = AfxBeginThread(thread1);
线程[1] = AfxBeginThread(thread2);
线程[2] = AfxBeginThread(thread3);
块-;
}

while(block>0)
{
Thread[0]=AfxBeginThread(thread1);
Thread[1]=AfxBeginThread(thread2);
Thread[2]=AfxBeginThread(thread3);
block--;
}



上面的代码看起来并不理智...为什么要使用循环?您知道,如果在进入循环之前block大于1,那么thread数组将被覆盖(这可能是您的最后一个问题).
:)



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).
:)


尝试一下:):
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;
}


这篇关于线程同步-MFC vc ++中while循环中的AfxBeginThread()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆