Using libssh2 in a multi-threaded program/library

转载 2015年11月20日 15:45:11
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]);
        }
}

相关文章推荐

Multi-threaded Program on SESC

Got it here: http://ce.et.tudelft.nl/~pepijn/doc/sesc.html http://dongrui-she.net/dokuwiki/doku.p...
  • orzorz
  • orzorz
  • 2011-06-04 14:13
  • 2841

Using AfxBeginThread for multi-thread in VS C++

Summary Two tricks about multi-thread Controlling function must be global or static Multithreading m...

zz - transcript of Defining and Using Shared Resources in a Custom Control Library

the original link is http://blogs.msdn.com/b/wpfsdk/archive/2007/06/08/defining-and-using-shared-res...

MTTTY (Multi-Threaded TTY)

  • 2014-08-11 11:12
  • 124KB
  • 下载

m2eclipse报错:Unable to locate the Javac Compiler in: C:Program FilesJavajre6..libtools.jar

m2eclipe经常会报这个错,原因是对于安装了JDK的机器,会有两个jre,一个在C:/Program Files/Java/jre6下,一个在C:Program FilesJavajdk1.6.0...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)