Boost logo

Boost :

From: williamkempf_at_[hidden]
Date: 2001-07-31 14:57:50


--- In boost_at_y..., "Alexander Terekhov" <terekhov_at_d...> wrote:
>
> > It's very close to being ready. Beman and I are currently working
> > hard on the documentation and cleaning up some issues revealed by
> > this work.
>
> have a look at:
>
> thread::thread()
> {
> #if defined(BOOST_HAS_WINTHREADS)
> HANDLE cur = GetCurrentThread();
> HANDLE real;
> DuplicateHandle(GetCurrentProcess(), cur, GetCurrentProcess(),
&real,
> 0, FALSE, DUPLICATE_SAME_ACCESS);
> m_thread = reinterpret_cast<unsigned long>(real);
> m_id = GetCurrentThreadId();
> #elif defined(BOOST_HAS_PTHREADS)
> m_thread = pthread_self();
> m_state_manager = get_list();
> m_state_manager->add(this);
> #endif
> }
>
> thread::~thread()
> {
> int res = 0;
> #if defined(BOOST_HAS_WINTHREADS)
> res = CloseHandle(reinterpret_cast<HANDLE>(m_thread));
> assert(res);
> #elif defined(BOOST_HAS_PTHREADS)
> {
> mutex::lock lock(m_mutex);
> if (m_state_manager)
> m_state_manager->remove(this);
> }
>
> res = pthread_detach(m_thread);
> assert(res == 0);
> #endif
> }
>
> do you really want pthread_detach after each
> pthread_self() !? this is hardly what is needed
> here unless i am missing something big.

Actually, this is a bug, but not probably for the reasons you
thought. Detaching every time is fine for the Boost.Threads
implementation, since we use a CV for join() instead of pthread_join
(). The problem is, if the thread in question was not created by
Boost.Threads but by POSIX calls directly then this may yank the rug
out from underneath someone. I'll have to fix this.

> also,
> your DuplicateHandle/CloseHandle is really an
> overkill.. keeping just ONE handle and TLS/TSD
> key association is much more attractive/
> efficient, IMHO.

Actually, the DuplicateHandle is required. It's the only way to
promote the pseudo-handle returned from GetCurrentThread() into a
real handle. The reason for using GetCurrentThread() instead of TSD
is twofold. First, the TSD approach will chew up a TSD slot, and if
we can avoid doing so we should, since TSD slots are limited to 64 to
begin with. The more important reason, though, is that the TSD
approach will only work with threads created by Boost.Threads. The
GetCurrentThread() approach safely adopts all threads into the
library.

 
> why not "static thread& thread::current()" ?

Because this would be difficult to implement. I could manage this
with TSD but that's a lot of overhead (actual heap allocations would
be needed and clean up stacks used) for little benefit in the
interface.

Bill Kempf


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk