Tag Info

Hot answers tagged

71

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


45

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


39

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


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


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


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


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


20

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


18

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


17

Multi-threading is a red herring. Multi-threading is a implementation detail to the real problem which is Concurrency. Not all threaded programs are concurrent because of locks and what not. Threads are only one model and implementation pattern for implementing concurrent programs. For instance you can write highly scalable and fault tolerant software ...


17

This depends on what the limiting factor is, doesnt it? If the bottleneck is the utility program, then sure, running more than one copy or using more threads will speed things up. If the network is the limiting factor, then adding multiple instances of the utility isnt going to help since you still will be stuck moving at most X bytes per second. In fact ...


17

Your code has significant other issues apart from just that. Manually deleting a pointer? Calling a cleanup function? Owch. Also, as accurately pointed out in the question comment, you don't use RAII for your lock, which is another fairly epic fail and guarantees that when DoSomethingImportant throws an exception, terrible things happen. The fact that this ...


16

It sounds to me like they are leading you toward a semaphore solution. Semaphores are used to signal another thread that it's their turn. They are used much less frequently than mutexes, which I guess is why they think it's a good interview question. It's also why the example seems contrived. Basically, you would create m semaphores. Each thread x waits ...


16

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


15

A few weeks back I asked a similar question on SO. In a nut shell, my approach for some time now has been to develop a Windows Service. I would use NServiceBus (essentially MSMQ under the covers) to marshal requests from my web app to my service. I used to use WCF but getting a distributed transaction to work correctly over WCF always seemed like a pain in ...


15

one use case - websockets: as websockets are long-lived compared to simple requests, on a busy server a lot of websockets will accumulate over time. microthreads give you a good conceptual modelling and also an relatively easy implementation. more in general, cases in which numerous more or less autonomous units are waiting for certain events to occur ...


15

It is almost always simpler to think sequentially, and then later modify that logic to work better using threads. And, as the expression goes, "If it ain't broken, don't fix it." Most programmers don't use threads simply because there is no need to use them. If you feel more comfortable using them, more power to you. However, know that if threads do ...


14

Interview questions are sometimes actually trick questions, intended to make you think about the problem that you're trying to solve. Asking questions about a question are an integral part of approaching any problem, whether it's in the real world or in an interview. There are a number of videos circulating the internet on how to approach questions in ...


14

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


13

If you use C++11, threading is part of standard library and components where it makes sense like smart pointers are thread-safe (collections generally require you lock them yourself). If you are using Boost, have a look at boost.thread. It is base for what was standardized in C++11 (most new things in C++11 come from boost).


13

This is best illustrated with an example. Suppose we have a simple task that we want to perform multiple times in parallel, and we want to keep track globally of the number of times that the task has been performed, for example, counting hits on a web page. When each thread gets to the point at which it's incrementing the count, it's execution will look ...


13

It might help to think of what Erlang was originally designed to do, which was to manage telecommunications. Activities like routing, switching, sensor collection/aggregation, etc. Bringing this into the web world - consider a system like Twitter. The system probably wouldn't use microthreads in generating web pages, but it could use them in its ...


13

This is not so much about multiple cores as it is about multiple threads. The OS may schedule a thread to run on whatever core it likes, and this scheduling is transparent for the program being scheduled. However, many programs are not written using multiple threads, so they can only run on one core at once. Why would I write a single-threaded program? They ...


12

My favorite lesson – very hard won! – is that in a multithreaded program the scheduler is a sneaky swine that hates you. If things can go wrong, they will, but in an unexpected fashion. Get anything wrong, and you'll be chasing weird heisenbugs (because any instrumentation you add will change the timings and give you a different run pattern). The only sane ...


11

Joseph Albahari's article on Threading in C# is one of the best resources I've seen. The Table of Contents is below. Note that some of the topics, like the Task Parallel Library, are specific to .NET, but much of it is applicable to other languages, especially Java. GETTING STARTED Introduction and Concepts Join and Sleep How Threading Works Threads vs ...



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