[ACCEPTED]-pthread_key_t and pthread_once_t?-unix

Accepted answer
Score: 13

pthread_key_t is for creating thread thread-local storage: each thread gets 15 its own copy of a data variable, instead 14 of all threads sharing a global (or function-static, class-static) variable. The 13 TLS is indexed by a key. See pthread_getspecific et al for 12 more details.

pthread_once_t is a control for executing 11 a function only once with pthread_once. Suppose you 10 have to call an initialization routine, but 9 you must only call that routine once. Furthermore, the 8 point at which you must call it is after 7 you've already started up multiple threads. One 6 way to do this would be to use pthread_once(), which guarantees 5 that your routine will only be called once, no 4 matter how many threads try to call it at 3 once, so long as you use the same control 2 variable in each call. It's often easier 1 to use pthread_once() than it is to use other alternatives.

Score: 12

No, it can't be explained in layman terms. Laymen 39 cannot successfully program with pthreads 38 in C++. It takes a specialist known as a 37 "computer programmer" :-)

pthread_once_t is a little bit 36 of storage which pthread_once must access in order to 35 ensure that it does what it says on the 34 tin. Each once control will allow an init 33 routine to be called once, and once only, no 32 matter how many times it is called from 31 how many threads, possibly concurrently. Normally 30 you use a different once control for each 29 object you're planning to initialise on 28 demand in a thread-safe way. You can think 27 of it in effect as an integer which is accessed 26 atomically as a flag whether a thread has 25 been selected to do the init. But since 24 pthread_once is blocking, I guess there's allowed to 23 be a bit more to it than that if the implementation 22 can cram in a synchronisation primitive 21 too (the only time I ever implemented pthread_once, I 20 couldn't, so the once control took any of 19 3 states (start, initialising, finished). But 18 then I couldn't change the kernel. Unusual 17 situation).

pthread_key_t is like an index for accessing 16 thread-local storage. You can think of each 15 thread as having a map from keys to values. When 14 you add a new entry to TLS, pthread_key_create chooses a key 13 for it and writes that key into the location 12 you specify. You then use that key from 11 any thread, whenever you want to set or 10 retrieve the value of that TLS item for 9 the current thread. The reason TLS gives 8 you a key instead of letting you choose 7 one, is so that unrelated libraries can 6 use TLS, without having to co-operate to 5 avoid both using the same value and trashing 4 each others' TLS data. The pthread library 3 might for example keep a global counter, and 2 assign key 0 for the first time pthread_key_create is called, 1 1 for the second, and so on.

Score: 2

Wow, the other answers here are way too 5 verbose.

pthread_once_t stores state for pthread_once(). Calling pthread_once(&s, fn) calls 4 fn and sets the value pointed to by s to record 3 the fact it has been executed. All subsequent 2 calls to pthread_once() are noops. The name should become 1 obvious now.

pthread_once_t should be initialized to PTHREAD_ONCE_INIT.

More Related questions