Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I want to be able to create programs which are able to create multiple thread or multiple processes. I know about the concepts of locks, semaphores but I find them difficult to code in C++/Java. Is there a language which can enable me to do this easily?

share|improve this question
1  
It's not (only) existing languages sucking at concurrency, shared-memory concurrency is fundamentally more complex than single-threaded sequential control flow. That's not to say a better-equipped language doesn't help. But it doesn't make shared-memory concurrency trivial. – delnan Oct 28 '12 at 17:40
Hence Erlang, Concurrency but no shared state – Zachary K Oct 28 '12 at 18:36

closed as not constructive by GrandmasterB, Walter, maple_shaft Oct 29 '12 at 1:25

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.

7 Answers

up vote 13 down vote accepted

Look at Erlang. It's pretty specialized for such tasks.

Edit By Zach Kessin:

Erlang is probably the way to go here. In Java you often have small number of processes that share memory and do so with Locks and Mutexes. In Erlang you generally have a very large number of processes that share no mutable state. As there is no shared state there is no need for locks.

What Erlang does is use an actor model. Which is to say that processes send each other messages. This is a rather natural model as it is how we work in real life. I know how my office mate's weekend was because I asked him (we sent messages back and forth) not because I remember what is in His head.

In addition Erlang ships with the OTP framework which is setup for building very reliable services. It is very easy to build fault tolerant applications which will keep going in the face of all sorts of errors in OTP.

share|improve this answer
Erlang is the way to go. It has a number of very cool features but it starts with very light weight processes and no shared state, so there is no need for locks or anything like that. It is also just a fun language to code in – Zachary K Oct 28 '12 at 18:35

You can try doing functional code. Pure functional languages boast their ability to be easily and automagicaly parallelised. Pure functions and imutability are great for that.

Using multiple threads in applications with shared state and functions with side effects is many times complex than doing single-threaded. And it can easily result in many times complex code with only performance increase of few percents.

share|improve this answer
1  
+1 from me because you objectively said what group of languages would be helpful, rather than singling out a specific one for whatever personal reasons. (I'd upvote again because what you say is also right, but I can't.) – sbi Oct 28 '12 at 20:18

In Clojure you can have the best of the imperative and functional worlds, as far as concurrency and parallelism are concerned.

You can go as low- or high- level as you want (using from locks and java.util.concurrent to STM and reducers), with a great host platform and language implementation.

share|improve this answer

You'll want to read and understand Threads Cannot Be Implemented As A Library by Hans-J. Boehm. That one's all about C with pthreads, but the argument extends to C++, at least.

To answer your question in light of Boehm's paper: to be absolutely certain, you'll need to drop into assembly, and understand how the multiprocessing primitives interact with the instructions your program uses.

Otherwise, you'll need to stick to a language with threading or some other form of parallelism built-in.

share|improve this answer

Google's Go language is intended to be a modern replacement for C, with same intended use cases, but emphasis on creating parallel code easily. It hides the OS threads from programmer though, so if you want to learn threads specifically, then it's not the right choice.

If you want "real" threads, Java has pretty good thread and concurrency support, and also tons of documentation about threading, and how to do it right, and also various classes and libraries for higher level concepts like running tasks in thread pools.

share|improve this answer

If you're having difficulty in using the low-level locking and synchronisation objects then I'd advise you to be very very careful with your threaded code. Look instead to the 'task' approaches used in Intel's TBB or Apple's Grand Central Dispatch - these are where you kick off independent tasks that are processed like sub-processes rather than threads in an app.

Alternatively, you can look to code like OpenMP that simplifies threading tasks in C++ code through the use of pragmas that allow you to tell the system what kind of threading you want for a chunk of code, and it figures out how to make that "single-thread" code work in parallel.

Lastly, there is a movement towards on-parallel programming, using asynchronous programming where a single thread is used, but each task is not processed "in-line" but it put on a queue where the OS runs it as it can, when the task completes, it informs the main thread so it can process the results (usually by queuing another task). Windows 8 uses this model, but it was made popular in the server-side javascript system Node.JS. the trick to using this effectively is to make many calls to asynchronous tasks and keep any processing on the main thread short.

share|improve this answer
1  
Continuation passing style far predates Node.JS en.wikipedia.org/wiki/Continuation_passing_style also it's most useful for asynchrony which is not the same as concurrency – Jimmy Hoffa Oct 28 '12 at 18:57
Um, last time I looked (which, admittedly, is a while ago), TBB tasks were implemented as threads, and did nothing to prevent you from sharing state between them. – sbi Oct 28 '12 at 20:21

Ada was designed for parallel execution from the very beginning.

This is a good intro to tasks (roughly equivalent to threads in most other languages)

Ada also has protected objects, functions (read-only access, can be called by multiple threads simultaneously) and procedures (exclusive access) and synchronization built in.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.