175

The canonical time when you use far, far more processes than cores is when your processes aren't CPU bound. If your processes are I/O bound (either disk or more likely network), then you can absolutely and sensibly have a huge number of processes per core, because the processes are sleeping most of the time anyway. Unsurprisingly enough, this is how any ...


159

Because "multiple concurrent writes" is much, much harder to accomplish in the core database engine than single-writer, multiple-reader. It's beyond SQLite's design parameters, and including it would likely subvert SQLite's delightfully small size and simplicity. Supporting high degrees of write concurrency is a hallmark of large database engines such as ...


153

Alice wants to pay Bob for a service. Bob has quoted her $10. Give this quote a unique token. Alice clicks pay. When this response is send to the server, it must go with the token of what is being paid. This also allows you to discard duplicate payments. Whilst Alice's request is flying through the ether, Bob edits his quote. He now wants ...


118

Concurrency and parallelism are two related but distinct concepts. Concurrency means, essentially, that task A and task B both need to happen independently of each other, and A starts running, and then B starts before A is finished. There are various different ways of accomplishing concurrency. One of them is parallelism--having multiple CPUs working on ...


113

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


73

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


60

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


57

A quote should be a write-once record. Bob isn't allowed to edit it once it has been created and passed to Alice. You can ensure this at different levels, from simply not offering an edit dialog to sophisticated digital signature algorithms.


53

Short answer: Yes. Longer answer: Set your magic number stupid high, benchmark it, set it low, benchmark it again, and keep doing that until you have your answer. The number of moving parts here is way too high to arrive at an answer via analysis in any kind of reasonable timeframe, you'll get a much more reliable answer much more quickly by just running ...


44

The two concepts are related, but different. Concurrency means that two or more calculations happen within the same time frame, and there is usually some sort of dependency between them. Parallelism means that two or more calculations happen simultaneously. Put boldly, concurrency describes a problem (two things need to happen together), while parallelism ...


36

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.


34

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 blocks (ie gets stuck) on a long-running process. For instance, remember back when printing a word processor document would make the whole application freeze until the job was ...


32

I remember the JVM abandoning green threads and moving to native threads. This was for two simple reasons: the green threads were frankly rubbish, and there was a need to support multi-core processors with the limited developer effort available at Sun. This was a shame - green threads provide a far better abstraction, allowing concurrency to be a useful ...


30

Coroutines never left, they were just overshadowed by other things in the meanwhile. The recently increased interest in asynchronous programming and therefore coroutines is largely due to three factors: increased acceptance of functional programming techniques, toolsets with poor support for true parallelism (JavaScript! Python!), and most importantly: the ...


28

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


28

Major operating systems are mature enough to know how to handle processes which use every available core. Other processes may (and often will) be affected, but the computation won't become slower because you used every available core. The choice of the number of cores depends more on your intention of doing something else while the calculation is being ...


28

This must be an extremely common problem to deal with, no? No, it isn't. I doubt you'll be able to find a payment processor that lets you change the amount after the customer has authorised a particular amount. You sell to Alice at the price she authorised, because that's what your quote to her was, and what she authorised. You don't check that the money ...


27

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

You are looking for the File Locking And Concurrency documentation. SQLite processes use a series of locks to handle concurrency; to read, several processes can obtain a SHARED lock. A process that writes, will need to obtain a RESERVED lock, and only when actually having to flush changes to disk does it move to the PENDING state. Any reading process will ...


25

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.


24

I sketched my rough understanding on how an ES / CQRS app should look like contextualized to a simplified banking use case (withdrawing money). This is the perfect example of an event sourced application. Let's start. Every time a command is processed or retried (you will understand, be patient) the following steps are performed: the command reaches a ...


23

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

In practice, there is very little difference: both represent separate units of execution whose primary interface with the outside world is via messages. The differences are in the implementation details of the languages. Here are a few such details: Channels in Go are typed; if you want to send messages with different data, you need separate channels. ...


21

The ETag mechanism specifies only the communication protocol for optimistic locking. It's the responsibility of the application service to implement the mechanism to detect concurrent updates to enforce the optimistic lock. In a typical application that uses a database, you'd usually do this by opening a transaction when processing a PUT request. You'd ...


20

Just send the amount Alice agreed to pay along with the request. If the price has increased since Alice sent the request, you send a response indicating that the item could not be purchased at or below that price, and the current price is whatever it is. This is pretty much the same situation as when there's only one item available and, at the time the ...


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