Multi-threaded C # variables in study

See today know almost on a question [Why create different threads in the same process, but each thread can not access each other variables between threads? ]. In multiple threads, each thread is run independently, different threads may be the same piece of code, but it will not be the same scope, it will not be shared. The shared memory, and there is no scope of the points, within the same process, no matter what thread can all be accessed through the same virtual memory addresses, different processes can be shared memory data by other means ipc. Global variables: any thread can access; local variables (stack variables): any thread to execute when the function is accessible outside the function not accessible; thread variables: Each thread can only access its own copy of the other threads are not visible . C # today with the same code to implement different threads, global variables, local variables, thread variable.

Understand the processes and threads

What is multi-tasking, in short, it is the operating system can run multiple tasks simultaneously. For example: listening to music again, again written documents. Multi-core CPU can perform multiple tasks, but single-core CPU can perform multiple tasks, CPU is performed sequentially, the operating system so that in turn perform tasks, such as: songs performed once, pause 0.01s, written document executed once, pause, etc. 0.01s Wait. Since the execution speed of the CPU very quickly, we felt like all the tasks are executed simultaneously. On the operating system, a task is a process, a process has at least one thread. The process is the smallest unit of resource allocation, the thread is the smallest unit CPU scheduling.

Ordinary written procedure

private static List<int> data = Enumerable.Range(1, 1000).ToList();

public static void SimpleTest()
{
    for (int i = 0; i < 10; i++)
    {
        List<int> tempData = new List<int>();
        foreach (var d in data)
        {
            tempData.Add(d);
        }
        Console.WriteLine($"i: {i}, total: {data.Sum ()}, are equal to: {data.Sum () == tempData.Sum ()} " ); 
    } 

    Console.WriteLine ( " single-threaded end of run " ); 
}

Multithreading wording

private static List<int> data = Enumerable.Range(1, 1000).ToList();

public static async Task MoreTaskTestAsync()
{
    List<Task> tasks = new List<Task>();
    for (int i = 0; i < 10; i++)
    {
        var tempi = i;
        var t = Task.Run(() =>
        {
            List<int> tempData = new List<int>();
             The foreach ( var D in Data) 
            { 
                tempData.Add (D); 
            } 
            Console.WriteLine ($ " I: {} Tempi, total: {data.Sum ()}, are equal to: {data.Sum () tempData.Sum == ()} " ); 
        }); 
        tasks.Add (T); 
    } 

    the await Task.WhenAll (Tasks); // or Task.WaitAll (tasks.ToArray ()); 
    Console.WriteLine ( " multi- thread the end of the run " ); 
}

Different threads same piece of code, but it will not be the same scope, so tempData data does not affect each other.

Global variables: data, multiple threads can access, list is read only when the linear secure
local variables: i is a local variable, the thread may access the access removed [var tempi = i;], the results printed out, values is the same, each thread is increased access to a separate variable tempi

i: 10 , Total: 500500 , equality: True 
i: 10 , Total: 500500 , equality: True 
i: 10 , Total: 500500 , equality: True 
i: 10 , Total: 500500 , equality: True 
i: 10 , total: 500500 , equality: True 
i: 10 , total: 500500 , equality: True 
i: 10 , total: 500500 , equality: True 
i: 10 , total: 500500 , equality: True 
i: 10 , total: 500500 , equality: True 
i: 10 , total:500500 , for equality: True

Thread variable: tempData, each thread access only their own, independently of each other, the results

I: . 3 , total: 500 500 , equality: True 
I: . 6 , Total: 500500 , equality: True 
I: 0 , total: 500 500 , equality: True 
I: . 1 , total: 500 500 , equality: True 
I: 4 , total: 500 500 , equality: True 
I: 2 , total: 500 500 , equality: True 
I: . 7 , total: 500 500 , equality: True 
I: . 5 , total: 500 500 , equality: True 
I: . 8 , total: 500500 , equality: True 
i: 9 , total:500500 , for equality: True

Write multithreaded need to pay attention, variable scope, otherwise the results of the program will not be run out of the desired result, attention, pay attention to variable scope.

Guess you like

Origin www.cnblogs.com/zhao123/p/11084692.html