在多线程应用程序中使用OpenSSL [英] Using OpenSSL in a multi-threaded application

查看:321
本文介绍了在多线程应用程序中使用OpenSSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu上用C ++写了一个SOAP客户端应用程序,使用OpenSSL作为我的HTTPS传输和pthreads线程。我有一些线程 - 一个中央数据采集线程,它周期性地获取工作线程通过共享的互斥保护队列发出SOAP请求。

I have been writing a SOAP client application in C++ on Ubuntu using OpenSSL for my HTTPS transport and pthreads for threading. I have a number of threads - one central data acquisition thread which periodically gets worker threads to make SOAP requests via shared mutex protected queues.

阅读OpenSSL的文档我发现 OpenSSL线程安全吗? OpenSSL常见问题,描述了在使用OpenSSL时确保线程安全所需的机制。我实现了这一切,所有的作品都很好。

Reading the documentation for OpenSSL I found Is OpenSSL thread-safe? in the OpenSSL FAQ which describes the mechanisms required to ensure thread safety when using OpenSSL. I implemented this and all works fine.

我的问题的原因是一个概念上的困难真的。我正在考虑实现相同的功能,我的应用程序已经,但不是使用线程,我会创建2个单独的应用程序:一个用于工作线程(其中多个副本将运行)用于主数据采集线程。然后我将使用TCP套接字在两个而不是互斥保护队列之间通信。这可能是一个坏主意,但这不是真的很重要 - 令人困惑的是我必须实现相同的功能,以确保OpenSSL线程安全在第二种方法或不?

The reason for my question is a conceptual difficulty really. I was thinking about implementing the same functionality that my application has already, but instead of using threads, I would create 2 seperate applications : One for the worker thread (of which multiple copies would be running) and another for the main data acquisition thread. I would then use TCP sockets to communicate between the two rather than mutex protected queues. This may be a bad idea but that is not really important - what is confusing me is would I have to implement the same functions required to ensure OpenSSL thread safety in this second approach or not?

我的猜测是,我不会,他们可以被视为独立的(确实必须是如此多的应用程序使用OpenSSL),但是什么原因? 使用共享库代码和多个线程共享相同代码的多个应用程序之间有什么区别我已经成功编写了多线程应用程序

My guess is that I would not have to and they could be treated as independent (indeed surely it must be as so many applications use OpenSSL) but what is the reason for it?? What is different between multiple applications using shared library code and multiple threads sharing the same code?? I have been writing multithreaded applications successfully for a couple of years now and it concerns me that I cannot come up with an answer to this question.

推荐答案

不同的是,当多个 共享相同的库代码,它们也共享相同的全局数据结构;当多个进程共享库代码时,它们不会。

The difference is that when multiple threads share the same library code, they also share the same global data structures; when multiple processes share that library code, they don't.

例如,如果OpenSSL中有很多预先计算的表,OpenSSL中的许多加密算法都会更快。该表由多个调用相同OpenSSL函数的线程共享,但必须使用锁定,以确保只有一个线程在首次使用时尝试初始化表。

For example, many of the cryptographic algorithms in OpenSSL are faster if they have a large precalculated table available. This table is shared between multiple threads calling the same OpenSSL functions, but locking must be used to ensure that only one thread attempts to initialise the table on first use.

另一个例如,许多OpenSSL函数在内部访问随机数生成器,但是这是一个全局数据结构,必须通过线程同步它的访问。

As another example, many OpenSSL functions internally access the random number generator, but the state of this is a global data structure that must have its accessed synchronised across threads.

这篇关于在多线程应用程序中使用OpenSSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆