5

Digging around the internet for a few hours I could not find an answer to my question.

My model is this: I have one shared boost::asio::ssl::context that is being used to create instances of boost::asio::ssl::stream. Then stream objects are passed along to different threads.

Is this model thread safe? Can same boost::asio::ssl::context be used in construction of different SSL stream objects that will be concurrently used?

Please, before marking this issue as a duplicate, consider reading description carefully. Thank you!

I my specific case, no objects are shared betweed threads. So anything said in Boost.Asio SSL thread safety thread safety does not affect my case. I access boost::asio::ssl::context from a single thread.

Community
  • 1
  • 1
GreenScape
  • 7,191
  • 2
  • 34
  • 64

1 Answers1

4

Because SSL contexts needs to be shared among SSL sessions (otherwise, how would session resumption work) the SSL context class is fully, internally thread safe. You can use an SSL context in multiple SSL connections and from multiple threads however you want.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • but don't forget to initialise libssl properly! – Richard Hodges Nov 04 '15 at 10:45
  • Thank you very much for your help! This is an assertion I needed. Intuitively I though that this should be possible and it works. But when something works it does not mean it's correct and may break on another machine. – GreenScape Nov 04 '15 at 11:09
  • @RichardHodges I believe `boost` should initialize its SSL backend internally. And it does so, as far as I know. – GreenScape Nov 04 '15 at 11:10
  • @GreenScape This is how everyone else does it. There's really no other way. – David Schwartz Nov 04 '15 at 11:15
  • @GreenScape just had a look at the source. you're right. openssl is initialised on first instansiation of the `asio::ssl::context_service` – Richard Hodges Nov 04 '15 at 11:19