dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Getting a thread's attributes

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,497

    Getting a thread's attributes

    I'm trying to use SetThreadIdealProcessor or SetThreadAffinityMask to run a thread (created by libpthreads) on a specific CPU core. I do realise that Windows is free to ignore such requests but AFAICT it isn't making any attempt at all (even the first instance of the thread doesn't get started on the requested core).

    Both entries in MSDN state that the thread must have been created with the THREAD_SET_INFORMATION access right so I'm guessing that libpthreads maybe doesn't set that attribute when creating the thread in the first place. Is there any was I can examine a thread's access rights to see if it includes THREAD_SET_INFORMATION or not?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,511

    Re: Getting a thread's attributes

    Yes - of course you can. See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    You use GetSecurityInfo(). This gives you a pointer to the DACL. You then extract the ACE's from the DACL using GetEffectiveRightsFromAcl() which will give you an access mask - which you can then check against the thread object access rights to see what rights the user has to the thread.

    See
    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    Note that CreateThread() by default sets THREAD_ALL_ACCESS thread rights.

    If the SetThread...() functions return succeeds, then the problem is not the thread access rights. If the functions fail, then this could well be the problem but the extended error info available for a fail will say what is the problem.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.3)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,497

    Re: Getting a thread's attributes

    Thanks 2kaud. It's getting late here now so I'll browse through those links tomorrow morning. However, your last link refers to the GetSecurityInfo() function which requires me to pass a parameter of type SE_OBJECT_TYPE. How would I know what value to pass if the object is a thread type?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,511

    Re: Getting a thread's attributes

    For threads, SE_OBJECT_TYPE is SE_KERNEL_OBJECT

    See
    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    Welcome to the rabbit warren!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.3)

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,497

    Re: Getting a thread's attributes

    Something occurred to me overnight... since I'm using libpthreads I guess pthread itself could be swapping the threads for some reason? It's very unlikely but maybe something I should consider. I'll check on stackoverflow to see if I can find an example showing a normal use case for SetThreadIdealProcessor or SetThreadAffinityMask. It might be helpful if I can see them working once libpthreads is eliminated from the equation.

    [Edit...] Wow, this is quite interesting... even with libpthreads, I realised that SetThreadAffinityMask() does in fact work if I create a Release build. However, it won't work in a Debug build. SetThreadIdealProcessor() doesn't work in either case. Maybe it's just a simple timing issue (i.e. it'd be different on different systems?)
    Last edited by John E; May 17th, 2018 at 04:32 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)