小编san*_*ank的帖子

使用boost :: singleton_pool时处理std :: string/std :: vector成员变量

我正在编写一个性能关键型应用程序,我在其中创建大量类似类型的对象来下订单.我使用boost :: singleton_pool来分配内存.最后我的班级看起来像这样.

    class MyOrder{
    std::vector<int> v1_;
    std::vector<double> v2_;

    std::string s1_;
    std::string s2_;

public:
    MyOrder(const std::string &s1, const std::string &s2): s1_(s1), s2_(s2) {}

    ~MyOrder(){}

    static void * operator new(size_t size); 
    static void operator delete(void * rawMemory) throw();
    static void operator delete(void * rawMemory, std::size_t size) throw();

};

struct MyOrderTag{};
typedef boost::singleton_pool<MyOrderTag, sizeof(MyOrder)> MyOrderPool; 

void* MyOrder:: operator new(size_t size)
{
    if (size != sizeof(MyOrder)) 
        return ::operator new(size);

    while(true){
        void * ptr = MyOrderPool::malloc();
        if (ptr != NULL) return ptr; …
Run Code Online (Sandbox Code Playgroud)

c++ memory-management boost-pool

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

将整数流汇总为Java中的长变量

我有一个Java Set,其中包含一些Integer元素.我想用Java 8流来汇总它的元素.

Set<Integer> numbers = new HashSet<>();
// Some code that will populate numbers
int sum = numbers.stream().mapToInt(Integer::intValue).sum() //Can overflow!
Run Code Online (Sandbox Code Playgroud)

我可以用上面的代码来获得的总和,但内容numbersInteger元素远低于Integer.MAX_VALUE并且有大量的人,使得它们的总和可能溢出.如何将Integer元素流转换为元素流Long并将其安全地求和?

java java-8 java-stream

8
推荐指数
1
解决办法
799
查看次数

使用boost :: asio :: read_async读取Protobuf对象

我正在使用Boost asio编写一个应用程序,其中客户端和服务器交换使用google proto-buffers序列化的消息.我不知道通过网络发送的序列化消息的大小是多少.似乎proto-buf对象没有任何分隔符.

以下是.proto文件的内容.

 package tutorial;

message Person {
        required string name = 1;
        required int32 id = 2;
        optional string email = 3;
}
Run Code Online (Sandbox Code Playgroud)

这是我从服务器写的方式

        tutorial::Person p;
        p.set_name("abcd pqrs");
        p.set_id(123456);
        p.set_email("abcdpqrs@gmail.com");
        write(p);

        boost::asio::streambuf b;
        std::ostream os(&b);
        p.SerializeToOstream(&os);
        boost::asio::async_write(socket_, b,
                        boost::bind(&Server::handle_write, this,
                                boost::asio::placeholders::error));
Run Code Online (Sandbox Code Playgroud)

在客户端我正在使用boost :: asio :: async_read读取上面发送的消息.如何在下面的代码中找出arg要设置为参数的值boost::asio::transfer_at_least

 boost::asio::async_read(socket_, response_,
                            boost::asio::transfer_at_least(arg),
                            boost::bind(&Client::handle_read_header, this,
                                    boost::asio::placeholders::error));
Run Code Online (Sandbox Code Playgroud)

或者,如何在读取整个对象后确保boost :: async_read返回?

c++ protocol-buffers boost-asio

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

使用boost singleton_pool的自定义分配比默认值慢

我为MyOrder类编写了自定义运算符new和operator delete.我使用boost :: singleton pool分配内存.这是测试性能的程序,

#include <boost/pool/singleton_pool.hpp>
#include <boost/progress.hpp>
#include <iostream>
#include <new>
#include <vector>


class MyOrder{
    std::vector<int> v1_;
    std::vector<double> v2_;

    std::string s1_;
    std::string s2_;

public:
    MyOrder(std::string s1, std::string s2): s1_(s1), s2_(s2) {}

    ~MyOrder(){}

    static void * operator new(size_t size); 
    static void operator delete(void * rawMemory) throw();
};

struct MyOrderTag{};
typedef boost::singleton_pool<MyOrderTag, sizeof(MyOrder)> MyOrderPool; 

void* MyOrder:: operator new(size_t size)
{
    if (size != sizeof(MyOrder)) 
        return ::operator new(size);

    while(true){
        void * ptr = MyOrderPool::malloc();
        if (ptr != NULL) return ptr;

        std::new_handler …
Run Code Online (Sandbox Code Playgroud)

c++ memory-management memory-pool boost-pool

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

Unable to include cmath using GCC 5.5 on Solaris 10

I am trying to run the following test program on my Solaris 10 sparc machine using gcc 5.5.0

#include <iostream>
#include <cmath>

int main()
{
    std::cout << "exp2(4) = " << std::exp2(4) << '\n'
              << "exp2(0.5) = " << std::exp2(0.5) << '\n'
              << "exp2(-4) = " << std::exp2(-4) << '\n';
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Here are the OS details,

~$ uname -a
SunOS sovms577 5.10 Generic_147147-26 sun4v sparc SUNW,SPARC-Enterprise-T5220
~$ cat /etc/release 
                   Oracle Solaris 10 1/13 s10s_u11wos_24a SPARC
  Copyright (c) 1983, …
Run Code Online (Sandbox Code Playgroud)

c++ gcc solaris solaris-10

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

为什么要捕获一个const int和一个int&work?

在下面的代码中,我抛出一个int,将其作为const int&捕获,重新抛出它并再次捕获它将其作为int&捕获.

#include <iostream>

int main()
{
    try
    {
        try
        {
            int x = 1;
            throw x;
        }
        catch(const int& e)
        {
            std::cout << "Inner catch" << std::endl;
            throw;
        }
    }
    catch(int & e1)
    {
        std::cout << "Outer catch" << std::endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上述程序编译成功并打印

Inner catch
Outer catch
Run Code Online (Sandbox Code Playgroud)

另一方面,下面的程序,我试图初始化一个int和一个const int,甚至不会编译.

#include <iostream>
int main()
{
    int x = 0;
    const int& y = x;
    int& z = y 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我按预期得到以下错误

binding ‘const int’ to reference of …
Run Code Online (Sandbox Code Playgroud)

c++ const exception

5
推荐指数
2
解决办法
273
查看次数

boost :: unordered_map中的迭代器失效

我使用boost::unordered_map如下

typedef boost::shared_ptr<WriterExeciter> PtrWriter;
typedef std::list<PtrWriter> PtrList; 
boost::unordered_map<std::pair<unsigned int, unsigned long long>, PtrList>  Map
Map instrMap;
Run Code Online (Sandbox Code Playgroud)

现在我正在对PtrList循环中的类型列表进行一些更改

for(auto it = instrMap.begin(); it != instrMap.end(); ++it)
{
     auto key = it->first();
     auto list& = it->second();    
     //Make some change to an element in list 

      if(list.empty())
      {
            instMap.erase(key); 
      }



}
Run Code Online (Sandbox Code Playgroud)
  1. 对列表进行更改是否会使instrMap的迭代器失效?

  2. 擦除元素将使指向擦除元素的迭代器无效.如何修改我的代码,以便这不会导致任何问题?使用it++而不是++it帮助?

谢谢

c++ boost unordered-map

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

提升ASIO并发性

我正在学习使用Boost ASIO.以下是从Boost ASIO文档中提供的聊天示例中复制的一些代码,

typedef std::deque<chat_message> chat_message_queue;

class chat_client
{
    public:
        chat_client(boost::asio::io_service& io_service,
                tcp::resolver::iterator endpoint_iterator)
            : io_service_(io_service),
            socket_(io_service)
    {
        boost::asio::async_connect(socket_, endpoint_iterator,
                boost::bind(&chat_client::handle_connect, this,
                    boost::asio::placeholders::error));
    }

        void write(const chat_message& msg)
        {
            io_service_.post(boost::bind(&chat_client::do_write, this, msg));
        }

        void close()
        {
            io_service_.post(boost::bind(&chat_client::do_close, this));
        }

    private:

        void handle_connect(const boost::system::error_code& error)
        {
            //Implementation
        }

        void handle_read_header(const boost::system::error_code& error)
        {
            //Implementation
        }

        void handle_read_body(const boost::system::error_code& error)
        {
            //Implementation
        }

        void do_write(chat_message msg)
        {
            bool write_in_progress = !write_msgs_.empty();
            write_msgs_.push_back(msg);
            if (!write_in_progress)
            {
                boost::asio::async_write(socket_,
                        boost::asio::buffer(write_msgs_.front().data(),
                            write_msgs_.front().length()),
                        boost::bind(&chat_client::handle_write, this, …
Run Code Online (Sandbox Code Playgroud)

c++ concurrency boost boost-asio

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

使用流填充Java Map,该Java Map映射与其字母位置对应的字母表

我正在填充一个Map<Character, Integer> alphabetToNumber映射与其字母位置对应的字母

char [] alphabetArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
Map<Character, Integer> alphabetToNumber = new HashMap<>();
int counter = 1;
for(Character letter : alphabetArray) {
    alphabetToNumber.put(letter, counter);
    counter++;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码有效,但我想知道是否可以使用Java 8流完成.谢谢.

java java-8 java-stream

3
推荐指数
1
解决办法
207
查看次数

在派生类构造函数中抛出异常.为什么要调用基类析构函数而不是派生类析构函数?

#include <iostream>
class A
{
    public:
    A() 
    {   
        std::cout << "A()" << std::endl;
    }   

 virtual  ~A()
    {   
        std::cout << "~A()" << std::endl;
    }   
};

class B:public A
{
    public:
    B() 
    {   
        throw std::exception();
        std::cout << "B()" << std::endl;
    }   

   ~B()
    {   
        std::cout << "~B()" << std::endl;
    }   
};


int main()
{
    try 
    {   
        B b;
    }   
    catch(std::exception & e)  
    {   
    }   
    return 0;
}   
Run Code Online (Sandbox Code Playgroud)

上面的代码输出,

A()
~A()
Run Code Online (Sandbox Code Playgroud)

抛出异常时,已创建B.那么为什么B的析构函数不被称为?

c++ inheritance exception

2
推荐指数
1
解决办法
589
查看次数

C++ Single Producer多个消费者程序偶尔崩溃

在下面的代码中,我正在创建一个producer threadn consumer threads从每个专用queue和打印到的读取stdout.这段代码有时会在声明中崩溃consumerQueues[id]->empty().通过调试去我看到consumerQueues[id]0x0当它崩溃.现在在init()函数中,我在创建worker 之前创建了ith使用者.我不确定为什么会留下来.请帮我弄清楚发生了什么.queueiththreadconsumerQueues[id]0x0

#include <thread>
#include <queue>
#include <memory>
#include <iostream>
#include <mutex>
#include <condition_variable>

class Test
{
private:
    void producer()
    {
        while(true)
        {
            std::string s = "abc";
            for(const auto& q : consumerQueues)
            {
                std::unique_lock<std::mutex> lock(mutex);
                q->push(s);
                condition_variable.notify_all();
            }
        }
    }

    void consumer(int id)
    {
        while (true)
        {
            std::string job;
            {
                std::unique_lock<std::mutex> lock(mutex);
                while(consumerQueues[id]->empty())
                { …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading producer-consumer race-condition

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