ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I was confused by your changing the stack size from 15000 to 20000. At first I thought you meant that the stack size for a thread was already 15000 and you were changing it. But no, apparently you're setting it to 20000 before creating the thread. So what does it mean to change the stack size "from" 15000? Does it mean that 15000 is the default stack size?
Whatever the answer to that question is, on the Linux system I'm running, Debian 5.0.4, where uname -a yields
Code:
Linux tiger 2.6.26-2-686 #1 SMP Sat Dec 26 09:01:51 UTC 2009 i686 GNU/Linux
I find that:
The default stack size is 8388608.
The minimum stack size is 16384, so even trying to set the stack size to 15000 should fail.
Setting the stack size to 20000 works just fine.
As a demo, I take a program which creates two threads and compares the address of a particular variable in one thread's stack with the address of the same variable in another thread's stack. This is a quick and dirty measurement of the stack size. I run the program with the default stack size, with a stack size of 15000, and a stack size of 20000. The output of the program is this:
Code:
=== pthread_attr_setstacksize() is implemented on this system.
minimum stack size is 16384 00004000
default stack size is 8388608 00800000
using default stack size
difference is 8392704
=== pthread_attr_setstacksize() is implemented on this system.
minimum stack size is 16384 00004000
default stack size is 8388608 00800000
going for stack size 15000 00003A98
Houston, we have a problem on line 113
the problem: Invalid argument
=== pthread_attr_setstacksize() is implemented on this system.
minimum stack size is 16384 00004000
default stack size is 8388608 00800000
going for stack size 20000 00004E20
new stack size is 20000 00004E20
difference is 20480
Maybe we can get a handle on your situation by running the program the same way I did. You can do that by running the following shell script. Would you be so kind as to do so and post the results in this thread?
Code:
rm -f 1; cat > 1.c <<EOD; gcc -pthread -Wall -Werror 1.c -o 1; ./1; ./1 15000; ./1 20000
#include <bits/local_lim.h>
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/*--------------------------------------------------------------------------*/
/*
* x_stage gets 0 initially. The subsidiary thread sets it to 1 to indicate
* that the variable pointer has been set. The main thread sets it to 2 to
* indicate that the thread may exit.
*/
typedef struct x_s
{
int x_thread_number;
pthread_t x_thread_id;
pthread_mutex_t x_mutex;
pthread_cond_t x_cond;
int x_stage;
char *x_variable_pointer;
} x_t,
*x_p;
/*--------------------------------------------------------------------------*/
void
check(int line_number,int expression)
{
if(expression)
{
fprintf(stderr,
"Houston, we have a problem on line %d\n",
line_number
);
errno=expression;
perror("the problem");
exit(1);
}
} /* check() */
/*--------------------------------------------------------------------------*/
void *
doit(void *xx)
{
char something;
((x_p)xx)->x_variable_pointer=&something;
check(__LINE__,pthread_mutex_lock(&((x_p)xx)->x_mutex));
((x_p)xx)->x_stage=1;
check(__LINE__,pthread_cond_signal(&((x_p)xx)->x_cond));
check(__LINE__,pthread_mutex_unlock(&((x_p)xx)->x_mutex));
/* We've set the pointer. Wait for permission to finish. */
check(__LINE__,pthread_mutex_lock(&((x_p)xx)->x_mutex));
while(((x_p)xx)->x_stage<2)
{
check(__LINE__,pthread_cond_wait(&((x_p)xx)->x_cond,
&((x_p)xx)->x_mutex
)
);
}
check(__LINE__,pthread_mutex_unlock(&((x_p)xx)->x_mutex));
return xx;
} /* doit() */
/*--------------------------------------------------------------------------*/
int
main(int argc,
char **argv
)
{
#if defined(_POSIX_THREAD_ATTR_STACKSIZE)
long desired_size;
size_t stack_size;
pthread_attr_t attributes;
x_t fred;
x_t barney;
if(argc>1)
{
desired_size=strtol(argv[1],NULL,0);
}
printf("=== pthread_attr_setstacksize() is implemented on this system.\n");
printf("minimum stack size is %8d %08X\n",PTHREAD_STACK_MIN,PTHREAD_STACK_MIN);
check(__LINE__,pthread_attr_init(&attributes));
check(__LINE__,pthread_attr_getstacksize(&attributes,&stack_size));
printf("default stack size is %8d %08X\n",stack_size,stack_size);
if(argc>1)
{
printf("going for stack size %8ld %08lX\n",desired_size,desired_size);
check(__LINE__,pthread_attr_setstacksize(&attributes,desired_size));
check(__LINE__,pthread_attr_getstacksize(&attributes,&stack_size));
printf("new stack size is %8d %08X\n",stack_size,stack_size);
}
else
{
printf("using default stack size\n");
}
fred .x_thread_number=1;
barney.x_thread_number=2;
check(__LINE__,pthread_mutex_init(&fred .x_mutex,NULL));
check(__LINE__,pthread_mutex_init(&barney.x_mutex,NULL));
check(__LINE__,pthread_cond_init (&fred .x_cond, NULL));
check(__LINE__,pthread_cond_init (&barney.x_cond, NULL));
fred .x_stage=0;
barney.x_stage=0;
check(__LINE__,pthread_create(&fred .x_thread_id,&attributes,doit,&fred ));
check(__LINE__,pthread_create(&barney.x_thread_id,&attributes,doit,&barney));
/* Wait for both threads to have written the pointer. */
check(__LINE__,pthread_mutex_lock(&fred.x_mutex));
while(fred.x_stage<1)
{
check(__LINE__,pthread_cond_wait(&fred.x_cond,&fred.x_mutex));
}
check(__LINE__,pthread_mutex_unlock(&fred.x_mutex));
check(__LINE__,pthread_mutex_lock(&barney.x_mutex));
while(barney.x_stage<1)
{
check(__LINE__,pthread_cond_wait(&barney.x_cond,&barney.x_mutex));
}
check(__LINE__,pthread_mutex_unlock(&barney.x_mutex));
/* Let the threads finish. */
check(__LINE__,pthread_mutex_lock(&fred.x_mutex));
fred.x_stage=2;
check(__LINE__,pthread_cond_signal(&fred.x_cond));
check(__LINE__,pthread_mutex_unlock(&fred.x_mutex));
check(__LINE__,pthread_join(fred.x_thread_id,NULL));
check(__LINE__,pthread_mutex_lock(&barney.x_mutex));
barney.x_stage=2;
check(__LINE__,pthread_cond_signal(&barney.x_cond));
check(__LINE__,pthread_mutex_unlock(&barney.x_mutex));
check(__LINE__,pthread_join(barney.x_thread_id,NULL));
printf("difference is %8d\n",
fred.x_variable_pointer-barney.x_variable_pointer
);
#else
printf("=== pthread_attr_setstacksize() is not implemented on this system.\n");
#endif
return 0;
} /* main() */
EOD
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106
Original Poster
Rep:
Your code works great.... I'm looking back at my code to figure out what the differences are. Thanks for your help.
I figured out what my problem is.
Code:
if ( 0 != threadRet)
//should be:
if ( -1 != threadRet)
Even though the properties come back wrong my code still allows it to use the m_attributes to attempt to create the thread. It must still have worked using the default because the thread is still created.
Thanks for your help again.
Unfortunately this old problem isn't my current one. I was hoping that this was my current issue. I haven't posted that issue at all yet and it is also a segmentation fault that I can't see because of my gdb issues.
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106
Original Poster
Rep:
Wow I'm really glad that you replied to that because I used a bad source as a reference or I didn't understand the differences of what ibm is talking about at a first glance.
I did a search for "C++ pthread_attr_setstacksize" on google and got this as the first site hit.
It says the return value of it is -1 in a fail case.
Well, heck fire. It shore do! What you've stumbled across there is a man page describing behavior of IBM's product z/OS, which isn't UNIX or Linux. Read about it here.
In fairness to IBM, they do seem to have some sort of UNIX compatibility mode, because on that same man page you reference, it also says this:
Quote:
Special Behavior for Single UNIX Specification, Version 3:
If unsuccessful, pthread_attr_setstacksize() returns an error number to indicate the error.
You're not running z/OS, are you? If you are, my apologies.
And thanks for sharing the link. I didn't even know about z/OS until now, although I hail from the days of OS/360 (older than OS/390, not quite as old as archtoad6), when your main choices for programming (at least in our shop at the University of California at Irvine) were assembly language or FORTRAN.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.