我可以用两种方式初始化boost :: format变量:
第一:
void foo()
{
boost::format format{"Hellow %1% %2%"};
format % "Dear" % "user";
// do something with format
}
Run Code Online (Sandbox Code Playgroud)
第二种方式:
void foo()
{
boost::format v = boost::format {"Hellow %1% %2%"} % "Dear" % "user";
// do something with format
}
Run Code Online (Sandbox Code Playgroud)
我想知道,两者是否更有效.
谢谢.
bool Connection::Receive(){
std::vector<uint8_t> buf(1000);
boost::asio::async_read(socket_,boost::asio::buffer(buf,1000),
boost::bind(&Connection::handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
int rcvlen=buf.size();
ByteBuffer b((std::shared_ptr<uint8_t>)buf.data(),rcvlen);
if(rcvlen <= 0){
buf.clear();
return false;
}
OnReceived(b);
buf.clear();
return true;
}
Run Code Online (Sandbox Code Playgroud)
该方法工作正常,但仅当我在其中创建断点时才有效。等待接收的时间是否有问题?没有断点,什么也收不到。
可以在Boost线程中执行以下操作:
std::string key;
MyClass value;
myThread->setData(key, value);
MyClass retrievedValue = myThread->getData(key);
Run Code Online (Sandbox Code Playgroud)
setData并且getData只是想象的方法来解释我的需要.
编辑: @SLaks我得到了必要的答案.但是为了澄清:问题的目的不是"方法的方法"等价,而是它是一个全局等价物,我们可以通过某种方式设置线程中的某个位置,我们可以通过某种方式将其置于其他地方同一个线程...... :)
谢谢!
我必须实现Boost线程间通信。考虑以下代码:
#include <boost/thread/thread.hpp>
#include <Windows.h>
void threadA()
{
while(true)
{
std::cout << "From thread A" << std::endl;
Sleep(3000); //pretend to do the work
}
}
void threadB()
{
while(true)
{
std::cout << "From thread B" << std::endl;
Sleep(3000); //pretend to do the work
}
}
int main()
{
boost::thread *th1 = new boost::thread(&threadA);
boost::thread *th2 = new boost::thread(&threadB);
th1->join();
th2->join();
delete th1;
delete th2;
}
Run Code Online (Sandbox Code Playgroud)
如果我运行上面的代码,它将生成两个线程。我想要做的是启动,threadA然后向发送一些消息threadB,该消息在接收时将启动线程。或更笼统地说,如果这两个线程都独立运行,该如何处理通信?
我一直在使用 boost object_pool 一段时间,并且对结果总体上感到满意。以前我主要是分配单个对象,但很少单独释放它们,只是一次释放整个池。最近,当我遇到需要从池中释放许多对象时,我发现它非常慢。
显然 pool 正在搜索已发布的块列表以链接新发布的对象。该文档讨论了有序和无序池,并提到了 pool_allocator 和 fast_pool_allocator。无序池(使用 fast_memory_allocator)在释放内存块方面可能会快得多。但是,我看不到如何使用它。
我是否正确理解我只能在 pool_allocator 和 fast_pool_allocator 之间选择与 boost::singleton_pool 而不是 boost::object_pool ?
下面是一个说明问题的小测试程序。它是用 VS2013 和 boost 1_57_0 构建的。测试在对象池中分配 n 个对象,然后随机释放 10%。它有一些粗略的计时工具,表明对于 n == 100,000,分配需要 0.004 秒,而释放需要 0.4 秒。同时,对于 n == 1,000,000,在我的机器上分配需要 0.022 秒,释放需要 42 秒。
#include <boost/pool/object_pool.hpp>
#include "time.h"
#include <vector>
#include <random>
struct foo {
int data[10];
};
struct test {
test(unsigned n) : size{ n } {}
void run();
float elapsedSec(clock_t& watch);
unsigned size;
boost::object_pool<foo> _pool;
float mallocSec;
float freeSec; …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
标题:
class Counter
{
public:
Conuter(const std::string& fileName);
boost::uint16_t getCounter();
private:
tbb::atomic<boost::uint32_t> counter;
std::string counterFileName;
};
Run Code Online (Sandbox Code Playgroud)
cpp:
Counter::Counter(const std::string& fileName) : counter(), counterFileName(fileName)
{
std::string line;
std::ifstream counterFile (fileName.c_str());
if (counterFile.is_open())
{
getline (counterFile, line);
counterFile.close();
}
boost::uint32_t temp = std::stoul (line,nullptr,0);
counter = temp;
}
boost::uint32_t Counter::getCounter()
{
if (counter > 1000)
{
counter = 0;
}
assert( counter < 1000);
const boost::uint32_t ret = counter++;
if ((counter % 10) == 0)
{
// write the counter back …Run Code Online (Sandbox Code Playgroud) 我在PHP和带有Boost的C ++中都具有以下实现。它只是将文件读取为字符串,将其用空格分隔(我希望能够选择此字符),然后在具有20万个以空格分隔的随机数(称为“空格”)的文件上运行:
在PHP中:
<?php
$a = explode(" ", file_get_contents("spaces"));
echo "Count: ".count($a)."\n";
foreach ($a as $b) {
echo $b."\n";
}
Run Code Online (Sandbox Code Playgroud)
在C ++中:
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
using namespace boost;
using namespace std;
int main(int argc, char* argv[])
{
// ifstream ifs("spaces");
// string s ((istreambuf_iterator<char>(ifs)), (istreambuf_iterator<char>()));
char * buffer = 0;
long length;
string filename = "spaces";
FILE * f = fopen (filename.c_str(), "rb");
if (f)
{
fseek (f, 0, …Run Code Online (Sandbox Code Playgroud) 我正在尝试编译boost计时器,它正在抛出我不明白的错误.它让我觉得计时器库已被打破:
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/timer/timer.hpp>
int main(int argc, char **argv) {
// auto_cpu_timer t;
std::cout << boost::lexical_cast<std::string>(2.0) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果没有#include for timer.hpp,它就会编译.有了它,它会引发以下错误:
Invoking: GCC C++ Linker
g++ -Lsrc -o "timetest" ./src/main.o
./src/main.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [timetest] Error 1
Run Code Online (Sandbox Code Playgroud)
这是否意味着计时器库被破坏了?我正在使用Boost 1.49.0.
谢谢!
我应该如何得到它,由SVN?我可以使用Eclipse及其Subversive Team Provider来管理我的更改吗?然后,如果我做了一些改变,我怎么能承诺,谁决定我的变化是友好和足够的?老实说,我想尝试为某些图书馆做出贡献,因为有助于我学习C++细节的动力.
我搜索并阅读了一些关于Boost的内容,但我认为它太复杂而不能成为一个起点.
那么你能提供一个开始并成为开源库贡献者的途径吗?
在尝试编译gnu绘图示例时,g ++编译器抱怨它无法找到位于/ usr/include/boost中的boost库.
准确地说:
致命错误:提升:没有这样的文件或目录#include
我用过这个命令
g++ -l /usr/include/boost -Wall -std=c++11 -lboost_iostreams exampleplot.cpp -o exampleplot.out
g++ -I /usr/include/boost/* -Wall -std=c++11 -lboost_iostreams exampleplot.cpp -o exampleplot.out
这里也试过我的包括:
#include <iostream>
#include <cstring>
#include <vector>
#include<cmath>
#include<algorithm>
#include "gnuplot-iostream.h"
#include <boost>
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我有什么问题,并建议如何解决?
boost ×10
c++ ×10
boost-asio ×1
c++11 ×1
g++ ×1
linux ×1
performance ×1
php ×1
tbb ×1
timer ×1