Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1221882
  • 博文数量: 950
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 13070
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-04 09:23
文章分类

全部博文(950)

文章存档

2011年(1)

2008年(949)

我的朋友

分类: C/C++

2008-08-04 09:32:23

下载本文示例代码
下载源代码


  很多情况下我们需要多个线程互相协助,来完成同一个任务。但是线程很难从外部进行控制。利用线程同步技术可以使线程彼此交互,从而避免了外部控制对与时间和资源的浪费。在实际工作过程中我就遇到了类似的问题,需要对共享的缓冲区进行操作。有插入的线程也有读取的线程,这使我忽然想到了生产者和消费者。我从MSDN 找到 CSDN,使用了各式各样的搜索引擎,只找到了很有限的关于CSemaphore的资料。
、生产者消费者问题
生产者要不断将数据放入共享的缓冲,消费者要不断从缓冲取出数据。消费者必须等生产者
取走数据后才能再放新数据(不覆盖数据),消费者必须等生产者放入新数据后才能去取(不重复)。
、使用信号量的方法
当线程使用指定数量的共享资源时,首先调用信号量的lock方法"我能用资源吗"。当有的空闲
共享资源时(此时计数器值>0)线程继续执行并且减少计数器的数量告诉其他线程"我用了××个资源"。否则挂起自己直到有足够的可用的资源为止。当使用完资源时线程调用unlock方法告诉其他线程"我已经不用该资源了"。
、实现

1、创建一个基于对话框的程序。添加如下成员:
//缓慢显示线程进行的结果

bool m_bSlow;



//生产者线程

CProducerThread *m_pProducerThread;



//消费者线程

CConsumerThread *m_pConsumerThread;



//缓冲空的标志

CSemaphore* m_pSemaphoreEmpty;



//缓冲满的标志

CSemaphore* m_pSemaphoreFull;



CMutex *m_pMutex;//互斥信号量



添加两编辑框用类向导,相关的添加成员



//用来显示消费者取到的数据

CString m_sBufCSM;



//显示生产者插入缓冲的数据

CString m_sBuf;     
2、创建用户界面线程,生产者和消费者线程。
CProducerThread::CProducerThread(void* hParent):m_pParentDlg(hParent)

{ 

}



int CProducerThread::Run() 

{

	CP_CDlg *pDlg;

	pDlg=(CP_CDlg*)m_pParentDlg;

	CSingleLock mutexLock(pDlg->m_pMutex);

	

	for(int i=0;im_pSemaphoreEmpty->Lock();

		mutexLock.Lock();

		pDlg->m_sBuf.Format("%0.10d",i);    

		mutexLock.Unlock();

		pDlg->m_pSemaphoreFull->Unlock();  

	}

	

	return CWinThread::Run();

}

CConsumerThread::CConsumerThread(void *pParent)

:m_pParent(pParent)

{

	

}

int CConsumerThread::Run() 

{

	CP_CDlg *pDlg;

	pDlg=(CP_CDlg*)this->m_pParent; 

	char*pBuf;

	pBuf=this->m_Data;

	bool bSleep;

	for(int i=0;im_pSemaphoreFull->Lock();

		pDlg->m_pMutex->Lock();

		sprintf(pBuf,pDlg->m_sBuf);

		bSleep=pDlg->m_bSlow ;

		pDlg->m_pMutex->Unlock();    

		pBuf =10;

		if(pBuf>m_Data CSM_BUF_COUNT-10)

			pBuf=m_Data;

		m_Data[CSM_BUF_COUNT]=0;

		pDlg->m_pMutex->Lock();

		sprintf(pDlg->m_sBufCSM.GetBuffer(CSM_BUF_COUNT 10),m_Data);

		pDlg->m_pMutex->Unlock();

		if (bSleep)

			Sleep(100);

		pDlg->m_pSemaphoreEmpty ->Unlock();

	}

	

	

	return CWinThread::Run();

}      
3、启动线程:
m_pSemaphoreFull =new CSemaphore(1,1);

m_pSemaphoreEmpty =new CSemaphore(0,1);

m_pMutex =new CMutex;

this->m_bUpdateAuto =false;



this->m_pProducerThread =new CProducerThread(this);

this->m_pConsumerThread =new CConsumerThread(this);

this->m_sBuf.Format("1234567890");

this->UpdateData(false);

this->m_pProducerThread->CreateThread(CREATE_SUSPENDED);

VERIFY(m_pProducerThread->SetThreadPriority(THREAD_PRIORITY_IDLE)); 

this->m_pConsumerThread->CreateThread(CREATE_SUSPENDED);

VERIFY(m_pConsumerThread->SetThreadPriority(THREAD_PRIORITY_IDLE)); 

this->m_pProducerThread->ResumeThread(); 

this->m_pConsumerThread->ResumeThread(); 

下载本文示例代码
阅读(273) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~