<p>How do you make your application multithreaded ?
Do you use asynch functions ?
or do you spawn a new thread ?
I think that asynch functions are already spawning a thread so if your job is doing just some file reading, being lazy and just spawning your job on a thread would just "waste" ressources...
So is there some kind of design when using thread or asynch functions ?</p>

[{'answer_id': 61348, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 3, 'selected': False, 'text': '<p>If you are talking about .Net, then don\'t forget the <a href="http://msdn.microsoft.com/en-us/magazine/cc164139.aspx" rel="nofollow noreferrer">ThreadPool</a>. The thread pool is also what asynch functions often use. Spawning to much threads can actually hurt your performance. A thread pool is designed to spawn just enough threads to do the work the fastest. So do use a thread pool instead of spwaning your own threads, unless the thread pool doesn\'t meet your needs.</p>\n\n<p>PS: And keep an eye out on the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=348F73FD-593D-4B3C-B055-694C50D2B0F3&amp;displaylang=en" rel="nofollow noreferrer">Parallel Extensions</a> from Microsoft</p>\n'}, {'answer_id': 61349, 'author': 'kokos', 'author_id': 1065, 'author_profile': 'https://Stackoverflow.com/users/1065', 'pm_score': 0, 'selected': False, 'text': "<p>The use of threads makes you think more about the way your application needs threading and can in the long run make it easier to improve / control your performance.<br>\nAsync methods are faster to use but they are a bit magic - a lot of things happen to make them possible - so it's probable that at some point you will need something that they can't give you. Then you can try and roll some custom threading code.<br>\nIt all depends on your needs.</p>\n"}, {'answer_id': 61351, 'author': 'Fire Lancer', 'author_id': 6266, 'author_profile': 'https://Stackoverflow.com/users/6266', 'pm_score': 4, 'selected': True, 'text': '<p>Spawning threads is only going to waste resources if you start spawning tons of them, one or two extra threads isn\'t going to effect the platforms proformance, infact System currently has over 70 threads for me, and msn is using 32 (I really have no idea how a messenger can use that many threads, exspecialy when its minimised and not really doing anything...)</p>\n\n<p>Useualy a good time to spawn a thread is when something will take a long time, but you need to keep doing something else.</p>\n\n<p>eg say a calculation will take 30 seconds. The best thing to do is spawn a new thread for the calculation, so that you can continue to update the screen, and handle any user input because users will hate it if your app freezes untill its finished doing the calculation.</p>\n\n<p>On the other hand, creating threads to do something that can be done almost instantly is nearly pointless, since the overhead of creating (or even just passing work to an existing thread using a thread pool) will be higher than just doing the job in the first place.</p>\n\n<p>Sometimes you can break your app into a couple of seprate parts which run in their own threads. For example in games the updates/physics etc may be one thread, while grahpics are another, sound/music is a third, and networking is another. The problem here is you really have to think about how these parts will interact or else you may have worse proformance, bugs that happen seemingly "randomly", or it may even deadlock.</p>\n'}, {'answer_id': 61388, 'author': 'Ian', 'author_id': 4396, 'author_profile': 'https://Stackoverflow.com/users/4396', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ll second <a href="https://stackoverflow.com/questions/61342/threads-or-asynch#61351">Fire Lancer\'s</a> answer - creating your own threads is an excellent way to process big tasks or to handle a task that would otherwise be "blocking" to the rest of synchronous app, <strong>but</strong> you have to have a clear understanding of the problem that you must solve and develope in a way that clearly defines the task of a thread, and limits the scope of what it does.</p>\n\n<p>For an example I recently worked on - a Java console app runs periodically to capture data by essentially screen-scraping urls, parsing the document with DOM, extracting data and storing it in a database.</p>\n\n<p>As a single threaded application, it, as you would expect, took an age, averaging around 1 url a second for a 50kb page. Not too bad, but when you scale out to needing to processes thousands of urls in a batch, it\'s no good.</p>\n\n<p>Profiling the app showed that most of the time the active thread was idle - it was waiting for I/O operations - opening of a socket to the remote URL, opening a connection to the database etc.  It\'s this sort of situation that can easily be improved with multithreading.  Rewriting to be multi-threaded and with just 5 threads instead of one, even on a single core cpu, gave an increase in throughput of over 20 times.  </p>\n\n<p>In this example, each "worker" thread was explicitly limited to what it did - open the remote a remote url, parse the data, store it in the db.  All the "high level" processing - generating the list of urls to parse, working out which next, handling errors, all remained with the control of the main thread.</p>\n'}, {'answer_id': 71567, 'author': 'Noel Grandin', 'author_id': 6591, 'author_profile': 'https://Stackoverflow.com/users/6591', 'pm_score': 0, 'selected': False, 'text': '<p>The answer is "it depends".</p>\n\n<p>It depends on what you\'re trying to achieve. I\'m going to assume that you\'re aiming for more performance.</p>\n\n<p>The simplest solution is to find another way to improve your performance. Run a profiler. Look for hot spots. Reduce unnecessary IO.</p>\n\n<p>The next solution is to break your program into multiple processes, each of which can run in their own address space. This is easiest because there is no chance of the individual processes messing each other up.</p>\n\n<p>The next solution is to use threads. At this point you\'re opening a major can of worms, so start small, and only multi-thread the critical path of the code.</p>\n\n<p>The next solution is to use asynch IO. Generally only recommended for people writing some of very heavily loaded server, and even then I would rather re-use one of the existing frameworks that abstract away the details e.g. the C++ framework ICE, or an EJB server under java.</p>\n\n<p>Note that each of these solutions has multiple sub-solutions - there are different breeds of threads and different kinds of asynch IO, each with slightly different performance characteristics, but again, it\'s generally best to let the framework handle it for you.</p>\n'}]
['https://Stackoverflow.com/questions/61342', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2566/']