Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between multithreading and multitasking in C# [duplicate]

Possible Duplicate:
What is the difference between task and thread?

I understand the title itself may appear to be a duplicate question but I've really read all the previous posts related to this topic and still don't quite understand the program behavior.

I'm currently writing a small program that checks around 1,000 E-mail accounts. Undoubtedly I feel multithreading or multitasking is the right approach since each thread / task is not computationally expensive but the duration of each thread relies heavily on network I/O.

I think that under such a scenario, it would also be reasonable to set the number of threads / tasks at a number that is much larger than the number of cores. (four for i5-750). Therefore I've set the number of threads or tasks at 100.

The code snippet written using Tasks:

        const int taskCount = 100;
        var tasks = new Task[taskCount];
        var loopVal = (int) Math.Ceiling(1.0*EmailAddress.Count/taskCount);

        for (int i = 0; i < taskCount; i++)
        {
            var objContainer = new AutoCheck(i*loopVal, i*loopVal + loopVal);
            tasks[i] = new Task(objContainer.CheckMail);
            tasks[i].Start();
        }
        Task.WaitAll(tasks);

The same code snippet written using Threads:

        const int threadCount = 100;
        var threads = new Thread[threadCount];
        var loopVal = (int)Math.Ceiling(1.0 * EmailAddress.Count / threadCount);

        for (int i = 0; i < threadCount; i++)
        {
            var objContainer = new AutoCheck(i * loopVal, i * loopVal + loopVal);
            threads[i] = new Thread(objContainer.CheckMail);
            threads[i].Start();
        }
        foreach (Thread t in threads)
            t.Join();
        runningTime.Stop();
        Console.WriteLine(runningTime.Elapsed);

So what are the essential differences between these two?

like image 307
derekhh Avatar asked Oct 16 '12 20:10

derekhh


People also ask

What are the differences between threads and multi process?

By formal definition, multithreading refers to the ability of a processor to execute multiple threads concurrently, where each thread runs a process. Whereas multiprocessing refers to the ability of a system to run multiple processors concurrently, where each processor can run one or more threads.

Why is multithreading better than multitasking?

Unlike multitasking, multithreading provides the same memory and resources to the processes for execution. 1. In multitasking, users are allowed to perform many tasks by CPU. While in multithreading, many threads are created from a process through which computer power is increased.

What is difference between multitasking and multiprocessor?

The execution of more than one task simultaneously is known as multitasking. The availability of more than one processor per system, that can execute several set of instructions in parallel is known as multiprocessing.

What is multitasking in C?

Multitasking with Dynamic C. A task is an ordered list of operations to perform. In a multitasking environment, more than one task (each representing a sequence of operations) can appear to execute in parallel. In reality, a single processor can only execute one instruction at a time.


1 Answers

Tasks do not necessarily correspond to threads. They will be scheduled by the task library to threadpool threads in a much more efficient manner than your thread code.

Threads are fairly expensive to create. Tasks will queue up and reuse threads as they become available, so when a thread is waiting for network IO, it can actually be reused to execute another task. Threads that sit idle are wasted resources. You can only execute the number of threads that corresponds to your processor core count (simultaneously), so 100 threads means context switches on all your cores at least 25 times each.

If using tasks, just queue up all 1000 email processing tasks instead of batching them up and let it rip. The task library will handle how many threads to run it on.

like image 141
Mike Marynowski Avatar answered Sep 30 '22 17:09

Mike Marynowski