Tag Info

Hot answers tagged

68

Multi-threading is simple. Coding an application for multi-threading is very, very easy. There's a simple trick, and this is to use a well-designed message queue (do not roll your own) to pass data among threads. The hard part is trying to have multiple threads magically update a shared object in some way. That's when it gets error-prone because folks ...


59

Don't get fancy, just toss a simple (threadsafe) counter behind some communication endpoint (WCF, web service, whatever): long x = long.MinValue; public long ID(){ return Interlocked.Increment(ref x); } Yes, it will eventually overflow. Yes, it doesn't handle reboots. Yes, it's not random. Yes, someone could run this on multiple servers. ...


51

A Fiber is a lightweight thread that uses cooperative multitasking instead of preemptive multitasking. A running fiber must explicitly "yield" to allow another fiber to run, which makes their implementation much easier than kernel or user threads. A Coroutine is a component that generalizes a subroutine to allow multiple entry points for suspending and ...


41

It is deprecated as a general technique, because - as you noticed - creation and destruction of short lived objects per se (i.e. memory allocation and GC) is extremely cheap in modern JVMs. So using a hand-written object pool for your run-of-the-mill objects is most likely slower, more complicated and more error-prone than plain new.* It still has its uses ...


34

A lot of programs (especially games) inherently use concurrency, No, actually it's the reverse. Most apps are written in a single threaded mindset, and the developer(s) never made the necessary changes to support concurrency. In C, C++, and C# you need to explicitly tell the application to start new threads and / or processes. I think you're ...


32

Opening Thoughts How did you come to the conclusion that some parts of the system would fare better in another language? Are you suffering performance problems? How severe are those problems? If it can be faster, is it essential that it is faster? Single-Thread Asynchrony There are several questions and other web resources that already deal with the ...


32

You almost certainly should look at Clojure - in my opinion it's the best modern language for multi-core programming and it is extremely productive. Key attributes: It's a functional language, which is a boon for both concurrency and your ability to develop using higher level abstractions. It features fully immutable persistent data structures and lazy ...


32

Is it impossible, or just plain unlikely? Impossible. It can be implemented in different ways, e.g., via the Compare-and-swap where the hardware guarantees sequential execution. It can get a bit complicated in presence of multiple cores or even multiple sockets and needs a complicated protocol between the cores, but this is all taken care of.


28

Answer to the Question The general consensus is shared mutable state is Bad™, and immutable state is Good™, which is proven to be accurate and true again and again by functional languages and imperative languages as well. The problem is mainstream imperative languages are just not designed to handle this way of working, things aren't going to change for ...


27

You could try D. It offers three models. I recommend either the first or second. std.concurrency. If you use this module for all your concurrency needs, then a combination of the language and the standard library enforces isolation between threads. Threads primarily communicate via message passing, with limited support for shared memory in a way that ...


27

Good concurrency requires a lot more than throwing a few threads in an application and hoping for the best. There's a range in how concurrent a program can be going from embarrassingly parallel to pure sequential. Any given program can use Amdahl's law to express how scalable a problem or algorithm is. A couple qualifications for a embarrassingly parallel ...


26

The latest rage in academic circles seems to be Software Transactional Memory (STM) and it promises to take all the hairy details of multi-threaded programming out of the hands of the programmers by using sufficiently smart compiler technology. Behind the scenes it is still locks and semaphores but you as the programmer don't have to worry about it. The ...


25

Here's a quick and easy motivation: If you want to code for anything but the smallest, weakest systems, you will be writing concurrent code. Want to write for the cloud? Compute instances in the cloud are small. You don't get big ones, you get lots of small ones. Suddenly your little web app is a concurrent app. If you designed it well, you can just toss in ...


23

If I were asked that question, and they made it clear that it has to be unique across reboots and across different machines, I'd give them a function that calls into the standard mechanism for creating a new GUID, whatever that happens to be in the language being used.


22

Take a look at Microsoft's Parallel Programming for .net. It is very intuitive. Many personal computers and workstations have two or four cores (that is, CPUs) that enable multiple threads to be executed simultaneously. Computers in the near future are expected to have significantly more cores. To take advantage of the hardware of today and tomorrow, you ...


22

Erlang is definitely a great option, but something a little more practical might be Go, Google's new language. It's not so far from other common languages, so it's typically easy to get if you already know other 'easy' languages. Many people compare it with Python or even Lua in terms of how 'comfortable' is it to program.


22

Yes, but it depends. You can’t expect to write nontrivial, high-performance software without both taking advantage of parallel hardware and using concurrency as a program structuring technique. But most software is both trivial and non–performance-critical. A web app isn’t doing much number crunching, and CRUD apps have nothing like the hard timing limits ...


22

The interviewer said the method will be called concurrently, not in parallel; just return the date/time down to as many decimal places as you can. Why is everyone over-thinking this? You'll be dead a long time before any finiteness is expended and you don't have a chance of a collision. If you're worried about it returning the same time, add a delay for ...


21

The answer to the concrete question: 'Is object pooling a deprecated technique?' is: No. Object pooling is widely used in specific places - thread pooling, database connection pooling etc. General object creation has never been a slow process. Pooling in itself consumes resources - memory and processing power. Any optimization is a trade-off. The rule is: ...


21

I'm only a casual Go user, so take the following with a grain of salt. Wikipedia defines green threads as "threads that are scheduled by a virtual machine (VM) instead of natively by the underlying operating system". Green threads emulate multithreaded environments without relying on any native OS capabilities, and they are managed in user space instead of ...


20

Both Erlang and Scala have actor-based concurrency, which I found very intuitive and easy to learn. The Actor model in computer science is a mathematical model of concurrent computation that treats "actors" as the universal primitives of concurrent digital computation: in response to a message that it receives, an actor can make local decisions, create ...


20

You have multiple cores/procesors, use them Async is best for doing heavy IO bound processing but what about heavy CPU bound processing? The problem arises when single-threaded code gets stuck on a long-running process. For instance, remember back when printing a word processor document required the program to lock up until the request was finished being ...


19

The classic method to do this is to use a transactional database (so there's no clashes) and to do a tentative allocation of the seat to you that expires after some length of time (e.g., 10 minutes for kiosks) that gives you enough time to pay. If the (customer-visible) transaction falls through or times out, the seat allocation can be released back into the ...


19

I don't think you can do this in the general case - detecting deadlocks in a complex system is equivalent to the halting problem so you haven't got a hope of solving it. Deadlock recovery can also be arbitrarily complex, and it's almost impossible to return the system to a "safe" state. My overall advice would be to fix the underlying architectural issues ...


18

Multi-threaded programming is probably the most difficult solution to concurrency. It basically is quite a low level abstraction of what the machine actually does. There's a number of approaches, such as the actor model or (software) transactional memory, that are much easier. Or working with immutable data structures (such as lists and trees). Generally, ...


18

I'm learning about Haskell right now, and reading this paper has convinced me that Haskell is a good option for concurrent programming. Because it's purely functional (the type system knows whether a function does any input, output, or reading/modification of global state), it can do things like Software Transactional Memory (summed up very nicely in the ...


16

Per what is written in EWD310 "Hierarchical Ordering of Sequential Processes", it looks like number 5 has been chosen for educational purposes, in order to make it easier for students to understand algorithm designed to demonstrate solution of the problem. This very paper further supports the idea that 5 is not really relevant to general problem, first by ...


16

It has nothing to do with OO in particular. The problems are problems with the "explicit locking" paradigm as an approach to concurrency, regardless of other paradigms used in other parts of the problem. I haven't watched that two hour talk, so I won't comment on Hickey's reasoning. Here are the criticisms of locks I hear most commonly, summarized: Locks ...


15

Study the concept of atomic "Test and Set" operations. Essentially the operation cannot be divided - it is not possible for two things to do it at exactly the same time. It will check a value, set it if it is clear, and return the value as it was when test. In a lock operation, the result will always be "lock == TRUE" after a test-and-set, the only ...


14

Well, of course it does. And Java reuses all the infrastructure provided by the JVM, which was written in C, and it ultimately runs in machine code! The point of enabling developers to let their applications scale is not to provide more raw power. Assembler already has all the power that your hardware allows, and no language can provide more. In fact, the ...



Only top voted, non community-wiki answers of a minimum length are eligible