鲍里斯的文章向我们展示了如何创建boost :: asio的扩展.我尝试在已注册的信号上添加signal_set和async_wait.然后程序挂起,直到触发第二个SIGINT.虽然,我想在一个信号内完成它.
这是我的代码.我在Ubuntu上使用gcc-4.6.3和boost-1.52.0进行测试.
编译 -
gcc -I/boost_inc -L/boot_lib main.cpp -lpthread -lboost_system -lboost_thread
#include <boost/asio.hpp>
#include <iostream>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <cstddef>
template <typename Service>
class basic_timer
: public boost::asio::basic_io_object<Service>
{
public:
explicit basic_timer(boost::asio::io_service &io_service)
: boost::asio::basic_io_object<Service>(io_service)
{}
void wait(std::size_t seconds)
{ return this->service.wait(this->implementation, seconds); }
template <typename Handler>
void async_wait(std::size_t seconds, Handler handler)
{ this->service.async_wait(this->implementation, seconds, handler); }
};
class timer_impl;
template <typename TimerImplementation = timer_impl>
class basic_timer_service
: public …Run Code Online (Sandbox Code Playgroud) 给定一个结构 S:
struct S {
bool a : 1;
bool b : 1;
};
Run Code Online (Sandbox Code Playgroud)
如何确定S::a和S::b是在编译时位字段?
我试图提出一个类似的宏,IsBitField(S, a)但很难将 SFINAE 应用到offsetof()或addressof()(众所周知,每个位域的操作都是无效的)。
尽管cleanup属性是仅由GCC/Clang支持的扩展,但我认为它是纯C中最接近RAII的近似值.例如
#define loc_str __attribute__((cleanup(free_loc_str)))
void free_loc_str(char **str)
{ if(str && *str) free(*str); }
int main(void)
{
loc_str char *s = malloc(10);
return 0; // Great! s is freed when it exit its scope
}
Run Code Online (Sandbox Code Playgroud)
但是,该属性仅适用于自动范围但不适用于函数参数.即
void func(loc_str char *str)
{
return; // XXX - str will not be freed (compiled without any warning)
}
Run Code Online (Sandbox Code Playgroud)
我已经知道上面的情况,但是,为什么?有没有理由制造这样的限制?
- 更新 -
一个触发这个问题的完整故事:
我试图为C创建一个共享指针(或智能指针).以下是一个非线程安全和简化的代码段
struct impl_t;
struct impl_t* ctor();
void dtor(struct impl_t* inst);
struct shared_ptr_s
{
struct impl_t* inst;
int *use_cnt;
};
void free_shared(struct …Run Code Online (Sandbox Code Playgroud) 我想把以下C++代码转换成Haskell
#include <utility>
template<typename Pair>
struct inv_pair
{
typedef std::pair<
typename Pair::second_type,
typename Pair::first_type
> type;
};
Run Code Online (Sandbox Code Playgroud)
inv_pair基本上反转了对的first_type和second_type.它可以如下使用
typedef std::pair<int, std::string> pair_t1;
typedef inv_pair<pair_t1>::type inv_par_t1;
// of type std::pair<std::string, int>
Run Code Online (Sandbox Code Playgroud)
哈斯克尔
data Pair' = Pair' Int String
-- then?
Run Code Online (Sandbox Code Playgroud)
也许这不是一个有用的模式.仍然好奇并愿意学习.
c++ ×3
bit-fields ×1
boost ×1
boost-asio ×1
c ×1
gcc ×1
haskell ×1
raii ×1
sfinae ×1
type-traits ×1