Library support for native_handle and std::this_thread

582 views
Skip to first unread message

Nicholas Pezolano

unread,
Apr 28, 2013, 1:55:44 PM4/28/13
to std-pr...@isocpp.org
Is it possible to add support to return the native_handle() from instead a thread's function via std::this_thread?

for example

void foo()
{
    auto native = std::this_thread::get_id().native_handle();
    // auto_native = std::this_thread::native_handle();
}

Vicente J. Botet Escriba

unread,
Apr 28, 2013, 3:02:38 PM4/28/13
to std-pr...@isocpp.org
Le 28/04/13 19:55, Nicholas Pezolano a écrit :
Only the owner of the thread having access to the std::thread instance should have access to this function.

What would you like to do with it from the running thread?

Vicente

Nicholas Pezolano

unread,
Apr 28, 2013, 6:54:50 PM4/28/13
to std-pr...@isocpp.org
I would like for certain cross-platform tasks to be made easier.

On posix systems, you can call  pthread_self();  and widows GetCurrentThread(); to return the native_handle.  but I would think something like this should be provided by the standard.

Nicol Bolas

unread,
Apr 28, 2013, 9:14:51 PM4/28/13
to std-pr...@isocpp.org


On Sunday, April 28, 2013 3:54:50 PM UTC-7, Nicholas Pezolano wrote:
I would like for certain cross-platform tasks to be made easier.

On posix systems, you can call  pthread_self();  and widows GetCurrentThread(); to return the native_handle.  but I would think something like this should be provided by the standard.



He's asking what it is you want to do with that native thread handle. What are you trying to accomplish that you need the handle for the current thread?

Lawrence Crowl

unread,
Apr 29, 2013, 2:52:43 PM4/29/13
to std-pr...@isocpp.org
On 4/28/13, Nicol Bolas <jmck...@gmail.com> wrote:
> On Sunday, April 28, 2013 3:54:50 PM UTC-7, Nicholas Pezolano wrote:
> > I would like for certain cross-platform tasks to be made easier.
> >
> > On posix systems, you can call pthread_self(); and widows
> > GetCurrentThread(); to return the native_handle. but I would
> > think something like this should be provided by the standard.
> >
> > Heres the stackoverflow post that pointed me here,
> > http://stackoverflow.com/questions/16259257/c11-native-handle-is-not-a-member-of-stdthis-thread
>
> He's asking what it is you want to *do *with that native thread
> handle. What are you trying to accomplish that you *need* the
> handle for the current thread?

If you are writing system-specific code, you can use an OS function
to get the thread handle. So, how much code you would write that
needs a native_handle function that also is not system-specific?
If it is not common, do we really need to standardize the mechanism
used to get the handle?

--
Lawrence Crowl

the.b...@gmail.com

unread,
Feb 14, 2014, 12:35:47 AM2/14/14
to std-pr...@isocpp.org
Um, what do you think the rest of the C++11 std::thread wraps around? System-specific code.  This is what wrappers are for, and part of why the std library exists in the first place.

-Brian

Sean Middleditch

unread,
Feb 14, 2014, 2:53:55 PM2/14/14
to std-pr...@isocpp.org
On Monday, April 29, 2013 11:52:43 AM UTC-7, Lawrence Crowl wrote:
On 4/28/13, Nicol Bolas <jmck...@gmail.com> wrote:
> On Sunday, April 28, 2013 3:54:50 PM UTC-7, Nicholas Pezolano wrote:
> > I would like for certain cross-platform tasks to be made easier.
> >
> > On posix systems, you can call pthread_self(); and widows
> > GetCurrentThread(); to return the native_handle.  but I would
> > think something like this should be provided by the standard.
>
> He's asking what it is you want to *do *with that native thread
> handle.  What are you trying to accomplish that you *need* the
> handle for the current thread?

If you are writing system-specific code, you can use an OS function
to get the thread handle.  So, how much code you would write that
needs a native_handle function that also is not system-specific?
If it is not common, do we really need to standardize the mechanism
used to get the handle?

There are non-standard cross-platform libraries that already take a platform-specific thread handle.  It could be transparent to integrate these with C++11 if you could get the platform-specific thread handle.

I'm actually dealing with this right now (can't go over specifics due to NDAs).  The library lets you set some debug information on a thread.  The function to set that info needs the platform thread handle (it uses whatever the native handle is in Windows, POSIX, WiiU, Xbox, Playstation, etc.).  The only way to get that handle is using the OS-specific functions, which is silly if you're otherwise using C++11 functionality everywhere (or everywhere a complete C++11 library is available, anyway).  It just means you end up having two layers (C++11 and the third-party library) that are agnostic to the platform and then a need to write some platform-specific layer just to glue them together.
Reply all
Reply to author
Forward
0 new messages