在多线程中怎样创建和使用互斥量、信号量(delphi)(100分)

  • 主题发起人 主题发起人 yanghai0437
  • 开始时间 开始时间
Y

yanghai0437

Unregistered / Unconfirmed
GUEST, unregistred user!
在多线程中怎样创建和使用互斥量、信号量(delphi)<br>我的程序中创建了n个线程,但是他们都需要读写用一个端口,但是如果<br>不加以控制就会出错,我想创建互斥量来解决(我现在用了一个全局bool变量来解决此问题,但是我担心会出问题)这问题,但是我不会,请高手指点。谢谢!!
 
贴一篇 互斥的源程序<br>unit MutexFrm;<br>interface<br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br>type<br>&nbsp; //定义一个线程类<br>&nbsp; TsyncThread = class(TThread)<br>&nbsp; protected<br>&nbsp; &nbsp; procedure execute; override;<br>&nbsp; end;<br>&nbsp; TfrmMutex = class(TForm)<br>&nbsp; &nbsp; ListBox1: TListBox;<br>&nbsp; &nbsp; btnExecute: TButton;<br>&nbsp; &nbsp; ListBox2: TListBox;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; procedure btnExecuteClick(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormDestroy(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; //定义线程实例变量<br>&nbsp; &nbsp; Thread1, Thread2: TSyncThread;<br>&nbsp; &nbsp; //自定义过程,在线程结束时调用<br>&nbsp; &nbsp; procedure Thread1Done(sender: TObject);<br>&nbsp; &nbsp; procedure Thread2Done(sender: TObject);<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br>const<br>&nbsp; MaxSize = 100;<br>var<br>&nbsp; frmMutex: TfrmMutex;<br>&nbsp; //数据全局区<br>&nbsp; GlobalData: array[1..MaxSize] of integer;<br>&nbsp; IncNum: Integer = 0;<br>&nbsp; //定义互斥对象句柄<br>&nbsp; HMutex: THandle = 0;<br>implementation<br>{$R *.dfm}<br>//线程执行主体<br>procedure TSyncThread.execute;<br>var<br>&nbsp; i: integer;<br>&nbsp; WaitReturn: DWORD;<br>begin<br>&nbsp; FreeOnTerminate := True;<br>&nbsp; //进入同步,防止其他线程访问<br>&nbsp; WaitReturn := WaitForSingleObject(HMutex, INFINITE);<br>&nbsp; if WaitReturn = Wait_Object_0 then<br>&nbsp; &nbsp; for i := 1 to Maxsize do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Inc(IncNum);<br>&nbsp; &nbsp; &nbsp; GlobalData := IncNum;<br>&nbsp; &nbsp; &nbsp; Sleep(10);<br>&nbsp; &nbsp; end;<br>end;<br>//在窗体创建时,创建互斥量<br>procedure TfrmMutex.FormCreate(Sender: TObject);<br>begin<br>&nbsp; HMutex := CreateMutex(nil, false, nil);<br>end;<br>//在窗体释放时,释放互斥量<br>procedure TfrmMutex.FormDestroy(Sender: TObject);<br>begin<br>&nbsp; CloseHandle(HMutex);<br>end;<br>//创建线程实例<br>procedure TfrmMutex.btnExecuteClick(Sender: TObject);<br>begin<br>&nbsp; Thread1 := TSyncThread.Create(false);<br>&nbsp; Thread1.OnTerminate := Thread1Done;<br>&nbsp; Thread2 := TSyncThread.Create(false);<br>&nbsp; Thread2.OnTerminate := Thread2Done;<br>end;<br>//当线程1执行完后,将GlobalData数据显示出来<br>procedure TfrmMutex.Thread1Done(sender: TObject);<br>var<br>&nbsp; i: integer;<br>begin<br>&nbsp; ListBox1.Items.Clear;<br>&nbsp; for i := 1 to MaxSize do<br>&nbsp; &nbsp; Listbox1.Items.Add(intToStr(GlobalData));<br>&nbsp; //解除互斥,让其他线程可以访问<br>&nbsp; ReleaseMutex(HMutex);<br>end;<br>//当线程2执行完后,将GlobalData数据显示出来<br>procedure TfrmMutex.Thread2Done(sender: TObject);<br>var<br>&nbsp; i: integer;<br>begin<br>&nbsp; ListBox2.Items.Clear;<br>&nbsp; for i := 1 to MaxSize do<br>&nbsp; &nbsp; Listbox2.Items.Add(intToStr(GlobalData));<br>&nbsp; //解除互斥,让其他线程可以访问<br>&nbsp; ReleaseMutex(HMutex);<br>end;<br>end.
 
谢谢,我要试试
 
Delphi的SynObjs.pas中有现成的封装好的对象:)
 
to shenloqi:<br>&nbsp; 有使用的例子吗?
 
《Delphi5开发人员指南》中有详细的讲解及范例!
 
to shenloqi:<br>&nbsp; &nbsp;我定义了gSynchroObject:TSynchroObject;<br>然后创建了4个线程,创建的顺序是threads1,threads2,threads3,threads4<br>&nbsp; 在线程中这样写:<br>&nbsp; &nbsp;gSynchroObject.Acquire ;<br>// &nbsp; 每个线程都向同一个文件中写字符串,这个串每个线程不一样'threads4',<br>&nbsp; &nbsp;WsWriteLog('test.txt','threads4',true);<br>&nbsp; &nbsp;gSynchroObject.Release ;<br>&nbsp; &nbsp;Application.ProcessMessages ;<br>//但是当停止所有线程后发现,在文件的开头有threads1,threads2,threads3,threads4<br>可到后面就只有threads4的内容了,我试过将threads3放在最后创建,则文件的后面就是threads3的内容。<br><br>&nbsp; 我希望所有的线程均匀得到权限,该怎样做呢?
 
to takashiki:<br>&nbsp; &nbsp;我用你的方法测试,开始同样创建了4个线程,开始同时运行4个线程,<br>隔一段时间后同时停止,发觉4个线程得到的机会差不多,但是当再次同时<br>启动线程,隔一段时间后同时停止,发现只有1个线程得到了机会,其他3个<br>线程志得到了很少的机会,不知道为什么,他们的级别是一样的
 
多人接受答案了。
 
后退
顶部