summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-base/examples/crittest.pp
blob: ceb8f00cf9cbae32f001d95a91b84bb1f5bf1ac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
program crittest;
// originally a test to test .tryenter.
// A thread holds a lock for 5sec, while the main thread tries to lock
// it.  

{$mode Delphi}

Uses {$ifdef unix}cthreads,{$endif} syncobjs,sysutils,classes;

type TTestthread = class(tthread)
	    	     procedure execute; override;
                    end;

var crit : TCriticalSection;

procedure TTestThread.Execute;

begin
 crit.acquire;
 sleep(5000);
 crit.release;
end;


var thr : TTestthread;  
    I : integer;

begin
 crit:=TCriticalsection.create;
 thr :=TTestthread.Create(false);

 sleep(500);  // give thread time to start.

 writeln('tryenter');
 
 i:=0;
 while not(crit.tryenter) do
  begin
    writeln('tryenter attempt ',i);
    inc(i);
    sleep(100);
  end;
 writeln('lock acquired in mainthread!');
 writeln('no payload, so releasing');
 crit.release;
 thr.waitfor;
end.