- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用各种 Boost 函数开发一个事件驱动的应用程序。我一直在计划将我的多线程架构基于 ASIO 的 io_service 对象,由此每个工作原子将由 1 个或多个线程的线程组分派(dispatch),每个线程都调用了 run()
。 .
该引擎的早期“概念验证”版本使用单个 io_service
对象调度许多不同的任务,包括截止时间计时器、网络 I/O 和发布的操作。由于这些早期再现有时可能如此,所以这个早期版本一次调度的事件不会超过几个。在确信我走在正确的轨道上之后,我重构了引擎的一部分以支持更精细的粒度和更高的可扩展性。但是这个新版本正在崩溃,因为 io_service
中似乎有一个错误的指针。目的。
我将尝试针对我遇到的问题开发一个简化且可重现的测试用例。但在我这样做之前,我想确认我的架构基于的假设......
单io_service
对象可以在大量网络对象之间共享——tcp 和 udp 解析、套接字、计时器和任何其他需要 io_service
的野兽对象引用。
我问的原因是我无法在文档或在线讨论中找到明确说明这一点。另一个提示我的 io_service 有问题的是我在调用 tcp::socket 的 async_connect()
的下游某处遇到的崩溃。具有有效的端点和处理程序。最后一行执行async_connect()
电话 this->get_service()
. stream_socket_service 的指针 get_service()
应该返回最终为 0x2,自 ENIAC 以来这一直不是一个很好的指针值。
我的环境……
我已经尝试使用 Boost 版本 48 到 52 调试这个问题。
我在 OSX 上开发并尝试了从 4.2 到 4.7.3 的各种 gcc 4.x 编译器版本。
我在这个损坏问题之前的 session 中完成的异步操作包括一些计时器、一个 udp 解析和一个 tcp 解析。
我正在做的套接字async_connect()
on 在调用之前在堆中分配,并在其构造函数中传递给 io_service。
我有一个 io_service::work
对象。
我还没有使用 strands。
这些信息是否足以让任何人提供帮助,或者我是否需要提交一段可编译的代码?我也喜欢 io_service
的入门读物服务是,我敢肯定,其他 SO 读者也是如此。
更新#1:这是我遇到的问题的最小特征,我已经确认它仍然会崩溃。我在最新的 osx ML 上使用 Boost 1.52.0、gcc 4.6.3 构建它。
#include <stdlib.h>
#include <string>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/asio.hpp>
namespace foo {
namespace asio = boost::asio;
class ios_threads : private boost::noncopyable
{
public:
ios_threads(bool strt = false)
: work_(new asio::io_service::work(ios_))
{
if (strt)
start();
}
static asio::io_service &ios()
{
return ios_;
}
void start()
{
threads_.create_thread(boost::bind(&ios_threads::run, this));
}
void wait()
{
threads_.join_all();
}
private:
void run()
{
while (true) {
try {
ios_.run();
}
catch (std::exception &e) {
delete work_;
break;
}
}
printf("Shutting down.\n");
}
static asio::io_service ios_;
asio::io_service::work *work_;
boost::thread_group threads_;
};
asio::io_service ios_threads::ios_;
struct op;
typedef op * opPtr;
struct op
{
typedef boost::recursive_mutex mutex_type;
op(op *del)
: delegate_(del)
{
}
virtual ~op()
{
}
bool start_async()
{
boost::unique_lock< mutex_type > lock(mutex_);
return start_it();
}
protected:
virtual bool start_it()
{
return false;
}
virtual void did_it(const boost::system::error_code& error)
{
}
void completion_handler(const boost::system::error_code& error)
{
boost::unique_lock< mutex_type > lock(mutex_);
did_it(error);
}
opPtr delegate_;
mutable mutex_type mutex_;
};
struct interface_search : public op
{
typedef op super;
interface_search(op *del)
: super(del),
udp_resolver_(ios_threads::ios())
{
it_ = NULL;
}
bool start_it()
{
try {
std::string hostname = boost::asio::ip::host_name();
asio::ip::udp::resolver::query query(hostname, "", asio::ip::resolver_query_base::numeric_service | boost::asio::ip::resolver_query_base::passive);
udp_resolver_.async_resolve(query, boost::bind(&interface_search::udp_handler, this, asio::placeholders::error, asio::placeholders::iterator));
}
catch (std::exception& e) {
printf("UDP resolve operation failed. Exception: %s", e.what());
}
return super::start_it();
}
protected:
void udp_handler(const boost::system::error_code& error, asio::ip::udp::resolver::iterator it)
{
it_ = ⁢
completion_handler(error);
}
void did_it(const boost::system::error_code& error)
{
if (error == asio::error::operation_aborted)
return;
op *del = delegate_;
if (del)
del->start_async();
}
asio::ip::udp::resolver udp_resolver_;
asio::ip::udp::resolver::iterator *it_;
};
struct google_connect : public op
{
typedef op super;
google_connect()
: super(NULL),
socket_(ios_threads::ios())
{
}
void endpoint(asio::ip::tcp::endpoint &endpoint)
{
endpoint_ = endpoint;
}
bool start_it()
{
try {
// Crashes in the following call!
socket_.async_connect(endpoint_, boost::bind(&google_connect::connect_handler, this, asio::placeholders::error));
}
catch (std::exception& e) {
printf(e.what());
}
return super::start_it();
}
void connect_handler(const boost::system::error_code& error)
{
completion_handler(error);
}
void did_it(const boost::system::error_code& error)
{
if (error == asio::error::operation_aborted)
return;
boost::asio::ip::address addr = socket_.local_endpoint().address();
printf(addr.to_string().c_str());
}
asio::ip::tcp::socket socket_;
asio::ip::tcp::endpoint endpoint_;
};
struct google_resolve : public op
{
typedef op super;
google_resolve()
: super(new google_connect()),
resolver_(ios_threads::ios())
{
it_ = NULL;
}
bool start_it()
{
try {
asio::ip::tcp::resolver::query query(asio::ip::tcp::v4(), "google.com", "http");
resolver_.async_resolve(query, boost::bind(&google_resolve::tcp_handler, this, asio::placeholders::error, asio::placeholders::iterator));
}
catch (std::exception& e) {
printf(e.what());
}
return super::start_it();
}
protected:
void tcp_handler(const boost::system::error_code& error, asio::ip::tcp::resolver::iterator it)
{
it_ = ⁢
completion_handler(error);
}
void did_it(const boost::system::error_code& error)
{
if (error == asio::error::operation_aborted)
return;
asio::ip::tcp::resolver::iterator last;
if (*it_ != last) {
google_connect *gc = static_cast< google_connect * >(delegate_);
if (gc) {
asio::ip::tcp::endpoint ep = **it_;
gc->endpoint(ep);
gc->start_async();
super::did_it(error);
}
}
}
asio::ip::tcp::resolver resolver_;
asio::ip::tcp::resolver::iterator *it_;
};
} // namespace foo
int main(int argc, const char * argv[])
{
try {
foo::ios_threads threads(false);
foo::opPtr ops_;
ops_ = new foo::interface_search(
new foo::google_resolve()
);
ops_->start_async();
threads.start();
threads.wait();
}
catch (std::exception& e) {
printf(e.what());
}
return 0;
}
最佳答案
为了其他人的利益,我将自己回答这个问题。
我所描述的问题的原因是,在从 gcc 4.2 过渡到 gcc 4.6.3 并启用 c++0x 语言支持后,我需要链接我在构建编译器时构建的 libstdc++ 库。运行时错误不再发生,因为我正在链接正确的运行时库。
关于c++ - 来自 boost asio io_service get_service() 的错误指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13925465/
我刚接触 C 语言几周,所以对它还很陌生。 我见过这样的事情 * (variable-name) = -* (variable-name) 在讲义中,但它到底会做什么?它会否定所指向的值吗? 最佳答案
我有一个指向内存地址的void 指针。然后,我做 int 指针 = void 指针 float 指针 = void 指针 然后,取消引用它们以获取值。 { int x = 25; vo
我正在与计算机控制的泵进行一些串行端口通信,我用来通信的 createfile 函数需要将 com 端口名称解析为 wchar_t 指针。 我也在使用 QT 创建一个表单并获取 com 端口名称作为
#include "stdio.h" #include "malloc.h" int main() { char*x=(char*)malloc(1024); *(x+2)=3; --
#include #include main() { int an_int; void *void_pointer = &an_int; double *double_ptr = void
对于每个时间步长,我都有一个二维矩阵 a[ix][iz],ix 从 0 到 nx-1 和 iz 从 0 到 nz-1。 为了组装所有时间步长的矩阵,我定义了一个长度为 nx*nz*nt 的 3D 指针
我有一个函数,它接受一个指向 char ** 的指针并用字符串填充它(我猜是一个字符串数组)。 *list_of_strings* 在函数内部分配内存。 char * *list_of_strings
我试图了解当涉及到字符和字符串时,内存分配是如何工作的。 我知道声明的数组的名称就像指向数组第一个元素的指针,但该数组将驻留在内存的堆栈中。 另一方面,当我们想要使用内存堆时,我们使用 malloc,
我有一个 C 语言的 .DLL 文件。该 DLL 中所有函数所需的主要结构具有以下形式。 typedef struct { char *snsAccessID; char *
我得到了以下数组: let arr = [ { children: [ { children: [], current: tru
#include int main(void) { int i; int *ptr = (int *) malloc(5 * sizeof(int)); for (i=0;
我正在编写一个程序,它接受一个三位数整数并将其分成两个整数。 224 将变为 220 和 4。 114 将变为 110 和 4。 基本上,您可以使用模数来完成。我写了我认为应该工作的东西,编译器一直说
好吧,我对 C++ 很陌生,我确定这个问题已经在某个地方得到了回答,而且也很简单,但我似乎找不到答案.... 我有一个自定义数组类,我将其用作练习来尝试了解其工作原理,其定义如下: 标题: class
1) this 指针与其他指针有何不同?据我了解,指针指向堆中的内存。如果有指向它们的指针,这是否意味着对象总是在堆中构造? 2)我们可以在 move 构造函数或 move 赋值中窃取this指针吗?
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: C : pointer to struct in the struct definition 在我的初学者类
我有两个指向指针的结构指针 typedef struct Square { ... ... }Square; Square **s1; //Representing 2D array of say,
变量在内存中是如何定位的?我有这个代码 int w=1; int x=1; int y=1; int z=1; int main(int argc, char** argv) { printf
#include #include main() { char *q[]={"black","white","red"}; printf("%s",*q+3); getch()
我在“C”类中有以下函数 class C { template void Func1(int x); template void Func2(int x); }; template void
我在64位linux下使用c++,编译器(g++)也是64位的。当我打印某个变量的地址时,例如一个整数,它应该打印一个 64 位整数,但实际上它打印了一个 48 位整数。 int i; cout <<
我是一名优秀的程序员,十分优秀!