如何使用Cmutex和Cevent暂停和恢复线程? [英] how can Thread Suspended and resume using Cmutex and Cevent?

查看:118
本文介绍了如何使用Cmutex和Cevent暂停和恢复线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的编码:

//declaration
UINT thread(LPVOID param);
UINT thread2(LPVOID param);
CWinThread *pThread1,*pThread2;
CMutex  g_m;
CSingleLock lock1(&g_m);
CSingleLock lock2(&g_m);
//Implementation
void CMythreadDlg::OnBnClickedOk()
{
	p.pDlg = this;
	pThread1 = AfxBeginThread(thread, (LPVOID)&p);
}
void CMythreadDlg::OnBnClickedButton1()
{
	p.pDlg = this;
	pThread2 = AfxBeginThread(thread2, (LPVOID)&p);
	
}
UINT thread(LPVOID param)
{
        lock1.Lock();
         //do some process
	lock1.Unlock();
	return 0;
}
UINT thread2(LPVOID param)
{
	lock2.Lock();
	 //do some process
	lock2.Unlock();
	return 0;
}


我的问题:
我将单击事件处理程序,先前的工作线程已停止,另一个线程正在启​​动进程.
之后 线程工作将完成,然后开始工作,停止线程.如何做到这一点,请使用Windows窗口,您应始终使用WaitforEvents且永远不要锁定:一个事件警告线程!并使用Windows消息调度程序为每个正在运行的Windows进程工作:那么为什么要在具有两个不同锁的两个进程之间进行锁:您的程序部分是否设置了锁,并且同一锁又避免了两个线程之间的冲突?
最好的方法是使用互斥锁以获得最佳性能,并使用相同的互斥锁.


My Coding:

//declaration
UINT thread(LPVOID param);
UINT thread2(LPVOID param);
CWinThread *pThread1,*pThread2;
CMutex  g_m;
CSingleLock lock1(&g_m);
CSingleLock lock2(&g_m);
//Implementation
void CMythreadDlg::OnBnClickedOk()
{
	p.pDlg = this;
	pThread1 = AfxBeginThread(thread, (LPVOID)&p);
}
void CMythreadDlg::OnBnClickedButton1()
{
	p.pDlg = this;
	pThread2 = AfxBeginThread(thread2, (LPVOID)&p);
	
}
UINT thread(LPVOID param)
{
        lock1.Lock();
         //do some process
	lock1.Unlock();
	return 0;
}
UINT thread2(LPVOID param)
{
	lock2.Lock();
	 //do some process
	lock2.Unlock();
	return 0;
}


My question:
I will click event handler previous working thread is stopped and another thread is start process.After
thread work will be finished and then start work stopped thread .how can do this,give sample

解决方案

Using windows you should always use WaitforEvents and never lock : when there is an event warn the thread !! and use Windows message scheduller working for each windows process running : so why to lock between two process with two different locks : do your locks in the part of you progam and the same lock to avoid conflict between the two threads ?
The best way will be mutex for best performance and the same mutex.


这篇关于如何使用Cmutex和Cevent暂停和恢复线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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