Last active
April 16, 2025 21:29
-
-
Save phmongeau/4f8e83f514170be5c29b9ac47a7b4030 to your computer and use it in GitHub Desktop.
unloading shared lib with thread_local variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "lib.h" | |
| #include <stdio.h> | |
| class TestObject { | |
| public: | |
| float value = 0.2f; | |
| TestObject() { | |
| printf("construct TestObject\n"); | |
| } | |
| ~TestObject() { | |
| printf("destruct TestObject\n"); | |
| } | |
| }; | |
| // this should probably be an array [MAX_THREAD_COUNT] | |
| static TestObject *g = 0; | |
| extern "C" void load_lib() { | |
| static thread_local TestObject a; | |
| g = &a; | |
| printf("load_lib: %f\n", a.value); | |
| } | |
| extern "C" void unload_lib() { | |
| if (g != 0) { | |
| delete g; | |
| g = 0; | |
| } | |
| printf("unload_lib\n"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern "C" void load_lib(); | |
| extern "C" void unload_lib(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <dlfcn.h> | |
| int main(int argc, char **argv) { | |
| printf("test\n"); | |
| void (*load_lib)(void); | |
| void (*unload_lib)(void); | |
| void *handle = dlopen("./lib.so", RTLD_NOW | RTLD_GLOBAL); | |
| if (handle == 0) { | |
| } | |
| load_lib = (void (*)(void))dlsym(handle, "load_lib"); | |
| unload_lib = (void (*)(void))dlsym(handle, "unload_lib"); | |
| if (load_lib == 0 || unload_lib == 0) { | |
| printf("failed to load symbols from lib.so\n"); | |
| return 1; | |
| } | |
| load_lib(); | |
| unload_lib(); | |
| load_lib = 0; | |
| if (dlclose(handle) != 0) { | |
| printf("failed to dlclose\n"); | |
| } | |
| handle = dlopen("./lib.so", RTLD_NOW | RTLD_GLOBAL); | |
| if (handle == 0) { | |
| } | |
| load_lib = (void (*)(void))dlsym(handle, "load_lib"); | |
| load_lib(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| main: main.cpp lib.h lib.so | |
| g++ -o main main.cpp -ldl -ggdb3 -fno-gnu-unique | |
| lib.so: lib.cpp lib.h | |
| g++ -shared -o lib.so -fpic lib.cpp -ftls-model=local-dynamic -fno-gnu-unique |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment