Recent

Author Topic: Help with CriticalSection needed  (Read 15771 times)

beli0135

  • New Member
  • *
  • Posts: 47
Help with CriticalSection needed
« on: August 29, 2006, 08:48:13 pm »
Hi!

I am having trouble with CriticalSection. Following code (uncommented) does not work in any case.
Can someone help me with this?

I just want to enter critical section, delete item from the list and exit critical section

Code: [Select]

function TProcessThread.GetFileFromList: string;
var
   cs:TRTLCriticalSection;
begin
(*
   InitCriticalSection(cs);
   {$IFDEF windows}
     EnterCriticalSection(cs.LockCount);
   {$ELSE}
     EnterCriticalSection(cs.__m_count);
   {$ENDIF}
 *)
   result:=FileList.strings[0];
   FileList.Delete(0);

 (*
   {$IFDEF windows}
     LeaveCriticalSection(cs.LockCount);
   {$ELSE}
     LeaveCriticalSection(cs.__m_count);
   {$ENDIF}
   DoneCriticalsection(cs);
  *)
end;


Thanks!
Regards,
Emil Beli

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: Help with CriticalSection needed
« Reply #1 on: August 30, 2006, 10:14:46 am »
It's easier to use TCriticalSection in unit SyncObjs.

Create the CS in the initialization section and free it in finalization.

Then simply use CS.Acquire and CS.Release.

beli0135

  • New Member
  • *
  • Posts: 47
RE: Re: Help with CriticalSection needed
« Reply #2 on: August 30, 2006, 02:45:56 pm »
I will try, thanks.
However, I cannot use it initialization, it must be local, just to perform one operation.
Regards,
Emil Beli

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: RE: Re: Help with CriticalSection needed
« Reply #3 on: August 30, 2006, 04:58:49 pm »
Quote from: "beli0135"

However, I cannot use it initialization, it must be local, just to perform one operation.


Are you sure? Maybe I don't understand your example, but in my understanding, a CS which is local to a thread method does not much to prevent other threads from accessing anything.

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: RE: Re: Help with CriticalSection needed
« Reply #4 on: August 30, 2006, 04:59:10 pm »
Quote from: "beli0135"
I will try, thanks.
However, I cannot use it initialization, it must be local, just to perform one operation.


This is conceptually wrong. What are critical section created for? They are like traffic lights that make a process stop until another finishes a operation, so they don´t cross the same place at the same time.

A local critical section is like a traffic light in a street without any crosses. And also one that is always green (because noone is passing anyway).

So, the critical section must be accessible globally on your program.

If you create a local Critical section, it doesn´t matter how many times that same function is called by various different processes. Each instance of the function will have a different critical section, and thus they will never know that another instance of the same function is also accessing that memory. So that critical section code you put there is useless, a waste of CPU time.

Put in short words: Local critical sections are useless. Critical sections must be glocal.

This makes sense to you? I think my explanation isn´t very clear, but I hope it helps a little.

beli0135

  • New Member
  • *
  • Posts: 47
RE: Re: RE: Re: Help with CriticalSection needed
« Reply #5 on: August 30, 2006, 11:42:52 pm »
@Sekel,

Sorry to say, but you are completely wrong. Well, actually, I really do not know how lazarus is working with it. As far as Delphi is concerned, if you have a global TList and you want to mess with it from thread, you need to lock it ONLY when thread access it, to prevent colission (especially if you are deleting from the list).

In Delphi or C++, when you create critical section, no matter from where and lock it, it is global. Otherwise doesn't make sense.
If lazarus does in different way, then it is a bug.

@Theo
Thanks, worked as expected.
Regards,
Emil Beli

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: RE: Re: RE: Re: Help with CriticalSection needed
« Reply #6 on: August 31, 2006, 12:50:11 am »
Quote from: "beli0135"
In Delphi or C++, when you create critical section, no matter from where and lock it, it is global.


The problem is not where you lock it, but where you initialize it. Take a look at your original code again. You are initializing the critical section *inside* your procedure. The code to initialize the critical section should be outside the function, such as when you put it on the initialization part of the unit.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: RE: Re: RE: Re: Help with CriticalSection needed
« Reply #7 on: August 31, 2006, 01:41:13 am »
Quote from: "beli0135"

@Theo
Thanks, worked as expected.


You're welcome. However Sekel is absolutely right.
Do it as I wrote in my first answer (create in initialization section) or you won't have fun. ;-)

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: RE: Re: Help with CriticalSection needed
« Reply #8 on: August 31, 2006, 02:35:12 am »
Quote from: "beli0135"
I cannot use it initialization, it must be local, just to perform one operation.


If you don't want to put in initialization, put it in another global place, such as initializing the critical section on the constructor of the Thread class, and deinitialize on the destructor. In this case the critical section would be a field on the Thread class.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: RE: Re: Help with CriticalSection needed
« Reply #9 on: August 31, 2006, 09:59:59 am »
Quote from: "sekel"

 In this case the critical section would be a field on the Thread class.


Now I have a problem understanding what you mean here.
If the CS is a field of the thread class, then every instance of this class (thread) has its own CS and does nothing to prevent other threads from accessing the same code block / memory.
Or did I misunderstand you?

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: RE: Re: Help with CriticalSection needed
« Reply #10 on: August 31, 2006, 12:56:30 pm »
Quote from: "theo"
Now I have a problem understanding what you mean here.
If the CS is a field of the thread class, then every instance of this class (thread) has its own CS and does nothing to prevent other threads from accessing the same code block / memory.
Or did I misunderstand you?


Yes, you are right. I posted that very late at night, so I made a mistake =)

The CS cannot be a field on the Thread class.

beli0135

  • New Member
  • *
  • Posts: 47
RE: Re: RE: Re: Help with CriticalSection needed
« Reply #11 on: August 31, 2006, 06:14:28 pm »
Ah sorry, misunderstanding. That piece of code was just the test.
Surely I initialize critical section outside the thread.
What I thought that you said is to acquire section outside.

Anyways, in Delphi, even if it is initialized by thread, it's global lock, so doesn't make difference. But I like to do it the way you said.
Regards,
Emil Beli

 

TinyPortal © 2005-2018