libssh  0.7.0
Chapter 8: Threads with libssh

How to use libssh with threads

libssh may be used in multithreaded applications, but under several conditions :

Initialization of threads

To initialize threading, you must first select the threading model you want to use, using ssh_threads_set_callbacks(), then call ssh_init().

#include <libssh/callbacks.h>
...
ssh_threads_set_callbacks(ssh_threads_get_noop());

ssh_threads_noop is the threading structure that does nothing. It's the threading callbacks being used by default when you're not using threading.

Using libpthread with libssh

If your application is using libpthread, you may simply use the libpthread threading backend:

#include <libssh/callbacks.h>
...
ssh_threads_set_callbacks(ssh_threads_get_pthread());

However, you must be sure to link with the library ssh_threads. If you're using gcc, you must use the commandline

gcc -o output input.c -lssh -lssh_threads

Using another threading library

You must find your way in the ssh_threads_callbacks_struct structure. You must implement the following methods :

libgcrypt 1.6 and bigger backend does not support custom callback. Using anything else than pthreads (ssh_threads_get_pthread()) here will fail. Good luck !