Threading Rules and Condvars

BlitzMax Forums/BlitzMax Programming/Threading Rules and Condvars

Scienthsine(Posted 2009) [#1]
I have a few questions about threading.

Is it safe and acceptable to read data from multiple threads so long as I don't write to that data?

How do the Condvars work? They seem simple enough I guess... but what is the difference between SignalCondVar and BroadcastCondVar? Also, they have mutex parameters on those... so... I don't fully understand.

Can someone give me a quick explanation on CompareAndSwap, AtomicAdd, and AtomicSwap. I assume that these are threadsafe? What's their advantage and purpose?

Finally, does anyone know about how much time it takes to use mutexes? I mean, how much work does one need to offload to another thread for it to possibly be worth it?
--What I mean, is if I have an collection of data, and a long process involving hundreds of operations per entity in the collection, then using Semaphores would be great. I could have a few threads each processing an entity in parallel and one thread supplying the work. With a single core, this will ofcourse be slower than just doing it with the single thread, but with four cores it would be a great improvement.
--On the other hand, if I was doing just a couple operations per entity, even with thousands of threads, this system (with the single entity supply-demand model) would have too much overhead in the thread interaction. It would be faster to just do it all in one thread. Ofcourse there are a few things that could help, such as assigning a range of entities to each thread. I'd still like to have a better understanding of the overhead involved though.
--I guess I can setup some benches and try to get an idea of this myself... but I thought I'd ask just incase someone has a quick 'mutexes take a few milliseconds to settle', '5 clock cycles on most processors', 'faster than 3 floating point operations', or whatever.

Any and all help appreciated.


N(Posted 2009) [#2]
I may be wrong about any number of things since most of what I know is self-taught and/or conjecture, so if I say something and it contradicts what someone else said, chances are the other person is right. Now...

Is it safe and acceptable to read data from multiple threads so long as I don't write to that data?
I think it should be, but I don't know what sort of rules there are off the top of my head.

Can't comment on condvars, not sure how they work.

Can someone give me a quick explanation on CompareAndSwap, AtomicAdd, and AtomicSwap. I assume that these are threadsafe? What's their advantage and purpose?
CompareAndSwap checks a value, and if it matches the old value, it swaps it with the new value. AtomicAdd adds a value to something, AtomicSwap exchanges a value for another. The idea is that these are atomic operations (and so thread safe-ish, I believe), and I'd recommend googling about those for more information. CompareAndSwap is sort of handy for spin-locks, AtomicAdd and AtomicSwap could probably be used for other things (e.g., incrementing a count variable or exchanging some values for whatever reason).

Finally, does anyone know about how much time it takes to use mutexes? I mean, how much work does one need to offload to another thread for it to possibly be worth it?
Mutexes under Windows are incredibly slow, mutexes under Linux and Mac OS are generally very fast. I would try to design something to be lock-free if at all possible, though. This sort of thing came up with the new GC, so Mark or James might be able to comment on it more thoroughly.


Scienthsine(Posted 2009) [#3]
Another question. Assuming I can read data from multiple threads at once, as long as one isn't writing. Am I safe to assume that TList traversal through next and previous, aswell as foreach are thread safe? Can I traverse the same TList with multiple threads safely so long as I don't change it?


N(Posted 2009) [#4]
Another question. Assuming I can read data from multiple threads at once, as long as one isn't writing. Am I safe to assume that TList traversal through next and previous, aswell as foreach are thread safe? Can I traverse the same TList with multiple threads safely so long as I don't change it?
This should be safe. There are probably plenty of people who would disagree with that or call it bad practice, though. TMap traversal, however.. I think that could possibly be unsafe.


Kurator(Posted 2010) [#5]
Sorry for resurrecting this old thread, I stumbled over it while searching for programming with conditional variables.

Heres a good tutorial about conditionVars http://www.ibm.com/developerworks/linux/library/l-posix3/index.html?S_TACT=105AGX03&S_CMP=EDU


ImaginaryHuman(Posted 2010) [#6]
I would say you can read from the same data from as many threads as you like without any problems, so long as the data doesn't need to be written. If you think about it, without changing the data, every thread will read the same value. The only reason I can hypthesize as a possible problem is IF for some reason the resource is MOVED in memory as part of some kind of optimization or something, but I don't know if that's likely. I know we have things like locks for TImage and TBank but I generally assume my memory isn't going to move.


Galaxy613(Posted 2010) [#7]
Thanks for that link Kurator, I now understand them. :)