1829281 Members
10554 Online
109989 Solutions
New Discussion

PTHREAD stack size

 
SOLVED
Go to solution
meghana9490
Occasional Advisor

PTHREAD stack size

Hi , 

I have written the setting the size code foe less than the PTHREAD_STACK_MIN and greater than PTHREAD_STACK_MIN, I want to write the if our local stack size is equal to PTHREAD_STACK_MIN, but I had stuck here , can you please help me on how to write the logic for last condition

s = pthread_attr_init(&attr);
if (s != 0) {
FYI(FYI_FATAL, "%s pthread_attr_init failed %ld\n",__FUNCTION__,s);
}
if (StackSize < PTHREAD_STACK_MIN) {
FYI(FYI_INFORM, "Stack size is less than PTHREAD_STACK_MIN, hardcoding to PTHREAD_STACK_MIN\n" );
StackSize = PTHREAD_STACK_MIN;
} else if (StackSize > PTHREAD_STACK_MIN) {
s = pthread_attr_setstacksize(&attr, StackSize);
if (s != 0) {
FYI(FYI_FATAL, "%s pthread_attr_setstacksize failed %ld\n", __FUNCTION__, s);
}
}

meghanagujjeti
5 REPLIES 5
Vinky_99
Esteemed Contributor

Re: PTHREAD stack size

Hello @meghana9490 

 

To handle the case where the local stack size is equal to PTHREAD_STACK_MIN, you can add an extra condition in your code to check if StackSize is equal to PTHREAD_STACK_MIN. If it is, you can simply skip the call to pthread_attr_setstacksize() and use the default stack size provided by the system.

Here's how you can modify your code to handle this case:

s = pthread_attr_init(&attr);
if (s != 0) {
FYI(FYI_FATAL, "%s pthread_attr_init failed %ld\n", __FUNCTION__, s);
}

if (StackSize < PTHREAD_STACK_MIN) {
FYI(FYI_INFORM, "Stack size is less than PTHREAD_STACK_MIN, hardcoding to PTHREAD_STACK_MIN\n" );
StackSize = PTHREAD_STACK_MIN;
} else if (StackSize > PTHREAD_STACK_MIN) {
s = pthread_attr_setstacksize(&attr, StackSize);
if (s != 0) {
FYI(FYI_FATAL, "%s pthread_attr_setstacksize failed %ld\n", __FUNCTION__, s);
}
} else {
// StackSize is equal to PTHREAD_STACK_MIN, use default stack size
FYI(FYI_INFORM, "Stack size is equal to PTHREAD_STACK_MIN, using default stack size\n");
}

 

In this modified code, the 'else' block handles the case where StackSize is equal to PTHREAD_STACK_MIN. You can add any additional logic or print statements inside this block as per your requirement.

 

NOTE: Am not an expert, I may go wrong. Take a back-up of the data, before you perform any steps. You can give a try on your own risk. 

These are my opinions so use it at your own risk.
meghana9490
Occasional Advisor

Re: PTHREAD stack size

Hi @Vinky_99 

I have changed as you said, can you please look into this

s = pthread_attr_init(&attr);
if (s != 0) {
FYI(FYI_FATAL, "%s pthread_attr_init failed %ld\n",__FUNCTION__,s);
exit(1);
}
if (pthread_attr_getstacksize(&attr, &StackSize) != 0) {
StackSize = PTHREAD_STACK_MIN;
}

/* Check if the stacksize is large enough */
if (StackSize < PTHREAD_STACK_MIN) {
FYI(FYI_INFORM, "Stack size is less than PTHREAD_STACK_MIN, hardcoding to PTHREAD_STACK_MIN\n" );
StackSize = PTHREAD_STACK_MIN;
s = pthread_attr_setstacksize(&attr, StackSize);
}
/* Set stacksize and try to reinitialize attr if failed */
if (StackSize >= PTHREAD_STACK_MIN && pthread_attr_setstacksize(&attr, StackSize) != 0 && pthread_attr_init(&attr)) {
FYI(FYI_INFORM, "can't set log thread stack size\n");
exit(1);
}

 

meghanagujjeti
Vinky_99
Esteemed Contributor
Solution

Re: PTHREAD stack size

@meghana9490 

On the modified code you provided, you have added an additional check to retrieve the current stack size using pthread_attr_getstacksize() function before setting the stack size. This is a good practice to ensure that you are working with a valid stack size.

The code also handles the case where the requested stack size is less than PTHREAD_STACK_MIN by hardcoding it to PTHREAD_STACK_MIN and setting the stack size using pthread_attr_setstacksize().

In the else block, you are attempting to set the stack size using pthread_attr_setstacksize() and then reinitializing the attribute using pthread_attr_init() if it fails. However, the check condition is incorrect, as pthread_attr_init() returns 0 on success and non-zero on error. So, you should use if (StackSize >= PTHREAD_STACK_MIN && pthread_attr_setstacksize(&attr, StackSize) != 0 && pthread_attr_init(&attr) != 0) instead.

Overall, the modified code looks good and should work as intended.

 

Good Luck!

These are my opinions so use it at your own risk.
meghana9490
Occasional Advisor

Re: PTHREAD stack size

Hi @Vinky_99 

Thank you so much for your solution 

meghanagujjeti
Sunitha_Mod
Honored Contributor

Re: PTHREAD stack size

Hello @meghana9490

That's great! 

We are glad to know your concern has been addressed.