<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <meta name="generator" content="Doxygen 1.8.3.1"/> <title>libssh: Chapter 8: Threads with libssh</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="dynsections.js"></script> <link href="doxygen.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td style="padding-left: 0.5em;"> <div id="projectname">libssh  <span id="projectnumber">0.5.5</span> </div> </td> </tr> </tbody> </table> </div> <!-- end header part --> <!-- Generated by Doxygen 1.8.3.1 --> <div id="navrow1" class="tabs"> <ul class="tablist"> <li><a href="index.html"><span>Main Page</span></a></li> <li class="current"><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="files.html"><span>Files</span></a></li> </ul> </div> <div id="nav-path" class="navpath"> <ul> <li class="navelem"><a class="el" href="index.html">index</a></li><li class="navelem"><a class="el" href="libssh_tutorial.html">The Tutorial</a></li> </ul> </div> </div><!-- top --> <div class="header"> <div class="headertitle"> <div class="title">Chapter 8: Threads with libssh </div> </div> </div><!--header--> <div class="contents"> <div class="textblock"><h1><a class="anchor" id="threads_with_libssh"></a> How to use libssh with threads</h1> <p>libssh may be used in multithreaded applications, but under several conditions :</p> <ul> <li>Threading must be initialized during the initialization of libssh. This initialization must be done outside of any threading context.</li> <li>If pthreads is being used by your application (or your framework's backend), you must link with libssh_threads dynamic library and initialize threading with the ssh_threads_pthreads threading object.</li> <li>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.</li> <li>At all times, you may use different sessions inside threads, make parallel connections, read/write on different sessions and so on. You <em>cannot</em> 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.</li> </ul> <h2><a class="anchor" id="threads_init"></a> Initialization of threads</h2> <p>To initialize threading, you must first select the threading model you want to use, using <a class="el" href="group__libssh__threads.html#gaf473af3fbae6f1bb0e249fd1d079ff68" title="sets the thread callbacks necessary if your program is using libssh in a multithreaded fashion...">ssh_threads_set_callbacks()</a>, then call <a class="el" href="group__libssh.html#ga3ebf8d6920e563f3b032e3cd5277598e" title="Initialize global cryptographic data structures.">ssh_init()</a>.</p> <div class="fragment"><div class="line"><span class="preprocessor">#include <libssh/callbacks.h></span></div> <div class="line">...</div> <div class="line">ssh_threads_set_callbacks(<a class="code" href="group__libssh__threads.html#ga10fa1f7243c94524b734a96963818c5b" title="returns a pointer on the noop threads callbacks, to be used with ssh_threads_set_callbacks.">ssh_threads_get_noop</a>());</div> <div class="line"><a class="code" href="group__libssh.html#ga3ebf8d6920e563f3b032e3cd5277598e" title="Initialize global cryptographic data structures.">ssh_init</a>();</div> </div><!-- fragment --><p>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.</p> <h2><a class="anchor" id="threads_pthread"></a> Using libpthread with libssh</h2> <p>If your application is using libpthread, you may simply use the libpthread threading backend:</p> <div class="fragment"><div class="line"><span class="preprocessor">#include <libssh/callbacks.h></span></div> <div class="line">...</div> <div class="line">ssh_threads_set_callbacks(ssh_threads_get_pthread());</div> <div class="line"><a class="code" href="group__libssh.html#ga3ebf8d6920e563f3b032e3cd5277598e" title="Initialize global cryptographic data structures.">ssh_init</a>();</div> </div><!-- fragment --><p>However, you must be sure to link with the library ssh_threads. If you're using gcc, you must use the commandline </p> <div class="fragment"><div class="line">gcc -o output input.c -lssh -lssh_threads</div> </div><!-- fragment --><h2><a class="anchor" id="threads_other"></a> Using another threading library</h2> <p>You must find your way in the ssh_threads_callbacks_struct structure. You must implement the following methods :</p> <ul> <li>mutex_lock</li> <li>mutex_unlock</li> <li>mutex_init</li> <li>mutex_destroy</li> <li>thread_id</li> </ul> <p>Good luck ! </p> </div></div><!-- contents --> <!-- start footer part --> <hr class="footer"/><address class="footer"><small> Generated by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/> </a> 1.8.3.1 </small></address> </body> </html>