不解

HellowKitty 2008-03-06 03:12:47

DWORD WINAPI Proc(LPVOID param)
{
DWORD port = (DWORD)param;
//
Sleep(100);
//
cout<<port<<endl;
return 0;

}

int main(int argc,char*argv)
{
HANDLE threads[100]={0};
DWORD id[100] = {0};
for (int i=0;i<100;i++)
{
threads[i] = CreateThread(0,0,Proc,(LPVOID)(i+1),0,id+i);
}

WaitForMultipleObjects(100,threads,TRUE,INFINITE);
return EXIT_SUCCESS;
}

...全文
131 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnzdgs 2008-03-08
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 cnzdgs 的回复:]
有两点问题:
1、WaitForMultipleObjects的64限制,改成20应该可以,估计是你没改对,不妨再试一次。
2、多线程同时使用共享的控制台会有冲突,最好采用线程同步。
[/Quote]
第1点已经证实了。
第2点,很多东西是没有权威说明的,主要靠分析判断加上做程序验证。你试一下,用64,不加同步,多执行几次,注意观察输出的数据就会看到问题了。
HellowKitty 2008-03-08
  • 打赏
  • 举报
回复
不好意思是我自己的问题,for循环里i只到10,WaitForMultipleObjects(20,threads,TRUE,INFINITE);
的原因。
yxz_lp 2008-03-08
  • 打赏
  • 举报
回复

//更改后的代码,测试没有问题
#include "stdafx.h"
#include "Windows.h"
#include "iostream"
using namespace std;

DWORD WINAPI Proc(LPVOID param)
{
DWORD port = (DWORD)param;
//
Sleep(100);
//
cout<<port<<endl;
return 0;

}

int main(int argc,char*argv)
{
HANDLE threads[64]={0};
DWORD id[100] = {0};
int i;
for (i=0;i<64;i++)
{
threads[i] = CreateThread( NULL, 0, Proc, ( LPVOID )( i + 1 ), 0, id + i);
}

WaitForMultipleObjects(64,threads,TRUE,INFINITE);
return 0;
}

还有10楼说的不无道理,多线程共用一个控制台,可能是会有问题,测试中 ,有几个数字输出到一行的情况,建议同步。
HellowKitty 2008-03-08
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 cnzdgs 的回复:]
有两点问题:
1、WaitForMultipleObjects的64限制,改成20应该可以,估计是你没改对,不妨再试一次。
2、多线程同时使用共享的控制台会有冲突,最好采用线程同步。
[/Quote]
第一点在你说之前我早就试过了,关键之处根本不是这个问题。
第二点你什么依据没,比如说msdn之类的比较权威的。我代码中有加同步的,贴上来的代码去掉了而已。而且也不是库的原因,我用了多线程库和不用效果一样。
希望指出问题原因,并提供解决方法,马上结贴给分。
cnzdgs 2008-03-07
  • 打赏
  • 举报
回复
有两点问题:
1、WaitForMultipleObjects的64限制,改成20应该可以,估计是你没改对,不妨再试一次。
2、多线程同时使用共享的控制台会有冲突,最好采用线程同步。
用户 昵称 2008-03-07
  • 打赏
  • 举报
回复
俺将线程中的sleep放在cout后面就正常点了。
DWORD WINAPI Proc(LPVOID param)
{
DWORD port = (DWORD)param;
//
//
cout<<port<<endl;
Sleep(100);
return 0;

}
用户 昵称 2008-03-07
  • 打赏
  • 举报
回复
奇怪,debug和执行不一样。
用户 昵称 2008-03-07
  • 打赏
  • 举报
回复
// test7.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Windows.h"
#include "iostream.h"

DWORD WINAPI Proc(LPVOID param)
{
DWORD port = (DWORD)param;
//
Sleep(100);
//
cout<<port<<endl;
return 0;

}

int main(int argc,char*argv)
{
HANDLE threads[100]={0};
DWORD id[100] = {0};
int i;
for (i=0;i<100;i++)
{
threads[i] = CreateThread( NULL, 0, Proc, ( LPVOID )( i + 1 ), 0, id + i);
}

WaitForMultipleObjects(100,threads,TRUE,INFINITE);
return 0;
}
HellowKitty 2008-03-07
  • 打赏
  • 举报
回复
HellowKitty 2008-03-06
  • 打赏
  • 举报
回复
help!
anyboy knows???
HellowKitty 2008-03-06
  • 打赏
  • 举报
回复
我本来以为是
WaitForMultipleObjects(100,threads,TRUE,INFINITE);
的句柄大于64的缘故,试着把但是改成20还是不行,不过改成十却 可以,不解
HellowKitty 2008-03-06
  • 打赏
  • 举报
回复
和这个没关系把
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes
DWORD dwStackSize, // initial thread stack size
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
LPVOID lpParameter, // argument for new thread
DWORD dwCreationFlags, // creation flags
LPDWORD lpThreadId // pointer to receive thread ID
);

dwCreationFlags
Specifies additional flags that control the creation of the thread. If the CREATE_SUSPENDED flag is specified, the thread is created in a suspended state, and will not run until the ResumeThread function is called. If this value is zero, the thread runs immediately after creation. At this time, no other values are supported.
wawaku 2008-03-06
  • 打赏
  • 举报
回复
for (int i=0;i<100;i++)
{
threads[i] = CreateThread(0,0,Proc,(LPVOID)(i+1),0,id+i);
ResumeThread(threads[i]);
}

HellowKitty 2008-03-06
  • 打赏
  • 举报
回复
怎么线程里的
cout<<port<<endl;
没打印东西

15,466

社区成员

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

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