Setthreadaffinitymask Setting Thread affinity

Source: Internet
Author: User

The setthreadaffinitymask function sets a processor affinity mask for the specified thread.

[Delphi]View Plaincopyprint?
    1. Dword_ptr Setthreadaffinitymask (
    2. HANDLE Hthread,
    3. Dword_ptr Dwthreadaffinitymask
    4. );
Parameters
Hthread
[In] Handle to the thread whose affinity mask are to BES set.

This handle must has the thread_set_information and thread_query_information access rights. For more information, see Thread Security and Access rights.

Dwthreadaffinitymask
[In] Affinity mask for the thread.

Windows Me/98/95:this value must be 1.

Return Values

If The function succeeds, the return value is the thread ' s previous affinity mask.

Windows me/98/95:the return value is 1. To succeed, Hthread must is valid and Dwthreadaffinitymask must be 1.

If The function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

A thread affinity mask is a bit of vector in which each bit represents the processors that a thread was allowed to run on.

A thread affinity mask must is a proper subset of the process affinity mask for the containing process of a thread. A thread is an allowed to run on the processors it process is allowed to run on.

By calling Setthreadaffinitymask, you can set affinity masking for each thread:

[Delphi]View Plaincopyprint?
    1. Dword_ptr Setthreadaffinitymask (HANDLE hthread, dword_ptr dwthreadaffinitymask);

The H T H r e A D parameter in the function is used to indicate which thread to limit, and dwthreadaffinitymask is used to indicate which CPU the thread can run on. The dwthreadaffinitymask must be a corresponding subset of the affinity masks for the process. The return value is the previous affinity mask for the thread.

Therefore, to limit 3 threads to CPU1, 2, and 3, you can do this:

[Delphi]View Plaincopyprint?
    1. Thread 0 can only run on CPU 0.
    2. Setthreadaffinitymask (hThread0, 0x00000001); //No. 0 bit is 1
    3. Threads 1, 2, 3 run on CPUs 1, 2, 3.//1th 2 3 bit is 1
    4. Setthreadaffinitymask (HThread1, 0x0000000e);
    5. Setthreadaffinitymask (HThread2, 0x0000000e);
    6. Setthreadaffinitymask (HThread3, 0x0000000e);

http://blog.csdn.net/yanjiaye520/article/details/7638219

Setthreadaffinitymask Setting Thread affinity

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.