码迷,mamicode.com
首页 > 编程语言 > 详细

MFC多线程之购票系统

时间:2014-11-27 00:17:27      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:cc++   mfc   操作系统   多线程   线程同步   

MFC线程--购票系统演示

       在大学操作系统课程中,进程和线程是一个经常谈到的话题,而购票系统是关于线程的一个经典的例子,它涉及到创建线程,线程同步等。

       废话不多说,下面就用MFC来实现该购票系统。

       1.新建一个MFC的对话框应用程序(工程名为 线程),按下图添加控件。

bubuko.com,布布扣

        2.在“线程Dlg.h”文件中添加以下代码

//结构体,传给线程的参数
struct Ctrls
{
	CProgressCtrl* progress;
	CStatic* text;
};

//声明线程中执行的函数
UINT Buy(LPVOID lpParameter);

     3.在“线程Dlg.cpp”文件中添加以下代码

CRITICAL_SECTION cs;  //定义一个关键代码段对象
int total;  //总票数
Ctrls ctrl1, ctrl2, ctrl3, ctrl4;

UINT Buy(LPVOID lpParameter)
{
	while (1)
	{	
		EnterCriticalSection(&cs);
		if (total > 0)
		{
			Ctrls* c = (Ctrls*)lpParameter;
			c->progress->StepIt();
			int num = c->progress->GetPos();
			CString s;
			s.Format("%d", num);
			c->text->SetWindowText(s);
			total--;
			Sleep(50);
			LeaveCriticalSection(&cs);
		}
		else
			break;		
	}
	return 0;
}
//按钮的点击事件
void CMyDlg::OnBuy() 
{
	// TODO: Add your control notification handler code here
	m_buy.EnableWindow(false);

	CString str;
	GetDlgItem(IDC_TOTAL)->GetWindowText(str);
	total = atoi(str);
	if (total <= 0 || total > 30000)
		MessageBox("请不要乱来!");

	InitializeCriticalSection(&cs);

	m_pro1.SetRange32(0, total);
	m_pro1.SetStep(1);
	ctrl1.progress = &m_pro1;
	ctrl1.text = &m_num1;

	m_pro2.SetRange32(0, total);
	m_pro2.SetStep(1);
	ctrl2.progress = &m_pro2;
	ctrl2.text = &m_num2;

	m_pro3.SetRange32(0, total);
	m_pro3.SetStep(1);
	ctrl3.progress = &m_pro3;
	ctrl3.text = &m_num3;

	m_pro4.SetRange32(0, total);
	m_pro4.SetStep(1);
	ctrl4.progress = &m_pro4;
	ctrl4.text = &m_num4;

	AfxBeginThread(Buy, &ctrl1);
	AfxBeginThread(Buy, &ctrl2);
	AfxBeginThread(Buy, &ctrl3);
	AfxBeginThread(Buy, &ctrl4);
}

--****用于创建工作者线程的函数如下:

CWinThread* AFXAPI AfxBeginThread(
	AFX_THREADPROC  pfnThreadProc,
	LPVOID  pParam,
	int  nPriority,
	UINT  nStackSize,
	DWORD  dwCreateFlags,
	LPSECURITY_ATTRIBUTES lpSecurityAttrs
);


     第一个参数 pfunThreadProc,表示线程的入口函数地址,函数的原形应该如同:UINT
MyControllingFunction( LPVOID pParam );
     第二个参数 pParam,表示传递给线程的参数。
     第三个参数 nPriority,表明线程的优先级,默认的优先级别 THREAD_PRIORITY_NORMAL,
如果为 0,则与创建该线程的线程相同。该参数有以下几种级别,下面是从高到低排序的:
            THREAD_PRIORITY_TIME_CRITICAL
            THREAD_PRIORITY_HIGHEST
            THREAD_PRIORITY_ABOVE_NORMAL
            THREAD_PRIORITY_NORMAL
            THREAD_PRIORITY_BELOW_NORMAL
            THREAD_PRIORITY_LOWEST
            HREAD_PRIORITY_IDLE
     第四个参数 nStackSize,表示线程的栈大小,如果为 0 表示使用系统默认值。
     第五个参数 dwCreateFlags,表示创建线程时的标记,若该参数为 CREATE_SUSPENDED
表示线程创建后呈挂起状态;如果为 0,表示该线程一建立就立即运行。
     第六个参数 lpSecurityAttrs,表示安全属性,该参数一般为 NULL。
    
     该函数调用成功的返回值是 CWinThread 类的指针,可以通过它实现对线程的控制。在
线程函数返回时线程将被结束,在线程内部可以利用 void AfxEndThread( UINT nExitCode );
结束线程,nExitCode 为退出码。
     工作者线程一旦启动,就开始执行控制函数,线程结束,控制函数也就结束了。线程控
制函数的原形如下:
UINT MyControllingFunction(LPVOID pParam);

     3.编译--链接--运行。结果如下:
bubuko.com,布布扣





MFC多线程之购票系统

标签:cc++   mfc   操作系统   多线程   线程同步   

原文地址:http://blog.csdn.net/liujian619/article/details/41525863

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!