y***@sina.cn
2012-12-03 05:00:12 UTC
hi£¬all,
The io_service::strand class provides the ability to post and dispatch handlers with the guarantee that none of those handlers will execute concurrently.
But the implementation of call_stack::context::~context lets me very confused ,
and the code is as follows:
~context()
{
call_stack<Key, Value>::top_ = next_;
}
My question is how to ensure that the object to be destroyed is top of the call_stack elements.
To illustrate this situation, here is my sample code:
#include <iostream>
#include <boost/asio/detail/call_stack.hpp>
using namespace std;
using namespace boost::asio::detail;
class A
{};
typedef call_stack<A>::context Acontext;
void printcallback(){
cout<<"start print:"<<endl;
Acontext* elem=boost::asio::detail::call_stack<A>::top_;
while (elem){
cout<<elem->key_ <<endl;
elem = elem->next_;
}
cout<<"end print"<<endl<<endl;
}
int main(int argc, char* argv[])
{
A a,b,c;
Acontext* pctxa=new Acontext(&a);
Acontext* pctxb=new Acontext(&b);
Acontext* pctxc=new Acontext(&c);
printcallback();
delete pctxb;//it's not the top of call_stack
printcallback();
return 0;
}
(ps: In order to access the member variables of call_stack,I made changes to the call_stack.hpp )
the output is :
start print:
0x7fffa26cdcf5
0x7fffa26cdcf6
0x7fffa26cdcf7
end print
start print:
0x7fffa26cdcf7
end print
in this case,the pctxc is lost , how to deal with this situation ?
The io_service::strand class provides the ability to post and dispatch handlers with the guarantee that none of those handlers will execute concurrently.
But the implementation of call_stack::context::~context lets me very confused ,
and the code is as follows:
~context()
{
call_stack<Key, Value>::top_ = next_;
}
My question is how to ensure that the object to be destroyed is top of the call_stack elements.
To illustrate this situation, here is my sample code:
#include <iostream>
#include <boost/asio/detail/call_stack.hpp>
using namespace std;
using namespace boost::asio::detail;
class A
{};
typedef call_stack<A>::context Acontext;
void printcallback(){
cout<<"start print:"<<endl;
Acontext* elem=boost::asio::detail::call_stack<A>::top_;
while (elem){
cout<<elem->key_ <<endl;
elem = elem->next_;
}
cout<<"end print"<<endl<<endl;
}
int main(int argc, char* argv[])
{
A a,b,c;
Acontext* pctxa=new Acontext(&a);
Acontext* pctxb=new Acontext(&b);
Acontext* pctxc=new Acontext(&c);
printcallback();
delete pctxb;//it's not the top of call_stack
printcallback();
return 0;
}
(ps: In order to access the member variables of call_stack,I made changes to the call_stack.hpp )
the output is :
start print:
0x7fffa26cdcf5
0x7fffa26cdcf6
0x7fffa26cdcf7
end print
start print:
0x7fffa26cdcf7
end print
in this case,the pctxc is lost , how to deal with this situation ?