关于API 
CreateMutex(
lpMutexAttributes: PSecurityAttributes;  {a pointer to security attributes}
bInitialOwner: BOOL;  {flag for the initial ownership}
lpName: PChar  {mutex object name}
): THandle; {returns a handle of the mutex object}
的详细帮助你可以参看msdn
我给你个例子:function ThreadFunc0(Info: Pointer): Integer; stdcall
var
  ICount: Integer;        // general loop counter
  CountStr: string;       // holds a string representation of the counter
begin
  {wait for the mutex to become signaled. the mutex is created signaled so
  this thread gets ownership of the mutex and starts immediately}
  WaitForSingleObject(Form1.MutexHandle, INFINITE);  {start a counter to display something}  for ICount := 1 to 10000 do
  begin
    CountStr := IntToStr(ICount);
    Form1.Canvas.TextOut(10, 10, 'Thread 1 '+CountStr);
  end;  {Release ownership of the mutex so the other threads can fire}
  ReleaseMutex(Form1.MutexHandle);
  ExitThread(1);
end;function ThreadFunc1(Info: Pointer): Integer; stdcall
var
  ICount: Integer;        // general loop counter  CountStr: string;       // holds a string representation of the counter
begin
  {wait for the mutex to become signaled. the mutex is created signaled so
  this thread gets ownership of the mutex and starts immediately}
  WaitForSingleObject(Form1.MutexHandle, INFINITE);  {start a counter to display something}
  for ICount := 1 to 10000 do
  begin
    CountStr := IntToStr(ICount);    Form1.Canvas.TextOut(110, 10, 'Thread 2 '+CountStr);
  end;  {Release ownership of the mutex so the other threads can fire}
  ReleaseMutex(Form1.MutexHandle);
  ExitThread(2);
end;

解决方案 »

  1.   

    function ThreadFunc2(Info: Pointer): Integer; stdcall
    var
      ICount: Integer;           // general loop counter
      CountStr: string;          // holds a string representation of the counter
      LocalMutexHandle: THandle; // holds a handle to the mutexbegin
      {open a Handle to the mutex from this thread}
      LocalMutexHandle := OpenMutex(MUTEX_ALL_ACCESS, FALSE, 'MyMutex');  {take ownership of the mutex. this will wait until the mutex is signaled}
      WaitForSingleObject(LocalMutexHandle, INFINITE);  {start a counter to display something}
      for ICount := 1 to 10000 do
      begin
        CountStr := IntToStr(ICount);
        Form1.canvas.TextOut(210, 10, 'Thread 3 '+CountStr);  end;  {Release ownership of the mutex}
      ReleaseMutex(LocalMutexHandle);  {close the mutex handle}
      CloseHandle(LocalMutexHandle);
      ExitThread(3);
    end;procedure TForm1.CreateThreadClick(Sender: TObject);
    var
      ThreadId0, ThreadId1, ThreadId2: Integer; // holds thread identifiers
    begin
      {Create the mutex with the name MyMutex. the mutex is signaled
       so the first thread will start immediately}  MutexHandle := CreateMutex(nil, False, 'MyMutex');  {Create the first thread, and start it immediately}
      ThreadHandle := Windows.CreateThread(nil, 0, @ThreadFunc0, nil, 0, ThreadId0);  {Create the second thread, and start it immediately}
      ThreadHandle1 := Windows.CreateThread(nil,0, @ThreadFunc1, nil, 0, ThreadId1);  {Create the third thread, and start it immediately}
      ThreadHandle2 := Windows.CreateThread(nil,0, @ThreadFunc2, nil, 0, ThreadId2);  {Stop the main thread for a short time so that the other threads get
       a chance to take ownership of the mutex before the main thread
       calls WaitForSingleObject}
      Sleep(1000);  {Take ownership of the mutex; this will wait until the mutex is signaled}
      WaitForSingleObject(MutexHandle, INFINITE);  {Close the mutexHandle so that this will work again}
      CloseHandle(MutexHandle);
    end;
      

  2.   

    谢了!,我明白了。function ThreadFunc0(Info: Pointer): Integer; stdcall
    这个stdcall是什么东西啊,是什么意思啊!!???