2010-09-13 00:34:38 +04:00
|
|
|
/**
|
2010-12-05 12:53:39 +03:00
|
|
|
@page libssh_tutor_threads Chapter 8: Threads with libssh
|
2010-09-13 00:34:38 +04:00
|
|
|
@section threads_with_libssh How to use libssh with threads
|
|
|
|
|
|
|
|
libssh may be used in multithreaded applications, but under several conditions :
|
|
|
|
- Threading must be initialized during the initialization of libssh. This
|
|
|
|
initialization must be done outside of any threading context.
|
|
|
|
- If pthreads is being used by your application (or your framework's backend),
|
2011-05-27 15:26:15 +04:00
|
|
|
you must link with libssh_threads dynamic library and initialize
|
2010-09-13 00:34:38 +04:00
|
|
|
threading with the ssh_threads_pthreads threading object.
|
|
|
|
- If an other threading library is being used by your application, you must
|
|
|
|
implement all the methods of the ssh_threads_callbacks_struct structure
|
|
|
|
and initialize libssh with it.
|
|
|
|
- At all times, you may use different sessions inside threads, make parallel
|
2011-05-27 15:26:15 +04:00
|
|
|
connections, read/write on different sessions and so on. You *cannot* use a
|
|
|
|
single session (or channels for a single session) in several threads at the same
|
|
|
|
time. This will most likely lead to internal state corruption. This limitation is
|
|
|
|
being worked out and will maybe disappear later.
|
2010-09-13 00:34:38 +04:00
|
|
|
|
|
|
|
@subsection threads_init 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().
|
|
|
|
|
|
|
|
@code
|
|
|
|
#include <libssh/callbacks.h>
|
|
|
|
...
|
2011-09-17 23:32:08 +04:00
|
|
|
ssh_threads_set_callbacks(ssh_threads_get_noop());
|
2010-09-13 00:34:38 +04:00
|
|
|
ssh_init();
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
@subsection threads_pthread Using libpthread with libssh
|
|
|
|
|
|
|
|
If your application is using libpthread, you may simply use the libpthread
|
|
|
|
threading backend:
|
|
|
|
|
|
|
|
@code
|
|
|
|
#include <libssh/callbacks.h>
|
|
|
|
...
|
2011-09-17 23:32:08 +04:00
|
|
|
ssh_threads_set_callbacks(ssh_threads_get_pthread());
|
2010-09-13 00:34:38 +04:00
|
|
|
ssh_init();
|
|
|
|
@endcode
|
|
|
|
|
2011-05-27 15:26:15 +04:00
|
|
|
However, you must be sure to link with the library ssh_threads. If
|
2010-09-13 00:34:38 +04:00
|
|
|
you're using gcc, you must use the commandline
|
|
|
|
@code
|
2011-05-27 15:26:15 +04:00
|
|
|
gcc -o output input.c -lssh -lssh_threads
|
2010-09-13 00:34:38 +04:00
|
|
|
@endcode
|
|
|
|
|
|
|
|
|
|
|
|
@subsection threads_other Using another threading library
|
|
|
|
|
|
|
|
You must find your way in the ssh_threads_callbacks_struct structure. You must
|
|
|
|
implement the following methods :
|
|
|
|
- mutex_lock
|
|
|
|
- mutex_unlock
|
|
|
|
- mutex_init
|
|
|
|
- mutex_destroy
|
|
|
|
- thread_id
|
|
|
|
|
2014-01-08 19:58:49 +04:00
|
|
|
libgcrypt 1.6 and bigger backend does not support custom callback. Using anything else than pthreads (ssh_threads_get_pthread()) here will fail.
|
2010-09-13 00:34:38 +04:00
|
|
|
Good luck !
|
|
|
|
*/
|