Subject: Using libssh2_sftp_* functions in multi-threading environment

Using libssh2_sftp_* functions in multi-threading environment

From: Yoichiro Tanaka <>
Date: Mon, 26 Jan 2015 20:49:02 +0900

Hi there,

I'm developing my application to access SFTP server with libssh2. And, I
want to give an ability to access the SFTP server from multi-threads to the
application, for example, downloading some files simultaneously.

I have some questions about how to use libssh2_sftp_* functions.

(1) Are "libssh2_sftp_*" functions thread-safe?

(2) If yes, what is a value to issue for each thread? Do I need to issue a
sftp_session value issued by the libssh2_sftp_init() function for each
thread? Or, Can I share the sftp_session value with all threads? That is,
do I need to issue a sftp_handle value issued by the libssh2_sftp_open(dir)
for each thread with the same value of the sftp_session?

(3) If a lock (ex. mutex) is necessary, where I should apply the lock
against my code? From the libssh2_sftp_init() calling to the reading file
with the all libssh2_sftp_read() calling? Or, each API calling?

(4) Is there any sample code to use libssh2 in the multi-threading

Thank you for your advice.

Yoichiro Tanaka

Received on 2015-01-26