[ale] Thread safe coding, Java or C
Ed Cashin
ecashin at noserose.net
Fri Jul 19 23:42:55 EDT 2013
On Fri, Jul 19, 2013 at 4:47 PM, James Sumners <james.sumners at gmail.com>wrote:
> I'm guessing you are asking which is easier to develop thread safe
> applications in. If that's the case, I'm going to say C is not it.
> Evidence -- [1].
>
Well, the question there was not so great. This is a thread-safe function
in C that's easy to create:
int add1(int n) { return n + 1; }
The reason it's thread safe is that there are no side effects. That's
why functional language aficionados brag about their languages being
easier to use with concurrency.
That questioner was really asking about making critical sections,
which is not a very good way to do synchronization (as opposed to
data-based locking or message passing), but the questioner had
that particular thing in mind, and it's true that the C language itself,
as of C99, doesn't help. (I bet pthreads is the most popular way to
implement critical sections with C.)
These days many people are taking it to the next level and trying to
avoid synchronization altogether with "lock free" data structures that
can be safely modified by concurrently running processes. They do
that because the number of cores in a computer keeps going up.
Paul McKenney
Structured Deferral: Synchronization via Procrastination
https://queue.acm.org/detail.cfm?id=2488549
> For any hope of "easy" you'd have to switch to C++. Doing that,
> though, means spending the time to find the library that works for
> you. Hell, Id Software said "eff it" and wrote their own -- [2] (see
> [3] for a good review of their model).
>
One interesting thing about C++ is the multithreading support in the
new standard
https://en.wikipedia.org/wiki/C%2B%2B11#Multithreading_memory_model
--
Ed Cashin <ecashin at noserose.net>
http://noserose.net/e/
http://www.coraid.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20130719/3e7a55ac/attachment.html>
More information about the Ale
mailing list