🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Critical Sections

Started by
6 comments, last by Jedaye 22 years, 10 months ago
Do I need to place areas of ''risky data'' that only read the data in critical sections? ** Ste
**Ste
Advertisement
It probably depends on your data. For example, if you are just reading a single integer, then you are probably ok to do this outside of a critical section (keeping in mind that the integer you read could have changed between when you read it and when you evaluate it, so you won''t see the change until the next time you read).

However, if the data is something like several elements of an array, you need to lock it in a critical section. The reason for this is that you might read the first couple of array elements, then the thread changes all the elements, then you read the rest of the elements you wanted to read. In this case, some of your information will be from before the update and the rest will be from after which could cause you no end of problems.

Basically, when in doubt you should use a critical section. It will give a slight performance hit but will probably save you lots of trouble. If you find yourself using lots of critical sections, see if you can redesign your code in some way to not share so much data between threads.
Hi, .. mostly some advice (which was written at 5:10 am hehe) and a bit of a question

Basically only use a critical section when 2 seperate threads are going to be accessing the same data this is because you could in theory have the following happen:

Thread 1 reads Data1
Thread 2 reads Data1
Thread 1 Writes some important data to Data1
Thread 2 Writes some data to Data1 (overwriting and ignoring Data 1 that thread 1 just wrote)

a Critical section would force the following..

Thread 1 gets CriticalSection1
Thread 1 reads Data1
Thread 2 gets CriticalSection1... and is put into a wait state until CriticalSection1 is free
Thread 1 Writes some important data to Data1
Thread 1 Leaves CriticalSection1
Thread 2 now kicks back into action and gets CriticalSection1
Thread 2 reads Data1 ( will read the data thread 1 just wrote into it)
Thread 2 Writes some data to Data1
Thread 2 leaves CriticalSection1

Note : when thread 2 tries to grab CriticalSection1, it is "paused" while CriticalSection1 is being used by another thread. Dont lockup threads if you cant help it if you are worried about performance ;-)


This is kinda a question and kinda another note ... am i right to say:

If you want to have the maximum speed from your app.. you should assign a critical section to each structure so as not to lock all structures when you are only accessing one.. for example

I have a CircularArrayBuffer class which does what it says :-p and is accessed by multi threads.. i have an array of these.. and i assign a critical section to each so

Thread1 can access CircularArrayBuffer[1] while Thread2 can access CircularArrayBuffer[6] but one will be locked out if they both try to access say CircularArrayBuffer[4] at the same time

~ Tim
~ Tim
You are not wrong in doing that wrath. The only problem you will face is the additional memory required for the critical section.



Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Thanks Dire..

is it paged memory(or non paged i think i meen.. the stuff which there is less of and Sockets use :-p) it uses and does it use much ?

also to kill 2 stones with 1 bird (im sleepy.. i realised after i wrote that, that its wrong but i decided to ramble about it instead of changing it..) .. back to the point ... whats the difference with IOCP in win 2000 ? im sure i read it has major advantages.. something about it can handle the threads it self (i guess it meens the worker threads???)

thanks

Tim
~ Tim
Using seperate CSs for seperate arrays is OK, though you need an additional lock for the control structure (for adding/removing elements).

Now I don''t know much about CriticalSections, as I''m always trying to use more portable ways around problems, but consider the following generic inter-thread situation:

T1 locks array[1] and does something with it
T2 locks array[4] and does something with it
T1 realizes that it needs to change other parts of the array and locks array[4]
T2 realizes that it needs to change other parts of the array and locks array[1]

Voila, you got a perfect deadlock, as both threads wait on objects that another thread has locked, and they won''t release them because they can''t, as they''re waiting for the lock.

Can you enter two critical sections at once? If so, you could potentially run into a problem like this. If not, you''re fine, but you might have to use different locking techniques for more complex transactions.

cu,
Prefect

One line of sourcecode says more than a thousand words.
Widelands - laid back, free software strategy
There is no portable way to make code thread safe. You have to build your own classes (or use a library) that provides support for specific platforms. Thread synchronization objects are OS specific - you need to build wrapper classes for them to enable portability.

Wrath,

Critical sections use paged memory.



Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Sure, thread locking is OS-specific, just like graphics. So it goes without saying that I''m using libraries (or write the wrapper code myself when I feel like it )

Just curious, what is this "non-paged memory" deal all about? Does Windows really limit certain objects to a small memory area which can''t extend up to the size of the RAM? How dumb would that be?

cu,
Prefect

One line of sourcecode says more than a thousand words.
Widelands - laid back, free software strategy

This topic is closed to new replies.

Advertisement