CCriticalSection protectaSide;
int CHexMeshView::SearchTrgl(double *x, double *y, double *z, int *crt, int n)
{
CWinThread* pThread;
CWinThread* qThread;
CWinThread* rThread;
pThread=AfxBeginThread(MulThreadSearchTrgl1,this);
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
return trcount;
}
UINT /*CHexMeshView::*/MulThreadSearchTrgl1(LPVOID lpParameter){ //线程函数1
CHexMeshView * athis = (CHexMeshView *) lpParameter;
for(int i=0;i<1000000;i++){
protectaSide.Lock();
trcount=trcount+i;
protectaSide.Unlock();
}
return 0;
}
UINT /*CHexMeshView::*/MulThreadSearchTrgl2(LPVOID lpParameter){ //线程函数2
CHexMeshView * athis = (CHexMeshView *) lpParameter;
for(int i=1000000;i<2000000;i++){
protectaSide.Lock();
trcount=trcount+i;
protectaSide.Unlock();
}
return 0;
}
UINT /*CHexMeshView::*/MulThreadSearchTrgl3(LPVOID lpParameter){ //线程函数3
CHexMeshView * athis = (CHexMeshView *) lpParameter;
for(int i=0000000;i<3000000;i++){
protectaSide.Lock();
trcount=trcount+i;
protectaSide.Unlock();
}
return 0;
}
11 个解决方案
#1
pThread=AfxBeginThread(MulThreadSearchTrgl1,this);
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
HANDLE hThread[3];
hThread[0] = pThread;
hThread[1] = qThread;
hThread[2] = rThread;
WaitForMultipleObjects(3,hThread,TRUE,INFINITE)
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
HANDLE hThread[3];
hThread[0] = pThread;
hThread[1] = qThread;
hThread[2] = rThread;
WaitForMultipleObjects(3,hThread,TRUE,INFINITE)
#2
顶楼上的
HANDLE hThread[3];
hThread[0] = AfxBeginThread(MulThreadSearchTrgl1,this);
hThread[1] = AfxBeginThread(MulThreadSearchTrgl2,this);
hThread[2] = AfxBeginThread(MulThreadSearchTrgl3,this);
WaitForMultipleObjects(3,hThread,TRUE,INFINITE);
#3
__int64 sumInt(int value)
{
__int64 trcount=0;
for(int i=1000000;i<2000000;i++){
trcount+=i;
}
return trcount;
}
__int64 trcount1 = 0;
__int64 trcount2 = 0;
__int64 trcount3 = 0;
Concurrency::parallel_invoke(
[&trcount1 ] { trcount1 = sumint(1000000); },
[&trcount2 ] { trcount2 = sumint(2000000); },
[&trcount3 ] { trcount3 = sumint(3000000); }
);
__int64 result=trcount1+trcount2+trcount3;
{
__int64 trcount=0;
for(int i=1000000;i<2000000;i++){
trcount+=i;
}
return trcount;
}
__int64 trcount1 = 0;
__int64 trcount2 = 0;
__int64 trcount3 = 0;
Concurrency::parallel_invoke(
[&trcount1 ] { trcount1 = sumint(1000000); },
[&trcount2 ] { trcount2 = sumint(2000000); },
[&trcount3 ] { trcount3 = sumint(3000000); }
);
__int64 result=trcount1+trcount2+trcount3;
#4
请问楼上使用
HANDLE hThread[3];
hThread[0] = AfxBeginThread(MulThreadSearchTrgl1,this);
hThread[1] = AfxBeginThread(MulThreadSearchTrgl2,this);
hThread[2] = AfxBeginThread(MulThreadSearchTrgl3,this);
WaitForMultipleObjects(3,hThread,TRUE,INFINITE);
然而子线程还是没有结束,主线程就退出了,这是怎么回事?请大侠不吝赐教!!
HANDLE hThread[3];
hThread[0] = AfxBeginThread(MulThreadSearchTrgl1,this);
hThread[1] = AfxBeginThread(MulThreadSearchTrgl2,this);
hThread[2] = AfxBeginThread(MulThreadSearchTrgl3,this);
WaitForMultipleObjects(3,hThread,TRUE,INFINITE);
然而子线程还是没有结束,主线程就退出了,这是怎么回事?请大侠不吝赐教!!
#5
迅速围观大牛,顺带问一下,这是神马?
#6
CHexMeshView是窗口类来的吧,会自己退出吗,还是你强行让它退出了
#7
不过,不建议在窗口线程中等待线程结束
#8
CHexMeshView类是Cview的派生类。
CCriticalSection protectaSide;
int CHexMeshView::SearchTrgl(double *x, double *y, double *z, int *crt, int n)
{
CWinThread* pThread;
CWinThread* qThread;
CWinThread* rThread;
pThread=AfxBeginThread(MulThreadSearchTrgl1,this);
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
HANDLE hThread[3];
hThread[0] = pThread;
hThread[1] = qThread;
hThread[2] = rThread;
WaitForMultipleObjects(3,hThread,TRUE,INFINITE) //分步运行时,此行花费时间很短,理论上说,如果要等待三个子线程都运行完的话,此行要等待几分钟。
//Sleep(180000) //如果让主线程沉睡3分钟的话,运行结果正确
return trcount;
}
请问大侠我如何能让WaitForMultipleObjects(3,hThread,TRUE,INFINITE)成功等待
CCriticalSection protectaSide;
int CHexMeshView::SearchTrgl(double *x, double *y, double *z, int *crt, int n)
{
CWinThread* pThread;
CWinThread* qThread;
CWinThread* rThread;
pThread=AfxBeginThread(MulThreadSearchTrgl1,this);
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
HANDLE hThread[3];
hThread[0] = pThread;
hThread[1] = qThread;
hThread[2] = rThread;
WaitForMultipleObjects(3,hThread,TRUE,INFINITE) //分步运行时,此行花费时间很短,理论上说,如果要等待三个子线程都运行完的话,此行要等待几分钟。
//Sleep(180000) //如果让主线程沉睡3分钟的话,运行结果正确
return trcount;
}
请问大侠我如何能让WaitForMultipleObjects(3,hThread,TRUE,INFINITE)成功等待
#9
如果使用互斥量同步的话(代码如下),只开辟2个子线程,在主线程里执行一段与第三个子线程功能一样的代码,这样结果是对的(在此不知为什么临界区同步就不行)。问题是这样代码就不整齐,更重要的是据说临界区速度比互斥量快,因为我的程序在此要运行半个多小时,我想使用临界区提高速度。
CMutex protectaSide;
int CHexMeshView::SearchTrgl(double *x, double *y, double *z, int *crt, int n)
{
CWinThread* pThread;
CWinThread* qThread;
CWinThread* rThread;
pThread=AfxBeginThread(MulThreadSearchTrgl1,this);
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
//rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
for(int i=2000000;i<3000000;i++){ //在主线程中使用代码代替第三个子线程
protectaSide.Lock();
trcount=trcount+i;
protectaSide.Unlock();
}
return trcount;
}
CMutex protectaSide;
int CHexMeshView::SearchTrgl(double *x, double *y, double *z, int *crt, int n)
{
CWinThread* pThread;
CWinThread* qThread;
CWinThread* rThread;
pThread=AfxBeginThread(MulThreadSearchTrgl1,this);
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
//rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
for(int i=2000000;i<3000000;i++){ //在主线程中使用代码代替第三个子线程
protectaSide.Lock();
trcount=trcount+i;
protectaSide.Unlock();
}
return trcount;
}
#10
望各位大侠不吝赐教!!
#11
CWinThread* pThread[3] = {0};
HANDLE hThread[3] = {0};
pThread[0] = AfxBeginThread(ThreadProc1, (LPVOID)GetSafeHwnd(), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
pThread[1] = AfxBeginThread(ThreadProc2, (LPVOID)GetSafeHwnd(), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
pThread[2] = AfxBeginThread(ThreadProc3, (LPVOID)GetSafeHwnd(), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
for(int i=0; i<sizeof(pThread)/sizeof(pThread[0]); i++)
{
hThread[i] = pThread[i]->m_hThread;
pThread[i]->m_bAutoDelete = FALSE;
pThread[i]->ResumeThread();
}
WaitForMultipleObjects(sizeof(pThread)/sizeof(pThread[0]), hThread, TRUE, INFINITE);
for(int i=0; i<sizeof(pThread)/sizeof(pThread[0]); i++)
{
ASSERT(CloseHandle(pThread[i]->m_hThread));
delete pThread[i];
pThread[i] = NULL;
}
#1
pThread=AfxBeginThread(MulThreadSearchTrgl1,this);
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
HANDLE hThread[3];
hThread[0] = pThread;
hThread[1] = qThread;
hThread[2] = rThread;
WaitForMultipleObjects(3,hThread,TRUE,INFINITE)
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
HANDLE hThread[3];
hThread[0] = pThread;
hThread[1] = qThread;
hThread[2] = rThread;
WaitForMultipleObjects(3,hThread,TRUE,INFINITE)
#2
顶楼上的
HANDLE hThread[3];
hThread[0] = AfxBeginThread(MulThreadSearchTrgl1,this);
hThread[1] = AfxBeginThread(MulThreadSearchTrgl2,this);
hThread[2] = AfxBeginThread(MulThreadSearchTrgl3,this);
WaitForMultipleObjects(3,hThread,TRUE,INFINITE);
#3
__int64 sumInt(int value)
{
__int64 trcount=0;
for(int i=1000000;i<2000000;i++){
trcount+=i;
}
return trcount;
}
__int64 trcount1 = 0;
__int64 trcount2 = 0;
__int64 trcount3 = 0;
Concurrency::parallel_invoke(
[&trcount1 ] { trcount1 = sumint(1000000); },
[&trcount2 ] { trcount2 = sumint(2000000); },
[&trcount3 ] { trcount3 = sumint(3000000); }
);
__int64 result=trcount1+trcount2+trcount3;
{
__int64 trcount=0;
for(int i=1000000;i<2000000;i++){
trcount+=i;
}
return trcount;
}
__int64 trcount1 = 0;
__int64 trcount2 = 0;
__int64 trcount3 = 0;
Concurrency::parallel_invoke(
[&trcount1 ] { trcount1 = sumint(1000000); },
[&trcount2 ] { trcount2 = sumint(2000000); },
[&trcount3 ] { trcount3 = sumint(3000000); }
);
__int64 result=trcount1+trcount2+trcount3;
#4
请问楼上使用
HANDLE hThread[3];
hThread[0] = AfxBeginThread(MulThreadSearchTrgl1,this);
hThread[1] = AfxBeginThread(MulThreadSearchTrgl2,this);
hThread[2] = AfxBeginThread(MulThreadSearchTrgl3,this);
WaitForMultipleObjects(3,hThread,TRUE,INFINITE);
然而子线程还是没有结束,主线程就退出了,这是怎么回事?请大侠不吝赐教!!
HANDLE hThread[3];
hThread[0] = AfxBeginThread(MulThreadSearchTrgl1,this);
hThread[1] = AfxBeginThread(MulThreadSearchTrgl2,this);
hThread[2] = AfxBeginThread(MulThreadSearchTrgl3,this);
WaitForMultipleObjects(3,hThread,TRUE,INFINITE);
然而子线程还是没有结束,主线程就退出了,这是怎么回事?请大侠不吝赐教!!
#5
迅速围观大牛,顺带问一下,这是神马?
#6
CHexMeshView是窗口类来的吧,会自己退出吗,还是你强行让它退出了
#7
不过,不建议在窗口线程中等待线程结束
#8
CHexMeshView类是Cview的派生类。
CCriticalSection protectaSide;
int CHexMeshView::SearchTrgl(double *x, double *y, double *z, int *crt, int n)
{
CWinThread* pThread;
CWinThread* qThread;
CWinThread* rThread;
pThread=AfxBeginThread(MulThreadSearchTrgl1,this);
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
HANDLE hThread[3];
hThread[0] = pThread;
hThread[1] = qThread;
hThread[2] = rThread;
WaitForMultipleObjects(3,hThread,TRUE,INFINITE) //分步运行时,此行花费时间很短,理论上说,如果要等待三个子线程都运行完的话,此行要等待几分钟。
//Sleep(180000) //如果让主线程沉睡3分钟的话,运行结果正确
return trcount;
}
请问大侠我如何能让WaitForMultipleObjects(3,hThread,TRUE,INFINITE)成功等待
CCriticalSection protectaSide;
int CHexMeshView::SearchTrgl(double *x, double *y, double *z, int *crt, int n)
{
CWinThread* pThread;
CWinThread* qThread;
CWinThread* rThread;
pThread=AfxBeginThread(MulThreadSearchTrgl1,this);
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
HANDLE hThread[3];
hThread[0] = pThread;
hThread[1] = qThread;
hThread[2] = rThread;
WaitForMultipleObjects(3,hThread,TRUE,INFINITE) //分步运行时,此行花费时间很短,理论上说,如果要等待三个子线程都运行完的话,此行要等待几分钟。
//Sleep(180000) //如果让主线程沉睡3分钟的话,运行结果正确
return trcount;
}
请问大侠我如何能让WaitForMultipleObjects(3,hThread,TRUE,INFINITE)成功等待
#9
如果使用互斥量同步的话(代码如下),只开辟2个子线程,在主线程里执行一段与第三个子线程功能一样的代码,这样结果是对的(在此不知为什么临界区同步就不行)。问题是这样代码就不整齐,更重要的是据说临界区速度比互斥量快,因为我的程序在此要运行半个多小时,我想使用临界区提高速度。
CMutex protectaSide;
int CHexMeshView::SearchTrgl(double *x, double *y, double *z, int *crt, int n)
{
CWinThread* pThread;
CWinThread* qThread;
CWinThread* rThread;
pThread=AfxBeginThread(MulThreadSearchTrgl1,this);
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
//rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
for(int i=2000000;i<3000000;i++){ //在主线程中使用代码代替第三个子线程
protectaSide.Lock();
trcount=trcount+i;
protectaSide.Unlock();
}
return trcount;
}
CMutex protectaSide;
int CHexMeshView::SearchTrgl(double *x, double *y, double *z, int *crt, int n)
{
CWinThread* pThread;
CWinThread* qThread;
CWinThread* rThread;
pThread=AfxBeginThread(MulThreadSearchTrgl1,this);
qThread=AfxBeginThread(MulThreadSearchTrgl2,this);
//rThread=AfxBeginThread(MulThreadSearchTrgl3,this); //开辟三个子线程
for(int i=2000000;i<3000000;i++){ //在主线程中使用代码代替第三个子线程
protectaSide.Lock();
trcount=trcount+i;
protectaSide.Unlock();
}
return trcount;
}
#10
望各位大侠不吝赐教!!
#11
CWinThread* pThread[3] = {0};
HANDLE hThread[3] = {0};
pThread[0] = AfxBeginThread(ThreadProc1, (LPVOID)GetSafeHwnd(), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
pThread[1] = AfxBeginThread(ThreadProc2, (LPVOID)GetSafeHwnd(), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
pThread[2] = AfxBeginThread(ThreadProc3, (LPVOID)GetSafeHwnd(), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
for(int i=0; i<sizeof(pThread)/sizeof(pThread[0]); i++)
{
hThread[i] = pThread[i]->m_hThread;
pThread[i]->m_bAutoDelete = FALSE;
pThread[i]->ResumeThread();
}
WaitForMultipleObjects(sizeof(pThread)/sizeof(pThread[0]), hThread, TRUE, INFINITE);
for(int i=0; i<sizeof(pThread)/sizeof(pThread[0]); i++)
{
ASSERT(CloseHandle(pThread[i]->m_hThread));
delete pThread[i];
pThread[i] = NULL;
}