小编alp*_*pha的帖子

为什么allocate_shared和make_shared这么慢

我刚刚编写了一个测试程序,以找到分配和释放许多管理对象的最快方法shared_ptr.

我试图shared_ptrnew,shared_ptrpool,make_shared,allocate_shared.是什么让我感到惊讶的allocate_shared是慢shared_ptrpool.

vs2017+win10用发布版本测试代码.发布版本设置为默认值(/ O2).我也测试它gcc4.8.5+centos6.2g++ -std=c++11 -O3.

代码是:

#include <memory>
#include <iostream>
#include <vector>
#include <assert.h>
#include <chrono>
#include <mutex>
using namespace std;

struct noncopyable {
protected:
    noncopyable() = default;
    ~noncopyable() = default;
private:
    noncopyable(const noncopyable&) = delete;
    noncopyable& operator=(const noncopyable&) = delete;
    noncopyable(noncopyable&&) = delete;
    noncopyable& operator=(noncopyable&&) = delete;
};

class BlockPool : noncopyable …
Run Code Online (Sandbox Code Playgroud)

c++ performance shared-ptr c++11

9
推荐指数
1
解决办法
713
查看次数

http2/http1.1代理如何处理Transfer-Encoding?

HTTP/2禁止特定于连接的标头字段.不得出现以下标题字段:"连接","保持活动","代理连接","传输编码"和"升级".此外,"TE"标题字段不得包含"预告片"以外的任何值.

我想问的是,由于HTTP/2禁止Transfer-Encoding标头,所以HTTP/2代理如何处理标头Tranfer-Encoding:chunked?

如果代理总是将整个chunked请求缓存在内存中并添加Content-Length头,那么发送到HTTP/2服务器/客户端?

proxy http http2

5
推荐指数
1
解决办法
2743
查看次数

带有unique_ptr捕获的asio lambda

我正在使用asio独立1.10.6和vs2015 rc.

vs2015支持unique_ptr捕获.所以我写了一些代码如下:

auto data = std::make_unique<std::string>("abc");
auto buffer = asio::buffer(data->c_str(), data->size());
asio::async_write(s, buffer, [data = std::move(data)](
  const asio::error_code& error, size_t byte_transferred) mutable {
  do_something(std::move(data), error, byte_transferred);
});
Run Code Online (Sandbox Code Playgroud)

但是当我编译代码时,编译器说:

错误C2280:....试图引用已删除的函数

据我所知,它说我试图复制lambda,因为lambda捕获a std::unique_ptr,所以它是不可复制的.

令我困惑的是为什么asio想要复制lambda而不是移动lambda.

我的代码出了什么问题?如何解决它?

=========================

完整的代码是:

void do_something(std::unique_ptr<std::string> data) { }

void compile_failed() {
  asio::io_service io_service;
  asio::ip::tcp::socket s(io_service);

  auto data = std::make_unique<std::string>("abc");
  auto buffer = asio::buffer(data->c_str(), data->size());

  asio::async_write(s, buffer, [data = std::move(data)](const asio::error_code& error,
    size_t byte_transferred) mutable {
    do_something(std::move(data));
  });
}

template<typename T > struct lambda_evil_wrap { …
Run Code Online (Sandbox Code Playgroud)

c++ lambda boost-asio unique-ptr c++11

5
推荐指数
1
解决办法
1239
查看次数

为什么c ++使用memset(addr,0,sizeof(T))来构造一个对象?标准版还是编译错误?

这个问题与我的另一篇文章有​​关:为什么allocate_shared和make_shared这么慢

在这里,我可以更清楚地描述这个问题.

考虑以下代码:

struct A {
    char data_[0x10000];
};

class C {
public:
    C() : a_() { }
    A a_;
};

int main() {
    C c;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我发现代码中C() : a_(),编译器使用A memset(addr,0,0x10000)作为构造函数.如果类型A有一个空构造函数,则asm代码是正确的.

为了更清楚地描述问题,我写了一些测试代码:

#include <stdlib.h>

struct A {
    //A() {}
    char data_[0x10000];
    void dummy() { // avoid optimize erase by compiler
        data_[rand() % sizeof(data_)] = 1;
    }
    int dummy2() { // avoid optimize erase by compiler
        return data_[0];
    }
};

class B {
public:
    template<class …
Run Code Online (Sandbox Code Playgroud)

c++ constructor perfect-forwarding c++11

5
推荐指数
1
解决办法
626
查看次数

allocate_shared 如何工作?

来自cppreference 中的 std::allocate_shared

构造一个 类型的对象T并将其包装在std::shared_ptrusing中args作为 的构造函数的参数列表T。该对象就像通过表达式构造的::new (pv) T(std::forward<Args>(args)...),其中是指向适合保存该类型的对象的存储的pv内部指针。存储空间通常大于共享指针的控制块和对象的一次分配。void*Tsizeof(T)T

所有内存分配都是使用 的副本完成的alloc,它必须满足Allocator要求。

让我困惑的是,考虑以下用户定义的分配器,也来自cppreference

template <class T>
struct Mallocator {
    typedef T value_type;
    Mallocator() = default;
    template <class U> Mallocator(const Mallocator<U>&) {}
    T* allocate(std::size_t n) { return static_cast<T*>(std::malloc(n * sizeof(T))); }
    void deallocate(T* p, std::size_t) { std::free(p); }
};
template <class T, class U>
bool operator==(const Mallocator<T>&, const Mallocator<U>&) { …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

4
推荐指数
1
解决办法
2621
查看次数

如何与clang libc ++静态链接

我刚刚coroutine在c ++ 2a中编写了一个测试代码。

我用clang 5.0构建代码:

clang++ testcoroutine.cpp -std=c++2a -I../asio_alone -fcoroutines-ts -stdlib=libc++
Run Code Online (Sandbox Code Playgroud)

该代码工作正常。

现在,我想静态链接libc ++。so,以便可以在其他PC上运行a.out,我用google搜索过,但只找到了-static-libstdc++

我不能使用,-static-libstdc++因为libstdc++不支持coroutine

如果我使用-static-libstdc ++:

clang++ testcoroutine.cpp -std=c++2a -I../asio_alone -fcoroutines-ts
-static-libstdc++ 
testcoroutine.cpp:26:10: fatal error: 'experimental/coroutine' file not found
#include <experimental/coroutine>


 ^~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated.
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?


测试代码:

#define ASIO_STANDALONE
#define ASIO_HAS_STD_CHRONO

#ifdef _WIN32
#pragma warning (disable:4819)
#pragma warning (disable:4503)
#pragma warning (disable:4996)
#pragma warning (disable:4100) // unref parameters
#define _CRT_SECURE_NO_WARNINGS
#define NOMINMAX
#define _CRT_NONSTDC_NO_DEPRECATE
#define WIN32_LEAN_AND_MEAN
#endif

#ifdef _WIN32 …
Run Code Online (Sandbox Code Playgroud)

c++ clang coroutine libc++

4
推荐指数
2
解决办法
3853
查看次数