5 ms·
>> "you need multiple threads when you want multiple things to happen at the same time" Computers don't work that way. Unless you have many CPUs, nothing happe
by axod 17y ago
>> "you need multiple threads when you want multiple things to happen at the same time"
Computers don't work that way. Unless you have many CPUs, nothing happens at the same time.
Rewrite your 'long running task' to do things bit by bit.
By effectively doing your own timeslicing, you remove the need for any locking or concurrency issues. Once you get into the habbit of programming like this, you wouldn't believe how much easier things are.
FWIW this is how Mibbit backend works - thousands of connections handled in a single thread.
Javascript doesn't have threads (thankfully). There is no need for threads. They look like magic, but they cause more issues than they solve IMHO. The mapping of 'work' onto physical CPUs should be done silently by the hardware IMHO (If you have more than one CPU).
- andrew1 17y agoI appreciate that nothing happens at the same time on a single core but CPU time is shared between threads so it 'appears' as if more than one thing happens at once. A good example of this is a web browser on a single core machine - the browser does not freeze up while it is downloading data. That is because the CPU time is shared between the UI thread and the other worker threads.
- axod 17y agoPossibly in some browsers. An alternate (better) model would be to simply have a single thread with a main loop, have async networking, and UI updates periodically in the same thread. while(true) { networking.check(); // Check if any sockets are ready for read/write/connect ui.update(); // Update the UI a bit if needed } The only case this would be a terrible idea is if you don't have control of all the code, or need to interface to things that may block/crash/etc.
- gaius 17y agoThis is how Tcl works.
- coliveira 17y agoThis only works if the UI library has good performance. During years this was a big issue in Java, and why it really needed multi-threading.
- scott_s 17y agoNo modern web browser controls all of the code, since it must execute arbitrary JavaScript - which can block and crash. Everyone doing something doesn't make them right. But when all major instances of an application are implemented differently than you think is best, perhaps you don't understand the problem as well as you think you do. Chrome, I think, is the best browser architecture, and it looks like IE and Firefox will adapt something similar. I think they use separate processes to manage tabs instead of threads, but it's still parallel.
- axod 17y ago>> "No modern web browser controls all of the code, since it must execute arbitrary JavaScript - which can block and crash." That's a silly argument. The following can't block or crash. while(true) { jsRuntime.executeInstruction(); // Other stuff. }
- scott_s 17y agoExecute a single JavaScript instruction at a time? I doubt the performance of that would be acceptable. But if you're aware of any browsers doing that, I'd like to know.
- axod 17y agoIf I were to write a browser right now, it's how I'd do it. You would more likely execute a few js instructions per loop, depending on what else you have to do in that loop also - network check, ui update, etc. Why would there be a performance hit in doing js instructions one by one though ;) A loop isn't expensive.
- scott_s 17y agoYou would have terrible cache performance, since you would constantly bounce back and forth between the JavaScript VM and other browser code.
- 17y ago
- andrew1 17y agoI can't reply to your comment to this. Pretty much everything I do at work involves communicating with resources which may block/crash so it wouldn't really be practical to put everything in a single while loop. It sounds a little bit to me like you're re-inventing the wheel - multi-threading means you don't have to go to the pains of breaking your long-running tasks up into pieces, the infrastructure takes care of that for you. I'm not trying to convert you, it sounds like you're perfectly happy and successful working in a single threaded environment. It just sounds like you're doing quite a lot of work to avoid multiple threads, which really aren't that difficult to manage.
- axod 17y ago... and then you have to deal with all the concurrency and blocking issues that come with outsourcing your timeslicing. Timeslicing isn't hard. It's even simpler if you're the one writing the code. If you are in the horrible position of having to interface with blocking/crashing code though, great shame :(
- andrew1 17y agoOne of the big pains of my work day is our accounting system which, while being great at what it does well, can be abysmally slow to respond to queries (the simple question of how much of X do we own now, takes it around three minutes to answer...). We know from experience though that internally it can deal with up to three requests at a time without it slowing down. If I only had one thread then three requests (which block) would take me nine minutes to process, with three threads I can get all the results back in three minutes.
- axod 17y agoInterfacing with a broken bad interface is never fun. But perhaps fix the problem instead of working around it and letting it spread...
- andrew1 17y agoUnfortunately that's not practical, it's third party software and at least the last time we checked there wasn't a better alternative with the same functionality.
- dkersten 17y agonothing happens at the same time on a single core Not always true. There are multiple levels of parallelism in modern CPUs and cores is only one of these, eg instruction-level parallelism, hyperthreading etc Anyway, theres more to concurrent programming than doing multiple things at once in parallel - theres also the use of threads to turn a synchronous blocking call into an asynchronous one, like you said.
- caf 17y agoMaking the hardware map a single thread of control onto parallel computations on separate cores is a hard problem - one that hasn't been solved yet. If you reckon you can solve it, you should definitely be able to get rich from it.
- axod 17y agoI don't think it's a problem that needs to be solved for most applications. Certainly for average websites/webapps, they're just passing data around. CPU usage shouldn't be high at all unless something really computationally expensive needs to go on like speech recognition or video encoding or something. I'll let the scientists who actually need CPU power figure that one out. Most of the websites people here are working on could be run on a 386 and still have cycles to spare.
- prodigal_erik 17y agoWindows 3 and MacOS classic apps used to get wedged every so often because cooperative multitasking is so easy to get slightly wrong. Writing everything in continuation-passing style is like writing your own filesystem or parser state machine. It's a little more efficient per core, it's complicated enough that the challenge of getting it right might be gratifying, but it's rarely the best use of our time when we're surrounded with ridiculously powerful hardware. Even netbooks have multiple cores now.
- coliveira 17y ago> Windows 3 and MacOS classic apps used to get wedged every so often because cooperative multitasking is so easy to get slightly wrong This is a different issue: multitasking of processes in the same CPU. This is an area that needs to be done by the OS, because you can't guarantee that other processes will be cooperative. Inside your own process, however, you can do whatever you want by diving tasks in small chunks, without the need for threads. The only place where I think we need threads is when dealing with libraries that we can't control. For example, UI libraries, networking libraries, math libraries, etc.
- jcl 17y agoI think his point is that when you "divide tasks in small chunks", you are no longer writing code in the way that is most convenient for you but in a way that gets better performance from a simple compiler. It means more code spread over more functions, which increases the probability of bugs.
- bobbyi 17y ago> Computers don't work that way. Unless you have many CPUs, nothing happens at the same time. I have many CPUs.
- ErrantX 17y ago> By effectively doing your own timeslicing, you remove the need for any locking or concurrency issues Native threads do this timeslicing for you; that is why they were invented (so that programmers didnt have to mess around with crazy micro-management of that they were doing). Can you imagine writing Chrome or FireFox using a single thread (it would be hard, and probably unusable)? :) Threads serve an excellent purpose
- krakensden 17y ago> Can you imagine writing Chrome or FireFox using a single thread (it would be hard, and probably unusable)? :) Firefox is single threaded. Chrome runs each tab in its own process- also not multithreading.
- petewarden 17y ago> Firefox is single threaded Incorrect. See http://mxr.mozilla.org/firefox/search?string=thread http://mxr.mozilla.org/firefox/search?string=thread
- andrew1 17y agoAt least on Windows they are both multi-threaded. If you look in Task Manager then firefox.exe will have multiple threads (32 on my machine) and each chrome.exe process will have multiple threads (anywhere between 3 and 21 on the 15 tabs I have open).
- coliveira 17y ago> Native threads do this timeslicing for you; that is why they were invented But they don't solve the fundamental problem of how to organize your code to take advantage of this. It is really easy to create a threaded application that has lower performance than a single threaded application that divides work by chunks. And, after all research in this area, there is no clear way to use threads without spending a lot of time to make sure that they work.
- axod 17y ago
- scott_s 17y agoComputers don't work that way. Unless you have many CPUs, nothing happens at the same time. Most processors in the future will be parallel architectures of some kind. Even my current laptop is a multicore.
- axod 17y agoI've heard that argument since we started having dual CPUs 5-10 years ago. I don't buy it really. Netbooks are so popular mainly because we don't need so much cpu power on our local thin clients to the web.
- coliveira 17y agoI think the idea that every piece of software will run in parallel in the future is nonsense. Hardware vendors are just trying to create a need where there is none. Clearly, someone will find applications where this power is needed (graphics, simulation, robotics), but there is no way that MS Word will run in parallel in more than a few processors. The biggest change in multicore is in enabling new applications, not in changing the way current applications are developed.
- scott_s 17y agoI did not say every piece of software will run in parallel, or that it should. The subject is the runtime environment of a programming language. If Python is going to be used in these new applications you brought up, it would help if they were able to remove the GIL.
- liuliu 17y agoThese kind of argument sounds so familiar. Decades ago, when people worked on supercomputer, hundreds of millions dollars thrown to "parallel compiler", "shared memory machine" which aimed to reduce the complexity of parallel computing for programmers. But, it just doesn't work. If a programmer cannot aware of the underlying architecture of your parallel machine, the performance will get heavily harmed. That's why there are threads, message-passing, NUMA today.
- omouse 17y agoYou know why it doesn't work? Because programmers are still mainly using C or C-derived languages.
- dkersten 17y agoTotally agree. For one, languages like Erlang, Haskell and Clojure already make concurrent programming reasonably approachable and second, if people were to switch to using proper dataflow languages* then parallelization is implicit and automagically done for the programmer. * What I really want is a proper dataflow/imperative hybrid that lets me choose the right tool for the job...
- j_baker 17y agoThe problem is that you're also preventing yourself from being able to take advantage of multiple cores. Of course, it would be nice if hardware would divide those tasks up between processors automatically. But then they'd be threads.
- omouse 17y agoFWIW this is how Mibbit backend works - thousands of connections handled in a single thread. You might as well not be running an OS and have Mibbit run straight on the hardware. All it's doing is handling network connections and some other data processing right? And don't look at me like I'm crazy, your OS is using valuable CPU time!
- axod 17y ago... which would be relevant if it was CPU bound.