🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Runtime TLS initialization when using clone()

Started by
2 comments, last by Prune 13 years, 5 months ago
If I create threads using clone() with the CLONE_THREAD flag, can I assume that any thread local storage the glibc runtime uses, as well as anything I have declared with __thread, is initialized properly? In Windows, _beginthreadex provides for this. If it's not the case with clone(), how do I go about doing it?

[Edit:] __thread doesn't seem to be working, as I get the same address of a static __thread variable in a function called from both threads... So how do I get __thread working when/after using clone()?

[Edited by - Prune on January 5, 2011 10:13:56 PM]
"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)
Advertisement
If I'm not mistaken, to have __thread work properly with clone you must use the flag CLONE_SETTLS and setup a thread local storage descriptor to pass as the sixth argument to clone. As far as I am aware this will be kernel/architecture specific. That all said, I've never done this myself.

This is all done for you if you use pthreads.
The only reason I didn't use pthreads is since pthread_create() would be the only function I'm using (I use atomic operations and FUTEXes directly for synchronization). But it's no big deal and I'll do it with pthread_create().

The second issue I'm having is: I noticed that if I setpriority() to some value and at a later point to a lower nice value (higher priority), the second call gives an error and the nice value remains the higher value. It seems to only allow a monotonically nonincreasing dynamic priority... This happens even if I try to set the priority of the thread from the main thread which remains with its originial higher priority of 0.

So how do I give a user account a permission to lower the nice value?
"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)
Looks like RLIMIT_NICE is 0 for regular users....
Adding entries for the soft and hard limits for nice for a given user/group in /etc/security/limits.conf solved the problem. I don't understand why in Linux one needs to edit a file which only root can modify in order to allow this, when in Windows SetThreadPriority() works for regular users with any priority level.
"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)

This topic is closed to new replies.

Advertisement