Skip to content

Instantly share code, notes, and snippets.

@ales-erjavec
Created July 18, 2018 11:43
Show Gist options
  • Save ales-erjavec/fab620ee5752b7bb1849d8ecd658afb4 to your computer and use it in GitHub Desktop.
Save ales-erjavec/fab620ee5752b7bb1849d8ecd658afb4 to your computer and use it in GitHub Desktop.
Get the default stack size of new pthread threads.
"""
Get the default stack size of new pthread threads
"""
import ctypes, ctypes.util
libc = ctypes.CDLL(ctypes.util.find_library("c"))
pthread_attr_getstacksize = libc.pthread_attr_getstacksize
pthread_attr_getstacksize.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_size_t)]
pthread_attr_getstacksize.restype = ctypes.c_int
pthread_self = libc.pthread_self
pthread_self.restype = ctypes.c_void_p
pthread_attr_init = libc.pthread_attr_init
pthread_attr_init.restype = ctypes.c_int
pthread_attr_init.argtypes = [ctypes.c_void_p]
pthread_attr_destroy = libc.pthread_attr_destroy
pthread_attr_destroy.argtypes = [ctypes.c_void_p]
pthread_attr_destroy.restype = ctypes.c_int
attr = (ctypes.c_char * 1024)() # buffer with probably sufficient size
if pthread_attr_init(attr):
raise OSError(ctypes.get_errno())
size = ctypes.c_size_t(0)
status = pthread_attr_getstacksize(
attr, ctypes.pointer(size)
)
print("Status:", status, ", size:", size.value)
pthread_attr_destroy(attr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment