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?
|
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.
|
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. |
||||
|
|
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. |
|||||
|
|
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 |
|||
|
|
|
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. |
|||
|
|
|
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. |
|||
|
|
|
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. |
|||||||
|
|
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. |
|||
|
|
