Topic: Adding this_thread::native_handle free function


Author: viboes <vicente.botet@wanadoo.fr>
Date: Wed, 16 Dec 2009 17:59:49 CST
Raw View
class std::thread defines a native_handle member function that allows
to use non portable or not provided features provided by the
underlying platform. Sometimes, we need to get the native handle of
the current thread.

What do you think about adding a free function to get this handle.

namespace this_thread {
   native_handle_type native_handle();
}

Vicente

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Howard Hinnant <howard.hinnant@gmail.com>
Date: Thu, 17 Dec 2009 11:59:22 CST
Raw View
On Dec 16, 6:59 pm, viboes <vicente.bo...@wanadoo.fr> wrote:
> class std::thread defines a native_handle member function that allows
> to use non portable or not provided features provided by the
> underlying platform. Sometimes, we need to get the native handle of
> the current thread.
>
> What do you think about adding a free function to get this handle.
>
> namespace this_thread {
>    native_handle_type native_handle();
>
> }

The C++ thread model is set up around the foundation:  there is one
owner to a thread.

If a thread can silently become its own owner and do things that its
other owner isn't suspecting, then I fear we will loose the ability to
rationalize about the thread's behavior.

For example, perhaps the owner of a thread needs native_handle() to
set the priority of the thread.  Very reasonable.

Somebody has possession of the the std::thread which can call
t.native_handle() and set the priority.  But what if somebody else
(other than the owner of t) does that?  If t can set its own priority,
what guarantee does the owning thread have that it actually had any
impact when it set t's priority?  None!

On the other hand, if t needs to set its own priority, that can be
accomplished by passing the std::thread referring to t, to the thread
executing t (std::thread's are movable).  So if there is no way to say
set_high_priority(std::this_thread::native_handle()), there is still a
way to cooperatively arrange for a thread to own itself and say
set_high_priority(self.native_handle()).  And then one knows that the
setting is good as long as you own the std::thread (designated by self
in this example).

Admittedly this restriction is not an air-tight prohibition on two
threads calling set_high_priority(nh) on the same native_handle.
However if the well-documented protocol is that there is only one
owner of a thread at a time (a thread can own itself or not), then it
becomes much easier to reason over who is the thread owner at any one
time, and who has the authority to set the priority (or whatever else
needs to be done via native_handle()).  This is the rationale behind
why std::thread is move-only and not copyable.

-Howard


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Yechezkel Mett <ymett.on.usenet@gmail.com>
Date: Sun, 20 Dec 2009 12:22:13 CST
Raw View
On Dec 17, 1:59 am, viboes <vicente.bo...@wanadoo.fr> wrote:
> class std::thread defines a native_handle member function that allows
> to use non portable or not provided features provided by the
> underlying platform. Sometimes, we need to get the native handle of
> the current thread.
>
> What do you think about adding a free function to get this handle.
>
> namespace this_thread {
>    native_handle_type native_handle();
>
> }

It isn't really necessary. Since the only point of getting the native
handle is to call platform-dependant functions, you might as well call
a platform-dependant function to get the handle.

Yechezkel Mett


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: viboes <vicente.botet@wanadoo.fr>
Date: Mon, 21 Dec 2009 19:10:30 CST
Raw View
On Dec 20, 7:22 pm, Yechezkel Mett <ymett.on.use...@gmail.com> wrote:
> On Dec 17, 1:59 am, viboes <vicente.bo...@wanadoo.fr> wrote:
>
> > class std::thread defines a native_handle member function that allows
> > to use non portable or not provided features provided by the
> > underlying platform. Sometimes, we need to get the native handle of
> > the current thread.
>
> > What do you think about adding a free function to get this handle.
>
> > namespace this_thread {
> >    native_handle_type native_handle();
>
> > }
>
> It isn't really necessary. Since the only point of getting the native
> handle is to call platform-dependant functions, you might as well call
> a platform-dependant function to get the handle.
>
> Yechezkel Mett
>
> --
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-...@netlab.cs.rpi.edu]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html                     ]

You are completely right. On what I was thinking :(

Thanks,
Vicente


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]