●●为何我只能同时运行两个线程

ok1996 2010-06-15 07:11:56
我有三个全局函数:
UINT myfun1 (LPVOID pParam)
{
long sum=0;
CString strTmp;
for(int i = 0;i < 50000;i++)
{
sum+=i;
strTmp.Format( "线程1在运行:%d ",sum);
AfxGetApp()-> GetMainWnd()-> GetDlgItem(IDC_TEXT1) -> SetWindowText(strTmp);
}

return 0;
}


UINT myfun2 (LPVOID pParam)
{
int sum=0;
CString strTmp;
for(int i = 0;i < 50000;i++)
{
sum+=i;
strTmp.Format( "线程2在运行:%d ",sum);
AfxGetApp()-> GetMainWnd()-> GetDlgItem(IDC_TEXT2) -> SetWindowText(strTmp);
}

return 0;
}

UINT myfun3 (LPVOID pParam)
{
int sum=0;
CString strTmp;
for(int i = 0;i < 50000;i++)
{
sum+=i;
strTmp.Format( "线程3在运行:%d ",sum);
AfxGetApp()-> GetMainWnd()-> GetDlgItem(IDC_TEXT3) -> SetWindowText(strTmp);
}

return 0;
}
=====================================================================================
想用三个按钮建立线程来运行上面三个函数
void CMyDlg::OnButton1()
{
// TODO: Add your control notification handler code here
AfxBeginThread(myfun1, (LPVOID)AfxGetApp()-> m_pMainWnd-> GetSafeHwnd(), THREAD_PRIORITY_NORMAL);
}

void CMyDlg::OnButton2()
{
AfxBeginThread(myfun2, (LPVOID)AfxGetApp()-> m_pMainWnd-> GetSafeHwnd(), THREAD_PRIORITY_NORMAL);

}

void CMyDlg::OnButton3()
{
AfxBeginThread(myfun3, (LPVOID)AfxGetApp()-> m_pMainWnd-> GetSafeHwnd(), THREAD_PRIORITY_NORMAL);

}

编译后发觉,最多只能同时运行两个线程, 我先按下按扭1,myfun1执行,,,过了一段时间,再按按钮2, myfun2执行,
我想再按按钮3时发觉阻塞了,不能执行myfun3, 一直到myfun1执行完成之后(myfun2仍在执行中...),myfun3才能开始执行

也就是说,同时只能有两个线程同时工作,这是怎么回事?
...全文
96 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ok1996 2010-06-15
  • 打赏
  • 举报
回复
OK了,是我把Sleep(0)放不对地方,谢谢兄弟了
ok1996 2010-06-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 schlafenhamster 的回复:]
各个线程都加入Sleep(0)
[/Quote]
试了,还是和原来一样结果,不行.
schlafenhamster 2010-06-15
  • 打赏
  • 举报
回复
各个线程都加入Sleep(0)

15,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧