Multi-processor thread dependency

Source: Internet
Author: User
Tags rounds

Multi-processor thread dependency

Summary

This article uses an interesting example to demonstrate how to set CPU thread dependencies and explains the differences and connections between physical and logical threads.

Keywords

CPU, thread, processthread, C #

The first thing to understand is that the thread in the managed code is not a real thread (OS thread ). It is just a thread that we define logically. The logic thread does not execute. The actual execution is a physical thread or an OS thread. Generally, each logical thread is associated with a physical thread, which is maintained until the end of the lifecycle of the logical thread. To set the processor dependency of a thread, we need to first obtain the physical OS thread or physical thread currently running. In this case, we need to use Windows API-getcurrentthreadid.

Class kernalhelper
{
[Dllimport ("Kernel32")]
Public static extern int getcurrentthreadid ();
}

 

Then, use the current osthreadid to find the corresponding processthread and set its processoraffinity.

The following code demonstrates how to draw a sine curve in the resource manager of a dual-core PC:

Private fields # region private fields
Private Static int rounds = 0;
Private Static int maxround = 60;
Private Static long [] sins = new long [maxround];
Private Static performancecounter PC;
Private Static bool tag = false;
# Endregion

Static void main (string [] ARGs)
{
Thread watchthread = new thread () => generatesin (1 ));
Watchthread. Start ();
Console. writeline ("press any key to break ");
Console. readkey ();
Watchthread. Abort ();
}


Public static void generatesin (INT num)
{
// Set the processor dependency for the current physical thread
Int osthreadid = kernalhelper. getcurrentthreadid ();
Foreach (processthread PT in process. getcurrentprocess (). threads)
{
If (osthreadid = pt. ID)
{
PT. processoraffinity = (intptr) num;
}
}

Try
{
Tag = true;
// Obtain the CPU usage
PC = new performancecounter ("processor", "% processor time", "_ total ");

For (int A = 0; A <maxround; A ++)
{
// Sine sequence with an amplitude of 5000000 and 60 at 60 points
Sins [a] = (long) (5000000 * Math. Sin (math. Pi * A/(double) maxround * 2f) + 5000000;
}


Long T1, T2;
Long timeslide;

While (TAG)
{
T1 = pc. rawvalue + sins [Rounds % maxround];
Timeslide = stopwatch. gettimestamp () + stopwatch. frequency;
T2 = timeslide-Stopwatch. Frequency + sins [Rounds % maxround] * stopwatch. Frequency/10000000;
While (PC. rawvalue <t1)
{
Thread. Sleep (10 );
}
While (stopwatch. gettimestamp () <timeslide );
Rounds ++;
}
}
Catch (exception ex)
{
Console. Write (ex. Message );
}
Finally
{
PC. Close ();
}
}

 

Effect:



 

For more interesting thoughts on CPU usage control, see <the beauty of programming-Microsoft technical interview experience>

A logical thread does not directly have the nature of a task for the CPU. It is only a data structure at the CLR level and is not an actual activity object at the OS level, the dependency between logical threads and OS physical threads is maintained by CLR. This is indeed a little abstract, but at the Managed Application Layer, you basically do not need to care about how the threads you define are mapped to the underlying threads. Because the OS's thread scheduling capability is much better than manual additional encoding control, unless you do have special tasks to do, or you need hardware-level memory ing, otherwise, do not change the dependency between processes and processors and between managed threads and OS threads. I don't know why. NET framework also provides corresponding methods and attributes to help programmers implement such a function that is neither "elegant" nor "secure. net's consistent style is somewhat out of tune.

By default, CLR does not guarantee that every thread is always running on the same processor. It is very likely that the thread will be processed by another processor on the next time slice. To avoid this situation, you can only explicitly specify the processor dependency. However, in general, the OS tries to ensure that all UI threads run on the processor that initiates them. Therefore, I still feel that in addition to this interesting program and some extreme tests, there is no need for manual interference.

Enjoy it ~

Yellow winter <fox23>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.