Search code examples
cpthreadsposix

How to copy pthread_attr_t?


I guess using memcpy to copy pthread_attr_t isn't a good idea as the struct seems private on both Darwin and Linux. What the proper way of copying pthread_attr_t? There isn't a copy function in pthreads. Doing get/set of all attributes is probably the safest way but it's not future proof.

This is what it looks like on my Ubuntu box, just for reference:

#define __SIZEOF_PTHREAD_ATTR_T 36

typedef union
{
    char __size[__SIZEOF_PTHREAD_ATTR_T];
    long int __align;
} pthread_attr_t;

Solution

  • Interesting question, but the answer (judging from the Rationale section of the pthread_attr_init() specification) is that you are not supposed to copy pthread_attr_t objects around. You are right that memcpy() etc are not reliable mechanisms (and what's wrong with plain old assignment anyway?).