

Threading a brief/details

Discussion in 'General Programming Chat' started by frozenocean, Nov 17, 2011.

  1. frozenocean

    frozenocean Junior Member

    Sep 30, 2011
    Likes Received:
    both arctic
    [FONT=&quot]I am writing this article for the people curious about threading or multithreading. I am a .net developer so my perspective is biased about windows and .net. Remember the concept is not too far for other OS. [/FONT]
    [FONT=&quot]I wrote this article with some plagiarism. Don?t bother about that read the article. [/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot]How Threading Works[/FONT]
    [FONT=&quot]Multithreading is managed internally by a thread scheduler, a function the CLR typically delegates to the operating system. A thread scheduler ensures all active threads are allocated appropriate execution time, and that threads that are waiting or blocked ? for instance ? on an exclusive lock, or on user input ? do not consume CPU time.[/FONT]
    [FONT=&quot]On a single-processor computer, a thread scheduler performs time-slicing ? rapidly switching execution between each of the active threads. This results in "choppy" behavior, such as in the very first example, where each block of a repeating X or Y character corresponds to a time-slice allocated to the thread. Under Windows XP, a time-slice is typically in the tens-of-milliseconds region ? chosen such as to be much larger than the CPU overhead in actually switching context between one thread and another (which is typically in the few-microseconds region).[/FONT]
    [FONT=&quot]On a multi-processor computer, multithreading is implemented with a mixture of time-slicing and genuine concurrency ? where different threads run code simultaneously on different CPUs. It's almost certain there will still be some time-slicing, because of the operating system's need to service its own threads ? as well as those of other applications.[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot]A thread is said to be preempted when its execution is interrupted due to an external factor such as time-slicing. In most situations, a thread has no control over when and where it's preempted.[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot]Threads vs. Processes[/FONT]
    [FONT=&quot]All threads within a single application are logically contained within a process ? the operating system unit in which an application runs.[/FONT]
    [FONT=&quot]Threads have certain similarities to processes ? for instance, processes are typically time-sliced with other processes running on the computer in much the same way as threads within a single application. The key difference is that processes are fully isolated from each other; threads share (heap) memory with other threads running in the same application. This is what makes threads useful: one thread can be fetching data in the background, while another thread is displaying the data as it arrives.[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot]When to Use Threads[/FONT]
    [FONT=&quot]A common application for multithreading is performing time-consuming tasks in the background.[/FONT]
    [FONT=&quot]The main thread keeps running, while the worker thread does its background job. With Windows Forms applications, if the main thread is tied up performing a lengthy operation, keyboard and mouse messages cannot be processed, and the application becomes unresponsive. For this reason, it?s worth running time-consuming tasks on worker threads even if the main thread has the user stuck on a ?Processing? please wait? modal dialog in cases where the program can?t proceed until a particular task is complete. This ensures the application doesn?t get tagged as ?Not Responding? by the operating system, enticing the user to forcibly end the process in frustration! The modal dialog approach also allows for implementing a "Cancel" button, since the modal form will continue to receive events while the actual task is performed on the worker thread. The BackgroundWorker class assists in just this pattern of use.[/FONT]
    [FONT=&quot]In the case of non-UI applications, such as a Windows Service, multithreading makes particular sense when a task is potentially time-consuming because it?s awaiting a response from another computer (such as an application server, database server, or client). Having a worker thread perform the task means the instigating thread is immediately free to do other things. Another use for multithreading is in methods that perform intensive calculations. Such methods can execute faster on a multi-processor computer if the workload is divided amongst multiple threads. (One can test for the number of processors via the Environment.ProcessorCount property).[/FONT]
    [FONT=&quot]A .net application can become multi-threaded in two ways: either by explicitly creating and running additional threads, or using a feature of the .NET framework that implicitly creates threads ? such as BackgroundWorker, thread pooling, a threading timer, a Remoting server, or a Web Services or ASP.NET application. In these latter cases, one has no choice but to embrace multithreading. A single-threaded web server would not be cool ? even if such a thing were possible! Fortunately, with stateless application servers, multithreading is usually fairly simple; one's only concern perhaps being in providing appropriate locking mechanisms around data cached in static variables.[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot]When Not to Use Threads[/FONT]
    [FONT=&quot]Multithreading also comes with disadvantages. The biggest is that it can lead to vastly more complex programs. Having multiple threads does not in itself create complexity; it's the interaction between the threads that creates complexity. This applies whether or not the interaction is intentional, and can result long development cycles, as well as an ongoing susceptibility to intermittent and non-reproducable bugs. For this reason, it pays to keep such interaction in a multi-threaded design simple ? or not use multithreading at all ? unless you have a peculiar penchant for re-writing and debugging![/FONT]
    [FONT=&quot]Multithreading also comes with a resource and CPU cost in allocating and switching threads if used excessively. In particular, when heavy disk I/O is involved, it can be faster to have just one or two workers thread performing tasks in sequence, rather than having a multitude of threads each executing a task at the same time. Later we describe how to implement a Producer/Consumer queue, which provides just this functionality.[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot]Hope this helps you to have a basic idea on threading. [/FONT]
    • Thanks Thanks x 1
  2. The SEO

    The SEO Jr. VIP Jr. VIP

    Dec 14, 2011
    Likes Received:
    Home Page:
    Its just awesome....I really got many helpful tips about threading
    Keep Sharing articles like that...