if one of the threads only read an Cardinal i have to protect the acess?
ps: sorry for the bunch of question, i don't have a Dual Core processor to test ;/
if one of the threads only read an Cardinal i have to protect the acess?
ps: sorry for the bunch of question, i don't have a Dual Core processor to test ;/
From brazil (:
Pascal pownz!
if it is only reading from a value, then no, you do not need toOriginally Posted by arthurprs
This is a misleading answer... Mirage summed it up best. If there is the possibility that one thread will read, while another writes to the same variable /record/object, then you should protect ALL access to it accordingly.Originally Posted by Memphis
If you have blocks of code that read and write, then protect them with critical sections. If you do more reading than writing, then you may get a performance benefit from using TMultiReadExclusiveWriteSynchronizer which allows simultaneous reads from lots of threads, but will completely block everything when one thread attempts a write.
You should assume nothing about atomic operations or execution sequence, nothing... except, that if it can go wrong, you can guarantee it will. So, my advice is think carefully about cross thread interactions... where possible keep it to an absolute minimum, and if you can't, try and group variables together into logical blocks that are likely to get updated together and protect the group... this will help keep the overheads down.
I should also point out, that without running on a dual core or dual CPU machine, you will not find all the potential problems, its impossible as you will only ever have a single thread of execution. To get the real nasty problems you need multiple threads of simultaneous execution... you can only get that on dual core or dual CPU machines.
:: AthenaOfDelphi :: My Blog :: My Software ::
Thanks for the answers guys, i have solved the problem with 2 CriticalSections
From brazil (:
Pascal pownz!
it is not misleading at all, if you read correctly.Originally Posted by AthenaOfDelphi-MMif it is only reading
Memphis, it is misleading unless you can ensure atomicity when writing to the variable from somewhere else
Peregrinus, expectavi pedes meos in cymbalis
Nullus norvegicorum sole urinat
and as he asked, if it is 'only' reading.... now it is not misleading at all, like i said.. IF it is only READING then no, if 2 threads read from the same variable, it is not a problem at all... and in an earlier post i also said to read athenaofdelphis post about IF writing to a variable also ( in which case it would cause a problem ) ... now i rest my case....Originally Posted by JSoftware
It's not a problem if two threads read from it but that was not what arthurps asked about. He asked if it was ok if one of the threads were reading the variable. That would imply that some other thread will write to it. You can't guarantee that this won't blow upOriginally Posted by Memphis
Peregrinus, expectavi pedes meos in cymbalis
Nullus norvegicorum sole urinat
Before this descends into a total flame war...
I think we (that is I, JSoftware, Memphis and Mirage) are all pretty much singing from the same song sheet.
If you can guarantee that a variable will only ever be read by multiple threads, the protection is not required. If however one of those could write to it, then protection is most likely required.
That I think sums it up nicely.
:: AthenaOfDelphi :: My Blog :: My Software ::
Bookmarks