Pthread problems, 32bit vs 64bit

I have an application which builds and executes without error on a 32bit implementation of Linux. When I transferred the code to a new project on a 64bit implementation, the code will build without error, but the pthread functions, such as pthread_attr_setschedparam() return an 'Invalid Argument' error. Can anyone shed some light on where I have gone wrong?

Without seeing source code, it is difficult to provide a definitive answer. However, the fact that pthread_attr_setschedparam() is returning EINVAL in 64-bit but works in 32-bit leads me to suspect that one or more of your structures has a programming model dependancy. For example in the IPL32 programming model an int is the same as a long (32 bits) but in an LP64 programming model an int is no longer the same size as a long (an int is 32 bits but a long is 64 bits.)

I have figured out the problem which comes from not totally understanding why I cannot simply call pthread_att_init() to initialize the thread attr data structure, but must call pthread_attr_setschedpolicy(). A snippet of the code follows, the code that failed is highlighted in RED:

err = pthread_attr_init(&thread_attr);
if (err)
fprintf(stderr, "pthread_attr_init() error, %s\n", strerror(err));

thread_param.sched_priority = 2;
err = pthread_attr_setschedparam(&thread_attr, &thread_param);
if (err)
fprintf(stderr, "pthread_attr_setschedparam() error, %s\n", strerror(err));

By adding the lines in BLUE, the code would then operate properly:

err = pthread_attr_init(&thread_attr);
if (err)
fprintf(stderr, "pthread_attr_init() error, %s\n", strerror(err));

err = pthread_attr_setschedpolicy(&thread_attr, SCHED_OTHER);
if (err)
fprintf(stderr, "pthread_attr_setschedpolicy() error, %s\n", strerror(err));

thread_param.sched_priority = 2;
err = pthread_attr_setschedparam(&thread_attr, &thread_param);
if (err)
fprintf(stderr, "pthread_attr_setschedparam() error, %s\n", strerror(err));

Shouldn't the ptread_attr_init() have taken care of the function of pthread_attr_setSchedpolicy() since all elements within the structure should have been set to their default state? In any event, thanks for the help.

According to POSIX.1:2008 the pthread_attr_init() function "shall initialize a thread attributes object attr with the default value for all of the individual attributes used by a given implementation."

However the specification does not specify a specific default value for either schedpolicy or schedparm which means that each implementation of Pthreads chooses some appropriate value. Thus, unless you are aware of the a specific implementation's defaults, if you set one value you should set the other. As a precaution and to ensure my code is portable, I always set both schedpolicy and schedparam if I am adjusting the scheduling policy.