__thread and clone()

Borislav Trifonov b.trifonov@crissyfieldmedia.com
Thu Jan 6 04:08:00 GMT 2011


Greetings,

I am not sure how to get __thread working right after I have created a
thread using clone() and the CLONE_THREAD flag.  Doing a clone() and
then calling a function from both the original thread and the cloned
thread which has a "static __thread" variable shows me the same
address for the variable for both threads.  I am calling clone() as
follows:
tid = clone(stack[4 * 1024 * 1024 - sizeof(unsigned long)],
CLONE_THREAD | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
CLONE_SYSVSEM | CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
CLONE_CHILD_CLEARTID, 0, &ptid, 0, &ctid);
Compilation is invoked with g++ and no flags (gcc version is 4.3.2).
I have no other issues except __thread not giving me the expected
behavior.  I prefer not to use pthreads since I'm porting an
application that uses a custom threading model and synchronization
primitives.
I am guessing I might have to add the CLONE_SETTLS flag, but that
requires an additional parameter of type user_desc and I have no idea
how to prepare it, and I'm having trouble finding proper
documentation.  Comments in man on this flag are a bit anemic...
Or I have to do something else.  Either way, I'm stuck so I appeal for
help to this list.  Hopefully someone can help, as well as with my
additional query as to whether there is any sort of initialization
required for libc for newly cloned threads in regards to
thread-safety.

Thanks



More information about the Libc-help mailing list