当前位置:编程学习 > 网站相关 >>

多线程实现厕所排号

场景如下:
公司同事排队上厕所, 如果厕所有空位, 优先考虑领导使用, 然后才是普通员工按排队顺序使用
 
---------------------------------------------------------------------------------------------------------------------------
 
单元文件
 
[delphi]  
unit utThreadPool;  
  
inte易做图ce  
  
uses  
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  Dialogs, StdCtrls, ComCtrls;  
  
type  
  TForm_CatchWC = class(TForm)  
    lvWC: TListView;  
    lvEmploye: TListView;  
    btnAddWC: TButton;  
    btnAddEmploye: TButton;  
    btnAddM: TButton;  
    btnStart: TButton;  
    procedure FormCreate(Sender: TObject);  
    procedure btnStartClick(Sender: TObject);  
    procedure btnAddWCClick(Sender: TObject);  
    procedure btnAddEmployeClick(Sender: TObject);  
    procedure btnAddMClick(Sender: TObject);  
  private  
    { Private declarations }  
    hMutex : THandle;  //互斥句柄  
    WCArr : array of dword;  //厕所  
    myMsg : dword;      //自定义消息  
    FActived : boolean;   //是否开抢  
    WC, Employe : integer;  //当前厕所与员工的匹配情况  
    //添加员工  
    procedure AddEmploye(const ARoleName: string);  
  public  
    { Public declarations }  
  end;  
  
var  
  Form_CatchWC: TForm_CatchWC;  
  
implementation  
  
{$R *.dfm}  
//使用WC  每人限用50秒  
function UseWC(index : integer): integer; stdcall;  
var  
  i, vWC, vEmploye : integer;  
  msg : TMsg;  
begin  
  while GetMessage(msg, 0, 0, 0) do  
  begin  
    if msg.message = Form_CatchWC.myMsg then  
    begin  
      vEmploye := Form_CatchWC.Employe;  
      vWC := Form_CatchWC.WC;  
  
      for i := 1 to 50 do  
      begin  
        sleep(200);  
        WaitForSingleObject(Form_CatchWC.hMutex,INFINITE);  
        Form_CatchWC.lvEmploye.Items.Item[vEmploye].SubItems[2] := inttostr(i);  
        ReleaseMutex(Form_CatchWC.hMutex);  
      end;  
      Form_CatchWC.lvWC.Items[vWC].SubItems[0] := '空闲';  
    end;  
  end;  
end;  
//分配WC  优先经理M  
function AssignWC(index : integer): integer; stdcall;  
var  
  i, j : integer;  
  JobFound : boolean;  
begin  
  while true do  
  begin  
    sleep(500);  
    if Form_CatchWC.FActived then  
    begin  
      JobFound := False;  
      for i := 0 to Form_CatchWC.lvWC.Items.Count - 1 do  
      begin  
        if Form_CatchWC.lvWC.Items[i].SubItems[0] = '空闲' then  
        begin  
        //经理优先  
          for j := 0 to Form_CatchWC.lvEmploye.Items.Count - 1 do  
          begin  
            if (Form_CatchWC.lvEmploye.Items[j].SubItems[0] = 'M') and (Form_CatchWC.lvEmploye.Items[j].SubItems[1] = '等待') then  
            begin  
              //可以如厕时, WC牌号和员工牌号  
              Form_CatchWC.WC := i;  
              Form_CatchWC.Employe := j;  
              //调整员工, WC状态  
              Form_CatchWC.lvWC.Items.Item[i].SubItems[0] := '使用';  
              Form_CatchWC.lvEmploye.Items.Item[j].SubItems[1] := '入厕';  
              //给线程发送消息, 告知如厕  
              PostThreadMessage(Form_CatchWC.WCArr[i], Form_CatchWC.myMsg, 0, 0);  
              //当前厕所已占用, 只能找其他厕所了  
              JobFound := True;  
              break;  
            end;  
          end;  
  
          if JobFound then Break;  
          //经理先蹲, 然后员工  
          for j := 0 to Form_CatchWC.lvEmploye.Items.Count - 1 do  
          begin  
            if (Form_CatchWC.lvEmploye.Items[j].SubItems[0] = 'E') and (Form_CatchWC.lvEmploye.Items[j].SubItems[1] = '等待') then  
            begin  
              Form_CatchWC.WC := i;  
              Form_CatchWC.Employe := j;  
  
              Form_CatchWC.lvWC.Items.Item[i].SubItems[0] := '使用';  
              Form_CatchWC.lvEmploye.Items.Item[j].SubItems[1] := '入厕';  
              PostThreadMessage(Form_CatchWC.WCArr[i], Form_CatchWC.myMsg, 0, 0);  
              JobFound := True;  
              break;  
            end;  
          end;  
        end;  
        if JobFound then break;  
      end;  
    end;  
  end;  
end;  
  
procedure TForm_CatchWC.AddEmploye(const
补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,