开发者

How to get current thread's stack size via C API on Mac OS X? (no Cocoa or carbon)

开发者 https://www.devze.com 2023-02-06 17:04 出处:网络
In Unix, there\'s getrusage function to get the resource status. It is in Mac OS X too, but it does not work. There\'s pre-implemented API on Cocoa(I don\'t know about Carbon),

In Unix, there's getrusage function to get the resource status. It is in Mac OS X too, but it does not work. There's pre-implemented API on Cocoa(I don't know about Carbon),

-[NSthread stackSize开发者_如何转开发]

but I have to implement it with only C API.

What's the equivalent C API with that?


You have to do this before creating the thread, by initializing a pthread_attr_t object and calling pthread_attr_getstacksize on it. If you know the current thread was created with the default attributes, you could probably defer the test until later, but it sounds like a bad idea.

With that said, I think testing the default is rather useless anyway. If you know you need a large stack, explicitly set a large stack size before creating the thread. If you know you only need a small stack, explicitly set that and enjoy the memory savings. I would recommend not going any lower than 32k unless you know you won't be using any library functions or recursion (in which case 4k is probably fine), nor any larger than 256k unless you intend to do some deep recursion. A rough estimate I'd use for getting a safe stack size for recursion is:

levels_of_recursion * (128 + total_size_of_locals) + (using_lib ? 32768 : 4096)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号