program thread;
uses
  windows;
var   tport:word;
    i,j:word;
    hc,s:handle;
procedure hchc(p:pointer);stdcall;
begin
WaitForSingleObject(hc,INFINITE);
j:=j+1;
writeln(j);
ReleaseMutex(hc);
end;
 begin
  hc:=CreateMutex(nil,true,'aaa');
  for i:=1 to 256 do
  begin
  CreateThread(nil,0,@hchc,@i,0,ThreadID);
  end;
  while j<>256 do
  begin
    end;
  WaitForSingleObject(hc,INFINITE);
  writeln('xxxxxxxxxxx');
  ReleaseMutex(hc);
  CloseHandle(hc);
  readln;
end.
不是每次都能创建256,再多点也不行了,为什么,是不是没设置好

解决方案 »

  1.   


    改一下试试。 if WaitForSingleObject(hc,INFINITE) = WAIT_OBJECT_0 then
    begin
      j:=j+1;
      writeln(j); 
    end;返 回 值                             含   义
    WA I T _ A B A N D O N E D 指定的对象是互斥对象,并且拥有这个互斥对象的线程在没有释放此对象之
    前就已终止。此时就称互斥对象被抛弃。这种情况下,这个互斥对象归当前线
    程所有,并把它设为非发信号状态
    WA I T _ O B J E C T _ 0 指定的对象处于发信号状态
    WA I T _ T I M E O U T 等待的时间已过,对象仍然是非发信号状态
      

  2.   

    谢谢大家,有什么不规范的地方麻烦指正,还有,我发现不同电脑创建的线程数目是不一样,是不是跟cpu有关,那s扫描器问什么能有1024的线程?