// gcc -g -O0 -o main ./tls_main.c -L. -lthreadfunc -ldl -lpthread #include #include #include #include #include #include typedef void* (*thread_func_t)(void*); int main() { pthread_t thread; void* handle; thread_func_t thread_func; handle = dlopen("./libthreadfunc.so", RTLD_LAZY); if (!handle) { fprintf(stderr, "Failed to load libthreadfunc.so: %s\n", dlerror()); exit(EXIT_FAILURE); } dlerror(); thread_func = (thread_func_t)dlsym(handle, "thread_func"); const char* dlsym_error = dlerror(); if (dlsym_error) { fprintf(stderr, "Failed to find symbol thread_func: %s\n", dlsym_error); dlclose(handle); exit(EXIT_FAILURE); } while (1) { if (pthread_create(&thread, NULL, thread_func, NULL)!= 0) { perror("Failed to create thread"); dlclose(handle); exit(EXIT_FAILURE); } sleep(1); } dlclose(handle); return 0; }