views:

229

answers:

5

I've just started learning about thread safety. This is making me code a lot more defensively, perhaps too defensively.

Would using a functional language like Erlang completely rid me of this concern?

+1  A: 

Disclaimer: I know virtually no Erlang. Take my opinion with a grain of salt.

A purely functional language that (obviously) only allows "pure" functions. Wiki says pure functions are always thread-safe, thus confirming my gut feeling on that topic.

But I don't know if Erlang is purely functional (wiki implies otherwise, so I guess it isn't). Perhaps it uses other means of achieving thread-safety. Anyway, its data structures are (mostly? exclusively?) immutable, and are thus inherently thread-safe. And being a functional language, at least idiomatic Erlang code won't use (muc? any?) global variables but use pure functions instead. Those are thread-safe. But things I/O may still be an issue... if an Erlang program reads and writes a file concurrently, such code unlikely not thread-safe. But most of the time, you'll be fine thanks to immutable data structures and such.

delnan
+15  A: 

in Erlang the unit of execution state isn't a thread, but a process. yeah, it's a lightweight kind of process implemented on top of threads; but it's more like a process than a thread.

The main point is that processes don't share state, they pass messages; while threads share everything by default, and have to arbitrate to avoid chaos.

thus, you don't need thread safety since you're not working with threads.

Javier
+1 exactly right
Matt Williamson
But you do need "process safety" :)
Alexey Romanov
+4  A: 

Javier is right.

However, I'd like to just add something as it has caught me before. If you are working with a built-in driver or nif it may not be thread safe anymore. It seems obvious since the driver or nif will be using C or C++ code, but it's worth mentioning. So you can't completely ignore thread safety just because you are using Erlang.

Olives
+3  A: 

No. See Erlang Style Actors Are All About Locking. It is much easier to achieve thread safety in a functional language, but you do need to think about it.

I've just started learning about thread safety. This is making me code a lot more defensively, perhaps too defensively.

Note that in that case it's very likely you are still getting things wrong. Shared-everything concurrency is very very hard to get right in anything except most trivial examples.

Alexey Romanov
Interesting article, but I don't think "thread-safe" means dead-lock free. In the Java/C# world it usually means that an object protected against concurrent updates, and can be used within a multi-threaded environment safely. The OP's reference to defensive programming suggests the same perspective.
dsmith
But you are correct. New Erlang programmers cannot ignore the possibility of dead-locks. But the interesting thing about the article is that it eludes to a way to "break" a dead-lock without stopping the VM. You open an Erlang shell and push a message to the locked process.
dsmith
+1  A: 

No, you still need to consider thread safety in Erlang, but the problems are much simpler to solve.

I read an article in which the author pointed out that your message API can cause threading issues. The example revolved around a bank account. His initial message API had GET and SET operations. Some code that wanted to deposit $100 would GET the current value, add 100 to it, and then SET the result. Of course, this only works if a single process is accessing the bank account. Once two processes are manipulating the balance, all bets are off.

His solution was to change the message API to DEPOSIT and WITHDRAW (he actually uses one message - UPDATE - but you get the idea). This causes the interaction to assume atomic semantics - the listening process will only process one deposit or withdrawal at a time, and will block other requests until the first is complete.

It's worth noting that this problem is essentially the same as the shared-memory problem. (If we use GET and SET messages to interact with a process, we have essentially created some shared memory). Another blogger compares ets to shared memory as well. However, as long as you understand where you have introduced shared-memory-like constructs and regulate access to that shared memory, you shouldn't have any threading issues (other than deadlock, of course).

Daniel Yankowsky