Reputation: 144
I am using an NSThread
and setting the stack size for it as follows:
thread=[[NSThread alloc]initWithTarget:self selector:@selector(fibnocciForLoop) object:nil];
[thread setStackSize:12];
[thread start];
As written Apple docs for for -[NSThread setStackSize:]
:
Discussion
You must call this method before starting your thread. Setting the stack size after the thread has started changes the attribute size (which is reflected by the stackSize method), but it does not affect the actual number of pages set aside for the thread.
But when I check the stack size later in the debugger I don't get the value I set:
print (int)[thread stackSize]
$1 = 524288
My question is why does the setStackSize:
method exist if it does nothing?
Please let me know where I am wrong, or whether the API for setStackSize:
is not of any use?
EDIT: Refer to Answer by @Josh Caswell for this question, i missed the K in 4K , and hence the above code will work fine as under:
thread=[[NSThread alloc]initWithTarget:self
selector:@selector(fibnocciForLoop) object:nil];
[thread setStackSize:12*4096];//4K=4096
[thread start];
EXTENSION OF THIS QUESTION:
Can someone kindly explain as to why Apple gave this method setStackSize, and how and when to use this particular method, because it requires a lot of calculations for the user to calculate as to how many bytes are/will be used.
I want to know its exact purpose in NSThread?
Thanks!
Upvotes: 2
Views: 1137
Reputation: 1458
Just by reading the documentation you linked, have you tried calling [setStackSize:12] after [start]?
thread=[[NSThread alloc]initWithTarget:self selector:@selector(fibnocciForLoop) object:nil];
[thread start];
[thread setStackSize:4096]; // multiple of 4KB
Edit: Ok, nevermind. I totally read the documentation wrong. Do not do this.
Upvotes: -1
Reputation: 64022
The setStackSize:
documentation also says:
The stack size for the receiver. This value must be in bytes and a multiple of 4KB.
12 is not a multiple of 4K, so your NSThread
ignores your setting and probably uses its default, which appears to be 128 pages (a page on iOS (and OS X) being 4KB), or 4 MB.
It's not clear what you want 12 to mean, but you'll need to change it to indicate at least 4096 bytes if you want NSThread
to use your setting.
Upvotes: 4