Whether you use libssh2 when it is compiled against OpenSSL or gcrypt, there's a known issue with multi-threaded programs, even if each session is correctly created and used within a single thread. Specifically, the multi-threading "glue" hooks in the support library need to be managed by the application.
The gcrypt library is documented at http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
For OpenSSL, the call is CRYPTO_set_locking_callback. An example for Win32 (lifted out of the OpenSSL source file crypto/threads/mttest.c):
/***************************/ /* t h r e a d _ s e t u p */ /***************************/ void thread_setup(void) { int i; lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE)); for (i=0; i<CRYPTO_num_locks(); i++) { lock_cs[i]=CreateMutex(NULL,FALSE,NULL); } CRYPTO_set_locking_callback((void (*)(int,int,const char *,int))win32_locking_callback); } /*******************************/ /* t h r e a d _ c l e a n u p */ /*******************************/ void thread_cleanup(void) { int i; CRYPTO_set_locking_callback(NULL); for (i=0; i<CRYPTO_num_locks(); i++) { CloseHandle(lock_cs[i]); } OPENSSL_free(lock_cs); } /***********************************************/ /* w i n 3 2 _ l o c k i n g _ c a l l b a c k */ /***********************************************/ void win32_locking_callback(int mode, int type, char *file, int line) { if (mode & CRYPTO_LOCK) { WaitForSingleObject(lock_cs[type],INFINITE); } else { ReleaseMutex(lock_cs[type]); } }