This is a demonstration of how a DLL's atexit handler is called on FreeLibrary if Microsoft's CRT is used.
Mixing Cygwin and Microsoft's CRT does not seem to work well. However according to Cygwin it is possible:
https://cygwin.com/faq/faq.html#faq.programming.msvs-mingw
https://github.com/openunix/cygwin/blob/master/winsup/testsuite/winsup.api/cygload.cc
I tried a program linked to Microsoft's CRT and a DLL linked to cygwin's CRT and the atexit handlers weren't called.
If you use a program and DLL that both use Microsoft's CRT (doesn't have to be the same version) or both use Cygwin's CRT then the test should PASS:
>>> main
libfoo was loaded.
>>> libfoo_init
<<< libfoo_init
libfoo was initialized.
>>> libfoo atexit handler
<<< libfoo atexit handler
libfoo was unloaded.
PASS: libfoo's atexit handler ran before main end.
<<< main
>>> main atexit handler
<<< main atexit handler
Other references: