Help with CriticalSection needed lazarus / 9-Editor ---------------------------------------------------------------------- beli0135 · 2006-08-29 18:48 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! ---------------------------------------------------------------------- theo · 2006-08-30 08:14 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 · 2006-08-30 12:45 I will try, thanks. However, I cannot use it initialization, it must be local, just to perform one operation. ---------------------------------------------------------------------- theo · 2006-08-30 14:58 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 · 2006-08-30 14:59 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 · 2006-08-30 21:42 @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. ---------------------------------------------------------------------- felipemdc · 2006-08-30 22:50 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 · 2006-08-30 23:41 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 · 2006-08-31 00:35 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 · 2006-08-31 07:59 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 · 2006-08-31 10:56 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 · 2006-08-31 16:14 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.