Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2006-11

Mutex!

9 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ales Katona
to Amir Aavani

There's no "visible" mutex interface in fpc right now. ATM all non-windows platforms have semaphores in the ThreadManager but the windows ones afaik don't.

I think you can simply use CriticalSection instead of mutex to achieve what you need.

Ales

Reply by email

Amir Aavani
to Ales Katona

Let me explain the problem with more details,

consider the following code:

while true do
begin
  ReadLn (S);
/// Lock Mutex
  AThread:= TMyThread.Create (True);//Suspended
  AThread.Prepare (S);
  AThread.Resume;
///UnLock Mutex
end;
...

To understand the EnterCriticalsection, I write the following code, I was expect to see a single "Here" while a lot of here is printed. Was I incorrect in anywhere?

  InitCriticalSection (cs);
  while true do
  begin
    EnterCriticalsection (cs);
    WriteLn ('Here!');
  end;
    LeaveCriticalsection (cs);

I referred to a document about EnterCriticalsection, It said that It prevent two different thread to access a critical section, but here

Reply by email

Peter Vreman
to Amir Aavani
you wrote 30 lines
Let me explain the problem with more details, consider the following code: while true do begin ReadLn (S); /// Lock Mutex AThread:= TMyThread.Create (True);//Suspended AThread.Prepare (S); AThread.Resume; ///UnLock Mutex end; ... To understand the EnterCriticalsection, I write the following code, I was expect to see a single "Here" while a lot of here is printed. Was I incorrect in anywhere? InitCriticalSection (cs); while true do begin EnterCriticalsection (cs); WriteLn ('Here!'); end; LeaveCriticalsection (cs); I referred to a document about EnterCriticalsection, It said that It prevent two different thread to access a critical section, but here

Criticalsections are like recursive mutexes

Peter

Reply by email

Colin Western
to Amir Aavani: Let me explain the problem with more details, consider the following code: while true do…
Amir Aavani wrote 9 lines
InitCriticalSection (cs); while true do begin EnterCriticalsection (cs); WriteLn ('Here!'); end; LeaveCriticalsection (cs);

Each Enter.. must be matched by a Leave..., and you don't need the loop. The standard way of doing this is:

EnterCriticalsection (cs);
try
   WriteLn ('Here!');
finally
   LeaveCriticalsection (cs);
end;

Reply by email

Michael Müller
to Colin Western
Colin Western wrote 11 lines
Amir Aavani wrote: > InitCriticalSection (cs); > while true do > begin > EnterCriticalsection (cs); > WriteLn ('Here!'); > end; > LeaveCriticalsection (cs); Each Enter.. must be matched by a Leave..., and you don't need the loop. The standard way of doing this is:

I'm sure that this is clear for Amir. But in his example he (and me too) expects that the loop should stop at the second time it calls EnterCriticalsection() because the old critical section wasn't left.

Show quoted text 6 lines
EnterCriticalsection (cs); try WriteLn ('Here!'); finally LeaveCriticalsection (cs); end;

But this is not an example to show how a critical section stops another one.

Regards

Michael

Reply by email

Amir Avani
to Michael Müller

Thanks Michael, you are right!
I want to know how should I use Criticalsections to make my program to print only one "here".

Michael Müller wrote 13 lines
Am 05.11.2006 um 12:14 schrieb Colin Western: > Amir Aavani wrote: >> InitCriticalSection (cs); >> while true do >> begin >> EnterCriticalsection (cs); >> WriteLn ('Here!'); >> end; >> LeaveCriticalsection (cs); > > Each Enter.. must be matched by a Leave..., and you don't need the

loop. The standard way of doing this is:

I'm sure that this is clear for Amir. But in his example he (and me too)

expects that the loop should stop at the second time it calls EnterCriticalsection() because the old critical section wasn't left.

Show quoted text 18 lines
> EnterCriticalsection (cs); > try > WriteLn ('Here!'); > finally > LeaveCriticalsection (cs); > end; But this is not an example to show how a critical section stops another one. Regards Michael _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Vinzent Höfler
to Amir Avani
Amir Avani wrote
Thanks Michael, you are right! I want to know how should I use Criticalsections to make my program to print only one "here".

By not using them at all?

count := 0;
while true do
begin
    if count = 0
       WriteLn ('Here!');
    count := 1;
end {while};

Critical sections don't make sense in a non-threading context.

Vinzent.

Reply by email

Amir Aavani
to Vinzent Höfler

What you suggested is a kind of busy waiting. But I want a suspension. In a message , i described my main problem which was a while like this.

while true do
begin
 ReadLn (S);
/// Lock Mutex
 AThread:= TMyThread.Create (True);//Suspended
 AThread.Prepare (S);
 AThread.Resume;
///UnLock Mutex
end;

And I told to check if Criticalsession could work in my situation i write that sample code.

Any idea for my problem?

Vinzent Höfler wrote 22 lines
Amir Avani wrote: > Thanks Michael, you are right! > I want to know how should I use Criticalsections to make my program to > print only one "here". By not using them at all? count := 0; while true do begin if count = 0 WriteLn ('Here!'); count := 1; end {while}; Critical sections don't make sense in a non-threading context. Vinzent.

Reply by email