Re: Threads: incompatibilities between C and C++?
On May 21, 4:22 pm, Volker Lukas <vlu...@gmx.de> wrote:
Rani Sharoni wrote:
One might argue to additional copying is allowed but then why
mandating the copying in the new thread?
Again, I only see a requirement that the calling thread makes a copy
of the function object and the arguments: "[...] with the calls to
DECAY_COPY being evaluated in the constructing thread." .
Question: How do you interpret "constructing thread"? Does this mean
to you the the new thread of execution, that which was just started,
or does it mean to you the old thread, that which constructed the
To me, it means the latter.
Ok. I think that you read this text better than me.
I looked at the boost::thread doc and it explicitly says the same.
The implementations you mentioned conforms without the wait penalty.
184.108.40.206/5 might add a bit more to my confusion:
"Synchronization: The completion of the invocation of the constructor
synchronizes with the beginning of the invocation of the copy of f."
I'm not sure about the meaning of this paragraph and why it's actually
I guess that the intention is that the caller waits to report for
potential failures in the new thread before the invocation of 'f'.
The text might be confusing though but the second implementation you
mentioned seem to have related bug (see below).
BTW - can you debug into the GCC implementation to see if it
waits? VC for sure does.
I can not see anything which I would call "waiting", i.e. I do not
see any waiting on condition variables, mutexes, semaphores etc...
This is how std::thread is implemented for GCC:
Thanks for checking this. I also looked at libc++ code you mentioned
and no wait in sight there:
thread(_Callable&& __f, _Args&&... __args)
I see an extra allocation for the args holder type (via shared_ptr)
but this is probably negligible compared with the thread creation
(native C interface doesn't require such).
For this program,
I get this output (GCC 4.7.0 + supplied library implementation):
main, thread id = 139890527434560
A(A const&, thread id = 139890527434560
A(A&&, thread id = 139890527434560
f, thread id = 139890510874368
So any copies of the function object and its argument are made in
the old thread. This is in line with my reading of the C++ standard
Thanks. definite proof that copying is done by the caller thread.
There is also an alternate implementation of std::thread, as part of
the LLVM project:
That implementation also makes the copies in the calling thread,
like with GCC.
thread::thread(_Fp&& __f, _Args&&... __args)
typedef tuple<typename decay<_Fp>::type, typename
int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>,
But there seem to be a bug in the underlying callback:
// throwing new will terminate
typedef typename __make_tuple_indices<tuple_size<_Fp>::value,
In this case the implementation should propagate the exception to the
caller thread that has to wait.
Better to simply pre-allocate this __thread_struct in the caller
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]