|
Boost Users : |
Subject: Re: [Boost-users] Asio::Async_read_some - bytes_transferred=0!
From: Lloyd (lloyd_at_[hidden])
Date: 2010-06-28 10:05:01
----- Original Message -----
From: "Igor R" <boost.lists_at_[hidden]>
To: <boost-users_at_[hidden]>
Sent: Monday, June 28, 2010 6:59 PM
Subject: Re: [Boost-users] Asio::Async_read_some - bytes_transferred=0!
>> Yes, you are right, the thread is exiting before the async_read_some.
> You mean "before async_read_some completion"? At least on Windows,this
> would cause the following behavior:"Note All I/O initiated by a given
> thread is canceled when thatthread exits. For overlapped sockets, pending
> asynchronous operationscan fail if the thread is closed before the
> operations complete.
> "http://msdn.microsoft.com/en-us/library/ms741688(VS.85).aspx
>
>> I would like to let you know why I am using threads. This is a server>
>> application. The client can give any command, and the server will
>> execute> it. Some commands may take much longer time. In this kind of
>> situations, the> client may want to STOP the execution of that long job.
>> With the help of> threading I am able to do this with the help
>> "Interruption points and> interrupts". The server will receive a "STOP
>> command with the thread ID"> from the client, So the server can interrupt
>> the thread and stop the job.>> Is there a better way to implement this? I
>> would be very happy to know> that, than using this dirty way I am using
>> now...
> Well, you can launch threads for your tasks -- still processing allthe i/o
> in one thread (where you call io_service::run()):
> // pseudo code// gotCommand is always being called from within a single
> thread,where io_service::run() is runningvoid gotCommand(Command cmd){
> Task task = makeTask(cmd);
> launchThread(task);}_______________________________________________Boost-users
> mailing
> listBoost-users_at_[hidden]http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
I think I am doing it in a very similar manner as you have mentioned. The
socket is created in the main thread, where the io_service::run() is called.
>From the initil data received on the socket, the command is recognised, then
a new thread is created and the socket and io_service is passed to it as a
reference. Then, inside the thread, the job is performed in an asynchronous
manner. As the thread exists before async_read_some is called (I think so),
I think at first I get the error in the async_read_some handler, later it
works fine... I think a code snippet would be far better than the written
words...
main()
{
...
try
{
boost::asio::io_service IOService;
TCPServer Server(IOService);
IOService.run();
}
catch(std::exception& e)
{
}
}
TCPServer:: TCPServer ()
{
StartAccept();
}
TCPServer:: StartAccept()
{
...
...
//here createa new TCPConnection object , shared_from_this
//Then inside TCPConnection object, it waits for new connection using
async_accept
}
"All the code is copied from asio sample", and the async_read Handler does
the following
AsyncReadhandler()
{
...
boost::thread *th=new
boost::thread(boost::bind(&TCPConnection::ExecutionRoutine,....))
}
//This is a thread
int TCPConnection::ExecutionRoutine(string Command)
{
...
...
if(Command = = "BIG_OPERATION")
{
boost::shared_ptr<BigOp> bo(new BigOp(Socket,IOService));
bo->Operation();
}
//Thread exits here, but the "Operation" continues on socket and
io_service....
}
BigOp::Operation()
{
Socket.async_read_some(....);
}
What is your opinion about "signals and slots" in this kind of situation?
Thanks a lot,
Lloyd
______________________________________
Scanned and protected by Email scanner
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net