Tag Info

Hot answers tagged

125

If it is truly a 1 in 10^55 event, there would be no need to code for it. That would imply that if you did the operation 1 million times a second, you'd get one bug every 3 * 10^41 years which is, roughly, 10^31 times the age of the universe. If your application has an error only once in every trillion trillion billion ages of the universe, that's probably ...


82

First of all, threads cannot speed up execution of code. They do not make the computer run faster. All they can do is increase the efficiency of the computer by using time that would otherwise be wasted. In certain types of processing this optimization can increase efficiency and decrease running time. The simple answer is yes. You can write any code to be ...


74

It is extremely important. What is more important though is to understand that multithreading is just one way to solve the asynchrony problem. The technical environment in which many people are now writing software differs from the historical software development environment (of monolithic applications performing batch computations) in two key ways: ...


64

From the cost-benefit standpoint, you should write additional code only when it gets you enough benefit. For example, if the worst thing that would happen if a wrong thread "wins the race" is that the information would not display, and the user would need to click "refresh", don't bother guarding against the race condition: having to write a lot of code is ...


59

After having been in this crazy business since about 1978, having spent almost all of that time in embedded real-time computing, working multitasking, multithreaded, multi-whatever systems, sometimes with multiple physical processors, having chased more than my fair share of race conditions, my considered opinion is that the answer to your question is quite ...


50

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


49

If cooling is insufficient, the CPU might overheat. But they all (well, at least all modern PC CPUs) feature various thermal protection mechanisms which will throttle the clock speed or, as a final resort, shut down. So yes, on a dusty laptop, 100 % CPU load could cause temporary problems, but nothing will break or "degrade" (whatever that means). For CPU ...


46

One assumption you are making might not be valid: you require (among other things) that your threads execute simultaneously. Might work for 3, but at some point the system is going to need to prioritize which threads to run first, and which one wait. Your implementation will ultimately depend on your API, but most modern APIs will let you tell in advance ...


43

Finding a race condition is the hard part. You probably spent almost as much time writing this question as it would have taken you to fix it. It's not like it makes it that much less readable. Programmers expect to see synchronization code in such situations, and actually might waste more time wondering why it's not there and if adding it would fix their ...


40

It is getting ever more important as modern processors have more and more cores. A decade ago most of the existing computers had only a single processor, so multithreading was important only on higher-end server applications. Nowadays even basic laptops have multicore processors. In a few years even mobile devices... So more and more code is required to use ...


36

Describe what it is, just leave out the technical terms except for definitions: You have five jobs to do. You need to start working on all of them right now. Each job is a thread. You are the processor. Spend a little bit of time working on each job and then move to the next one, making sure you give attention to all of them. If you have more people, a ...


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

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.


30

What can multiple threads do that a single thread cannot? Nothing. Simple proof sketch: [Church-Turing Conjecture] ⇒ Everything that can be computed can be computed by a Universal Turing Machine. A Universal Turing Machine is single-threaded. Ergo, everything that can be computed can be computed by a single thread. Note, however, that there is a ...


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


28

The operating system provides certain primitives for this kind of interprocess communication that don't require polling. If process A is waiting on mutex M, the OS knows A can't be run and puts it aside in a bucket of processes waiting for something to happen. When the process holding M releases it, the OS looks at the list of processes waiting for it. ...


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


26

Do not rely on threads executing in lockstep. Every operating system I know of does not make the guarantee that threads execute in time consistent with each other. This means that if the CPU runs 10 threads, they do not necessarily receive equal time in any given second. They could quickly get out of synch, or they could stay perfectly in synch. That is the ...


25

In general, multi-threading is already quite important, and is only going to get more important in the next few years (as Péter Török) pointed out - it is how processors will scale for the forseeable future (more cores instead of higher MHz). In your case, however, you seem to be working mainly with web applications. Web applications, by their nature, are ...


24

Obtain and release the locks. Probabilities change, algorithms change. It's a bad habit to get into, and when something goes wrong you don't have to stop and wonder whether you got the odds wrong...


22

Hey Kid. Have you ever walked and chewed gum at the same time while thinking about Pokemon? That's your brain multi-threading.


22

A thread pool is a group of pre-instantiated, idle threads which stand ready to be given work. These are preferred over instantiating new threads for each task when there is a large number of short tasks to be done rather than a small number of long ones. This prevents having to incur the overhead of creating a thread a large number of times. ...


21

There is actually a third way in Windows to run background services, and it is very common in the UNIX world. The third way is a CRON job that runs a piece of your infrastructure. In Windows this is known as the task scheduler and is very common for running code on a scheduled basis. To use this you would create a command-line app that is executed on a ...


21

All a thread does is interleave operations so that parts of the process appear to overlap in time. A single-core machine with multiple threads merely jumps around: it executes small bits of code from one thread, then switches to another thread. A simple scheduler decides which thread is highest priority and is actually executed in the core. On a ...


20

This is an oddly phrased question that is really, really broad if answered fully. I'm going to focus on clearing up some of the specifics that you're asking about. Immutability is a design trade off. It makes some operations harder (modifying state in large objects quickly, building objects piecemeal, keeping a running state, etc.) in favor of others ...


20

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


19

Threading is hard Sure. It can be. However, people get this idea in their head that it's so hard, such that they don't bother trying to figure it out. It's not like it's impossible.


19

Should I create my class without it and use a background worker to operate on that class? Yes, you should. And I will tell you why - you are violating the Single Responsibility Principle. By tightly coupling the class that accesses the excel doc with how it accesses the excel doc, you eliminate the ability for the "controller" code (any code that ...


18

you should use only one thread per processor, Possibly in HPC where you want maximum efifciency - but otherwise the stupidest thing I have heard today! You should use the number of threads that are appropriate for the design of the program and still give acceptable performance. For a web server it might be reasonable to fire a thread for each ...



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