Discussion:
[asio-users] Resource deadlock exception
Dev Guy
2015-12-25 05:22:09 UTC
Permalink
Hello can some help clear up what I am doing wrong with trying to use
threads with vector? I am coding up a simple async echo, here is a
snippet of the problem area,


for( int i=0; i < threads; ++i )
{
_workers.push_back( std::thread( [this]() {
_sock.get_io_service().run();
} ) );
}

for( auto & t : _workers)
{
t.join();
}

When I run my server and as soon as I connect to it using netcat I get
an exception?

$ ./simple_async_server 5050
terminate called after throwing an instance of 'std::system_error'
what(): Resource deadlock avoided
Aborted (core dumped)


However the following works no matter how many threads.

std::thread t( [this]() { _sock.get_io_service().run(); } );
t.join();

So not clean why it doesn't work when I save the threads in a vector?
Alex Biv
2015-12-25 05:38:09 UTC
Permalink
Hi!

std::thread is not copy-constructible.

You should use emplace_back.
Post by Dev Guy
Hello can some help clear up what I am doing wrong with trying to use
threads with vector? I am coding up a simple async echo, here is a
snippet of the problem area,
for( int i=0; i < threads; ++i )
{
_workers.push_back( std::thread( [this]() {
_sock.get_io_service().run();
} ) );
}
for( auto & t : _workers)
{
t.join();
}
When I run my server and as soon as I connect to it using netcat I get
an exception?
$ ./simple_async_server 5050
terminate called after throwing an instance of 'std::system_error'
what(): Resource deadlock avoided
Aborted (core dumped)
However the following works no matter how many threads.
std::thread t( [this]() { _sock.get_io_service().run(); } );
t.join();
So not clean why it doesn't work when I save the threads in a vector?
------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Scott Mueller
2015-12-25 05:59:23 UTC
Permalink
std::vector<>::push_back has an overload which takes an rvalue reference, which is what std::thread() will generate. If you’re trying to copy-construct a thread, it will fail at compile-time. There’s something else going on here. Is there a work object that you’re releasing after you’ve detected that the socket has expired? Do you need several threads to handle traffic on one socket? I think I need to see more code to find out what it is tha tyou’re trying to do.

Best regards,

M. Scott Mueller

From: Alex Biv [mailto:***@gmail.com]
Sent: Thursday, December 24, 2015 9:38 PM
To: asio-***@lists.sourceforge.net
Subject: Re: [asio-users] Resource deadlock exception

Hi!

std::thread is not copy-constructible.

You should use emplace_back.


2015-12-25 8:22 GMT+03:00 Dev Guy <***@gmail.com<mailto:***@gmail.com>>:
Hello can some help clear up what I am doing wrong with trying to use
threads with vector? I am coding up a simple async echo, here is a
snippet of the problem area,


for( int i=0; i < threads; ++i )
{
_workers.push_back( std::thread( [this]() {
_sock.get_io_service().run();
} ) );
}

for( auto & t : _workers)
{
t.join();
}

When I run my server and as soon as I connect to it using netcat I get
an exception?

$ ./simple_async_server 5050
terminate called after throwing an instance of 'std::system_error'
what(): Resource deadlock avoided
Aborted (core dumped)


However the following works no matter how many threads.

std::thread t( [this]() { _sock.get_io_service().run(); } );
t.join();

So not clean why it doesn't work when I save the threads in a vector?

------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net<mailto:asio-***@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Dev Guy
2015-12-25 19:02:58 UTC
Permalink
Hi Scott,

you can find the code I am using to learn about ASIO here:

https://github.com/ryadav/simple_async_server

There is a if / def you can play with to see what I mean.

Thanks,
Rajinder Yadav
Post by Scott Mueller
std::vector<>::push_back has an overload which takes an rvalue reference,
which is what std::thread() will generate. If you’re trying to
copy-construct a thread, it will fail at compile-time. There’s something
else going on here. Is there a work object that you’re releasing after
you’ve detected that the socket has expired? Do you need several threads to
handle traffic on one socket? I think I need to see more code to find out
what it is tha tyou’re trying to do.
Best regards,
M. Scott Mueller
Sent: Thursday, December 24, 2015 9:38 PM
Subject: Re: [asio-users] Resource deadlock exception
Hi!
std::thread is not copy-constructible.
You should use emplace_back.
Hello can some help clear up what I am doing wrong with trying to use
threads with vector? I am coding up a simple async echo, here is a
snippet of the problem area,
for( int i=0; i < threads; ++i )
{
_workers.push_back( std::thread( [this]() {
_sock.get_io_service().run();
} ) );
}
for( auto & t : _workers)
{
t.join();
}
When I run my server and as soon as I connect to it using netcat I get
an exception?
$ ./simple_async_server 5050
terminate called after throwing an instance of 'std::system_error'
what(): Resource deadlock avoided
Aborted (core dumped)
However the following works no matter how many threads.
std::thread t( [this]() { _sock.get_io_service().run(); } );
t.join();
So not clean why it doesn't work when I save the threads in a vector?
------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
--
Kind Regards,
Rajinder Yadav

SafetyNet Test Driven Development
http://safetynet.devmentor.org
Alex Biv
2015-12-25 20:37:50 UTC
Permalink
Hi again!



I tried to study your code (
https://github.com/ryadav/simple_async_server/blob/master/src/main.cpp )



It seems i understand what is going on.



Look at pthread_join man page:

http://linux.die.net/man/3/pthread_join



EDEADLK

A deadlock was detected (e.g., two threads tried to join with each other);
or *thread specifies the calling thread.*



After connection is accepted by your server, run (0) called in accept
callback. It starts waiting for another connection to accept.

Input parameter 'threads' is 0, so no threads are added to _workers, but
this code:

for( auto & t : _workers)

{

t.join();

}



is executing,



and it is executing from inside one of threads in _workers vector (probably
it is thread at the beginning of vector _workers). And it calls join for
himself.


P.S. Sorry for my english
Post by Dev Guy
Hi Scott,
https://github.com/ryadav/simple_async_server
There is a if / def you can play with to see what I mean.
Thanks,
Rajinder Yadav
Post by Scott Mueller
std::vector<>::push_back has an overload which takes an rvalue reference,
which is what std::thread() will generate. If you’re trying to
copy-construct a thread, it will fail at compile-time. There’s something
else going on here. Is there a work object that you’re releasing after
you’ve detected that the socket has expired? Do you need several threads
to
Post by Scott Mueller
handle traffic on one socket? I think I need to see more code to find out
what it is tha tyou’re trying to do.
Best regards,
M. Scott Mueller
Sent: Thursday, December 24, 2015 9:38 PM
Subject: Re: [asio-users] Resource deadlock exception
Hi!
std::thread is not copy-constructible.
You should use emplace_back.
Hello can some help clear up what I am doing wrong with trying to use
threads with vector? I am coding up a simple async echo, here is a
snippet of the problem area,
for( int i=0; i < threads; ++i )
{
_workers.push_back( std::thread( [this]() {
_sock.get_io_service().run();
} ) );
}
for( auto & t : _workers)
{
t.join();
}
When I run my server and as soon as I connect to it using netcat I get
an exception?
$ ./simple_async_server 5050
terminate called after throwing an instance of 'std::system_error'
what(): Resource deadlock avoided
Aborted (core dumped)
However the following works no matter how many threads.
std::thread t( [this]() { _sock.get_io_service().run(); } );
t.join();
So not clean why it doesn't work when I save the threads in a vector?
------------------------------------------------------------------------------
Post by Scott Mueller
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Post by Scott Mueller
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
--
Kind Regards,
Rajinder Yadav
SafetyNet Test Driven Development
http://safetynet.devmentor.org
------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Dev Guy
2015-12-25 22:42:57 UTC
Permalink
Hi Alex, thanks that was it, how I didn't notice that myself :)
Post by Alex Biv
Hi again!
I tried to study your code (
https://github.com/ryadav/simple_async_server/blob/master/src/main.cpp )
It seems i understand what is going on.
http://linux.die.net/man/3/pthread_join
EDEADLK
A deadlock was detected (e.g., two threads tried to join with each other);
or thread specifies the calling thread.
After connection is accepted by your server, run (0) called in accept
callback. It starts waiting for another connection to accept.
Input parameter 'threads' is 0, so no threads are added to _workers, but
for( auto & t : _workers)
{
t.join();
}
is executing,
and it is executing from inside one of threads in _workers vector (probably
it is thread at the beginning of vector _workers). And it calls join for
himself.
P.S. Sorry for my english
Post by Dev Guy
Hi Scott,
https://github.com/ryadav/simple_async_server
There is a if / def you can play with to see what I mean.
Thanks,
Rajinder Yadav
Post by Scott Mueller
std::vector<>::push_back has an overload which takes an rvalue reference,
which is what std::thread() will generate. If you’re trying to
copy-construct a thread, it will fail at compile-time. There’s something
else going on here. Is there a work object that you’re releasing after
you’ve detected that the socket has expired? Do you need several threads to
handle traffic on one socket? I think I need to see more code to find out
what it is tha tyou’re trying to do.
Best regards,
M. Scott Mueller
Sent: Thursday, December 24, 2015 9:38 PM
Subject: Re: [asio-users] Resource deadlock exception
Hi!
std::thread is not copy-constructible.
You should use emplace_back.
Hello can some help clear up what I am doing wrong with trying to use
threads with vector? I am coding up a simple async echo, here is a
snippet of the problem area,
for( int i=0; i < threads; ++i )
{
_workers.push_back( std::thread( [this]() {
_sock.get_io_service().run();
} ) );
}
for( auto & t : _workers)
{
t.join();
}
When I run my server and as soon as I connect to it using netcat I get
an exception?
$ ./simple_async_server 5050
terminate called after throwing an instance of 'std::system_error'
what(): Resource deadlock avoided
Aborted (core dumped)
However the following works no matter how many threads.
std::thread t( [this]() { _sock.get_io_service().run(); } );
t.join();
So not clean why it doesn't work when I save the threads in a vector?
------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
--
Kind Regards,
Rajinder Yadav
SafetyNet Test Driven Development
http://safetynet.devmentor.org
------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
--
Kind Regards,
Rajinder Yadav

SafetyNet Test Driven Development
http://safetynet.devmentor.org
Dev Guy
2015-12-25 18:59:26 UTC
Permalink
Hi Alex, in C++11 a temp object is move constructed with respect to a
vector, you can add a std::move or use emplace_back, it will result in
the same exception.

You can convince yourself with this sample code:
https://github.com/ryadav/vector_thread_group
Бобров Александр
2015-12-25 05:51:02 UTC
Permalink
Hi!
Post by Dev Guy
Hello can some help clear up what I am doing wrong with trying to use
threads with vector? I am coding up a simple async echo, here is a
snippet of the problem area,
for( int i=0; i < threads; ++i )
{
_workers.push_back( std::thread( [this]() {
_sock.get_io_service().run();
} ) );
}
for( auto & t : _workers)
{
t.join();
}
When I run my server and as soon as I connect to it using netcat I get
an exception?
$ ./simple_async_server 5050
terminate called after throwing an instance of 'std::system_error'
what(): Resource deadlock avoided
Aborted (core dumped)
However the following works no matter how many threads.
std::thread t( [this]() { _sock.get_io_service().run(); } );
t.join();
So not clean why it doesn't work when I save the threads in a vector?
------------------------------------------------------------------------------
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Loading...