我在侯捷翻译的WIN32多线程程序设计一书中,遇到如下情况
#include <iostream.h>
#include <afxwin.h>UINT ThreadFunc(LPVOID n)
{
int i = (int)n;
for(int j = 0; j < 8; j++)
{
cout<<i<<i<<i<<i<<i<<i<<i<<i<<endl;
}
Sleep(3000); //1
return 0;
}void main()
{
CWinThread* thread[5];
for(int i = 0; i < 5; i++)
{
if( AfxBeginThread(ThreadFunc, (LPVOID)i) )
cout<<"Thread "<<i<<" launched"<<endl; }
}
运行结果如下:
Thread 0 launched
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
Thread 1 launched
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
Thread 2 launched
22222222
22222222
22222222
22222222
22222222
22222222
22222222
22222222
Thread 3 launched
33333333
33333333
33333333
33333333
33333333
33333333
33333333
33333333
Thread 4 launched
44444444
44444444
44444444
44444444
44444444
44444444
44444444
44444444
但是当我把函数中的Sleep前提,
UINT ThreadFunc(LPVOID n)
{
cout<<"ThreadFunc is called"<<endl; //2
int i = (int)n; Sleep(3000); //1
for(int j = 0; j < 8; j++)
{
cout<<i<<i<<i<<i<<i<<i<<i<<i<<endl;
}
return 0;
}
执行结果就变为了
Thread 0 launched
ThreadFunc is called
Thread 1 launched
ThreadFunc is called
Thread 2 launched
ThreadFunc is called
Thread 3 launched
ThreadFunc is called
Thread 4 launched
ThreadFunc is called没有了数字的打印,这是为什么啊,而且该多线程程序为什么没有出现书上所描述的 race condition?
#include <iostream.h>
#include <afxwin.h>UINT ThreadFunc(LPVOID n)
{
int i = (int)n;
for(int j = 0; j < 8; j++)
{
cout<<i<<i<<i<<i<<i<<i<<i<<i<<endl;
}
Sleep(3000); //1
return 0;
}void main()
{
CWinThread* thread[5];
for(int i = 0; i < 5; i++)
{
if( AfxBeginThread(ThreadFunc, (LPVOID)i) )
cout<<"Thread "<<i<<" launched"<<endl; }
}
运行结果如下:
Thread 0 launched
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
Thread 1 launched
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
Thread 2 launched
22222222
22222222
22222222
22222222
22222222
22222222
22222222
22222222
Thread 3 launched
33333333
33333333
33333333
33333333
33333333
33333333
33333333
33333333
Thread 4 launched
44444444
44444444
44444444
44444444
44444444
44444444
44444444
44444444
但是当我把函数中的Sleep前提,
UINT ThreadFunc(LPVOID n)
{
cout<<"ThreadFunc is called"<<endl; //2
int i = (int)n; Sleep(3000); //1
for(int j = 0; j < 8; j++)
{
cout<<i<<i<<i<<i<<i<<i<<i<<i<<endl;
}
return 0;
}
执行结果就变为了
Thread 0 launched
ThreadFunc is called
Thread 1 launched
ThreadFunc is called
Thread 2 launched
ThreadFunc is called
Thread 3 launched
ThreadFunc is called
Thread 4 launched
ThreadFunc is called没有了数字的打印,这是为什么啊,而且该多线程程序为什么没有出现书上所描述的 race condition?
解决方案 »
- 简单的通信程序,客户端提示连接失败。
- winpcap3.0和winpcap4.0的问题!
- 如何将ComboBox的选择项传递到List中去
- 在控件microsoft web browser的编辑模式下如何即时获取当前所选对象的属性
- 救命!!!!
- 《全面掌握Microsoft Visual C++ 6.0 MFC 应用程序开发》的问题
- tabCtrl控件PostMessage(TCN_SELCHANGE,WPARAM,LPARAM)如何设定WPARAM、LPARAM?
- 对话框最大/最小化是响应什么消息?
- 这个问题实在搞不明白
- MFC 串口得到的数据 怎么显示在listctrl 上
- SetWindowLong(hwnd,0,0);中第二个参数中的0代表什么?
- 如何求过已知点(点的个数大于等于3)的曲线?
试试
void main()
{
CWinThread* thread[5];
for(int i = 0; i < 5; i++)
{
if( AfxBeginThread(ThreadFunc, (LPVOID)i) )
cout<<"Thread "<<i<<" launched"<<endl; }
Sleep(20000);
}
#include <conio.h>
void main()
{
CWinThread* thread[5];
for(int i = 0; i < 5; i++)
{
if( AfxBeginThread(ThreadFunc, (LPVOID)i) )
cout<<"Thread "<<i<<" launched"<<endl; }
getch();//输出结束后按任意键
}
在含超线程技术或者多CPU的机器上可以看到race condition
VOID Sleep(
DWORD dwMilliseconds // sleep time in milliseconds
);
Parameters
dwMilliseconds Specifies the time, in milliseconds, for which to suspend execution. A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. A value of INFINITE causes an infinite delay.