C++0x Socket Threading

Hey CPlusPlus

I am in desperate need for some help with the new C++0x release, VS11 Beta and Winsock2. I can't get the thread system to successfully merge with winsock.
When the #include <thread> is included in the Socket_Class.h (Custom class for socket 'stuff') it gives no compile error, however, when I try to 'listen(SOCKET s, int backlog)', the socket returns socket error 10022. The reason is the #include <thread> inside the Socket_Class header, I did track it down.
When I try to connect the socket and thread outside the class, in main.cpp for example, thread t(listenFunction); gives no compile error, but when I try to listen I recieve the 10038 socket error.

//Example of thread t(listenFunction)
void listenFunction()
{
Socket_Class sControl;
sControl.listen();
sControl.accept();
}

I have no idea why the socket error only occurs when I thread the listen(), or include #<thread> inside the class header, peculiar indeed!
Anyone experienced similar problems?

It might be a really basic error, but I can't wrap my head around this, so thanks.
http://msdn.microsoft.com/en-us/library/aa924071.aspx

10036 WSAEINPROGRESS
Operation now in progress. A blocking operation is currently executing. Windows Sockets only allows a single blocking operation — per task or thread — to be outstanding, and if any other function call is made (whether or not it references that or any other socket) the function fails with the WSAEINPROGRESS error.

10022 WSAEINVAL
Invalid argument. Some invalid argument was supplied (for example, specifying an invalid level to the setsockopt (Windows Sockets) function). In some instances, it also refers to the current state of the socket — for instance, calling accept (Windows Sockets) on a socket that is not listening.

You have a problem with the socket initialisation somewhere. Have you called WSAStartup? Have you checked all your error codes?
Yes, that is what makes this so peculiar. If I try to listen or any other socket operation without the thread included it works perfectly.
But it suddently becomes an socket error if I have threads included in the Socket_Class header, or try to connect a new thread with the socket.
I can't really add anymore from the information you've provided.
Call all sockets functions in same thread, including WsaStartup.
Try not using <thread> header, maybe it has undocumented issues, stick with well known APIs.
Thank you, I will check tomorrow.
It sounds like the correct solution, however, how can I then create an infinite loop for listening in another thread?
I might try and create the socket listening and initialization outside a class.
Topic archived. No new replies allowed.