小编doo*_*pav的帖子

我是否需要在 OpenSSL 1.1.0+ 中使用 CRYPTO 锁定函数来保证线程安全?

这个问题涉及 OpenSSL 1.1.0+。在我使用的代码示例中std::string_view,这意味着C++17. 这不是必需的,任何事情C++11都可以,我只是懒得将 const char* bufstd::size_t len作为单独的变量。

#include <string_view>

#include <openssl/err.h>
#include <openssl/ssl.h>

void startup()
{
    SSL_library_init();
    SSL_load_error_strings();
    OpenSSL_add_ssl_algorithms();
    ERR_load_crypto_strings();
}

void shutdown()
{
    ERR_free_strings();
    EVP_cleanup();
}

void thread_shutdown()
{
    CRYPTO_cleanup_all_ex_data();
}

void run_per_thread()
{
    // intial non SSL stuff
    int sockfd = get_connected_socket();
    std::string_view hostname = get_hostname();
    std::string_view buffer = get_buffer();
    // SSL context setup
    auto ssl_ctx = SSL_CTX_new(TLS_client_method());
    auto ssl_ctx_options = SSL_OP_SINGLE_DH_USE || SSL_OP_NO_SSLv3;
    SSL_CTX_set_options(ssl_ctx, ssl_ctx_options);
    SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading openssl thread-safety

6
推荐指数
1
解决办法
1131
查看次数

如何在编译时将同构元组转换为数组?

我想首先说明,当我说同构元组时,我的意思是每个元素的类型都是相同的,或者每个元素都有一个公共成员,该成员在所有元素中都是相同的类型。在这种情况下,公共成员也必须是static constexpr

这是描述我的问题的一段简化代码:

#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>

template <std::size_t N>
struct Int : std::integral_constant<std::size_t, N>
{
    Int() = default;
    Int(const Int&) = delete;
    Int(Int&&) = delete;

    Int& operator=(const Int&) = delete;
    Int& operator=(Int&&) = delete;
};

template <std::size_t... Ns>
struct Stuff
{
    using type = std::tuple<Int<Ns>...>;
    using arr_type = std::array<std::size_t, std::tuple_size_v<type>>;
    static constexpr arr_type arr = {Ns...};
};
Run Code Online (Sandbox Code Playgroud)

我相当确定它是正确的,唯一的问题是最后一行,但希望你明白要点。

现在,数组是使用Ns.... 我希望能够从而type不是构造数组(原因是type已经从元组中删除了重复类型(基于.value),并且还在.value此代码的实际版本中对类型进行了排序(基于))。如果相反,在编译时从数组中排序和删除重复值更容易,那么这也将被接受作为答案。

所以理想情况下,我会编写某种模板化结构来创建数组(因为在结构中会有一个static constexpr …

c++ compile-time variadic-templates stdtuple c++17

0
推荐指数
1
解决办法
159
查看次数