2

を使用してNSThread、スタック サイズを次のように設定しています。

thread=[[NSThread alloc]initWithTarget:self selector:@selector(fibnocciForLoop) object:nil];
[thread setStackSize:12];
[thread start];    

for のAppleドキュメントに書かれているように-[NSThread setStackSize:]

ディスカッション
スレッドを開始する前に、このメソッドを呼び出す必要があります。スレッドの開始後にスタック サイズを設定すると、属性のサイズ (stackSize メソッドによって反映される) が変更されますが、スレッド用に確保されている実際のページ数には影響しません。

しかし、後でデバッガーでスタック サイズを確認すると、設定した値が得られません。

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!

4

2 に答える 2

4

setStackSize:ドキュメントにも次のように書かれています。

レシーバーのスタック サイズ。この値はバイト単位で、4KB の倍数である必要があります。

12 は 4K の倍数ではないため、NSThread設定を無視し、おそらく 128 ページ (iOS (および OS X) のページは 4KB) または 4 MB のように見えるデフォルトを使用します。

12 が何を意味するかは明確ではありませんが、設定を使用する場合は、少なくとも 4096 バイトを示すように変更する必要がありますNSThread

于 2012-06-27T07:11:46.597 に答える
-1

リンク先のドキュメントを読んだだけで、[start] の後に[ setStackSize:12] を呼び出してみましたか?

thread=[[NSThread alloc]initWithTarget:self selector:@selector(fibnocciForLoop) object:nil];
[thread start]; 
[thread setStackSize:4096]; // multiple of 4KB

編集:わかりました、気にしないでください。ドキュメントを完全に読み間違えました。こんなことしないで。

于 2012-06-27T07:11:28.077 に答える