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
  • 2943

A Multi-Threaded Search Engine In C#.

  • 2008年08月21日 09:27
  • 195KB
  • 下载

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...

翻译 Cg Program in Unity - 2.2 Transparency (2)

Premultiplied Alpha Blending  预先相乘的Alpha混合 这里还要讲一个重要的Alpha混合的变体:有些时候片段输出的颜色已经用了它的alpha值来乘以他的颜色。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Using libssh2 in a multi-threaded program/library
举报原因:
原因补充:

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