| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1429 人关注过本帖
标题:关于互斥对象 线程同步的问题
只看楼主 加入收藏
夜神月
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2007-3-11
收藏
 问题点数:0 回复次数:2 
关于互斥对象 线程同步的问题
学习多线程的时候看到了这样一段代码!
///////////////////////////////////////////////////////////////////////////////////////
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

  end;

var
  Form1: TForm1;
  MutexHandle: THandle;
  ThreadHandle, ThreadHandle1, ThreadHandle2: THandle;
implementation

{$R *.DFM}

function ThreadFunc1(Info: Pointer): Integer; stdcall;
var
  ICount: Integer;
  CountStr: string;
begin
  WaitForSingleObject(MutexHandle, INFINITE);
  for ICount := 1 to 100000 do
  begin
    CountStr := IntToStr(ICount);
    Form1.Canvas.TextOut(10, 10, 'Thread 1 ' + CountStr);
  end;
  ReleaseMutex(MutexHandle);
  ExitThread(1);
end;

function ThreadFunc2(Info: Pointer): Integer; stdcall;
var
  ICount: Integer;
  CountStr: string;
begin
  WaitForSingleObject(MutexHandle, INFINITE);
  for ICount := 1 to 100000 do
  begin
    CountStr := IntToStr(ICount);
    Form1.Canvas.TextOut(160, 10, 'Thread 2 ' + CountStr);
  end;
  ReleaseMutex(MutexHandle);
  ExitThread(2);
end;

function ThreadFunc3(Info: Pointer): Integer; stdcall;
var
  ICount: Integer;
  CountStr: string;
  LocalMutexHandle: THandle;
begin
  LocalMutexHandle := OpenMutex(MUTEX_ALL_ACCESS, FALSE, 'MyMutex');
  WaitForSingleObject(LocalMutexHandle, INFINITE);
  for ICount := 1 to 100000 do
  begin
    CountStr := IntToStr(ICount);
    Form1.canvas.TextOut(310, 10, 'Thread 3 ' + CountStr);
  end;
  ReleaseMutex(LocalMutexHandle);
  CloseHandle(LocalMutexHandle);
  ExitThread(3);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ThreadId1, ThreadId2, ThreadId3: DWORD;
begin
  if MutexHandle<>DWORD(-1) then
     CloseHandle(MutexHandle);
  MutexHandle := CreateMutex(nil, False, 'MyMutex');
  ThreadHandle := CreateThread(nil, 0, @ThreadFunc1, nil, 0, ThreadId1);
  ThreadHandle1 := CreateThread(nil, 0, @ThreadFunc2, nil, 0, ThreadId2);
  ThreadHandle2 := CreateThread(nil, 0, @ThreadFunc3, nil, 0, ThreadId3);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   MutexHandle:=DWORD(-1);
end;

end.
///////////////////////////////////////////////////////////////////////////////////////////



 就编译运行了下!令我白死不的其解!运行结果很有随即性! 我多次运行有以下几种情况:
1.点击运行按钮 但是并没有反映;
2.点击后直接程序错误;
3.运行 线程1 线程2 线程3(都运行完就是都显示到100000)(我认为应该的情况);
4.只线程1(并没有显示到100000);
5.运行完线程1后 直接显示错误;
 后面还有几种 真是饶人啊!
有哪个高手可以帮我解释下! 将下那个互斥对象的工作原理  希望详细点啊!
搜索更多相关主题的帖子: 线程 对象 Sender procedure declarations 
2008-02-10 23:27
zhou
Rank: 1
等 级:禁止发言
帖 子:429
专家分:0
注 册:2006-6-16
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2008-03-30 17:44
makebest
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:3
帖 子:658
专家分:962
注 册:2005-3-17
收藏
得分:0 
线程
把你的程序改了一下, 这样更能看出各个线程各自的运行情况, 希望对你有所启发:
(窗口上放一个按钮和备注)
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  MutexHandle: THandle;
  ThreadHandle, ThreadHandle1, ThreadHandle2: THandle;
  t1,t2,t3 : string;
const max = 10;
implementation

{$R *.DFM}

function ThreadFunc1(Info: Pointer): Integer; stdcall;
var
  ICount: Integer;
  CountStr: string;
begin
  for ICount := 1 to max do
  begin
    WaitForSingleObject(MutexHandle, INFINITE);
    CountStr := IntToStr(ICount);
    //Form1.Canvas.TextOut(10, 10, 'Thread 1 ' + CountStr);
    t1 := 'Thread 1 ' + CountStr;
    form1.Memo1.Lines.Add(t1);
    ReleaseMutex(MutexHandle);
  end;
  ExitThread(1);
end;

function ThreadFunc2(Info: Pointer): Integer; stdcall;
var
  ICount: Integer;
  CountStr: string;
begin
  for ICount := 1 to max do
  begin
    WaitForSingleObject(MutexHandle, INFINITE);
    CountStr := IntToStr(ICount);
    //Form1.Canvas.TextOut(160, 10, 'Thread 2 ' + CountStr);
    t2 := 'Thread 2 ' + CountStr;
    form1.Memo1.Lines.Add(t2);
    ReleaseMutex(MutexHandle);
  end;
  ExitThread(2);
end;

function ThreadFunc3(Info: Pointer): Integer; stdcall;
var
  ICount: Integer;
  CountStr: string;
  LocalMutexHandle: THandle;
begin
  LocalMutexHandle := OpenMutex(MUTEX_ALL_ACCESS, FALSE, 'MyMutex');
  for ICount := 1 to max do
  begin
    WaitForSingleObject(LocalMutexHandle, INFINITE);
    CountStr := IntToStr(ICount);
    //Form1.canvas.TextOut(310, 10, 'Thread 3 ' + CountStr);
    t3 := 'Thread 3 ' + CountStr;
    form1.Memo1.Lines.Add(t3);
    ReleaseMutex(LocalMutexHandle);
  end;
  CloseHandle(LocalMutexHandle);
  ExitThread(3);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ThreadId1, ThreadId2, ThreadId3: DWORD;
begin
  if MutexHandle<>DWORD(-1) then
     CloseHandle(MutexHandle);
  MutexHandle := CreateMutex(nil, False, 'MyMutex');
  ThreadHandle := CreateThread(nil, 0, @ThreadFunc1, nil, 0, ThreadId1);
  ThreadHandle1 := CreateThread(nil, 0, @ThreadFunc2, nil, 0, ThreadId2);
  ThreadHandle2 := CreateThread(nil, 0, @ThreadFunc3, nil, 0, ThreadId3);
end;

end.
2008-04-01 16:18
快速回复:关于互斥对象 线程同步的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022150 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved