Sign in to follow this  
JohnGreek

[SDL/pthread]Problem creating 2 threads with these libs

Recommended Posts

JohnGreek    100
[size="3"]Hello, i am programming a winsock server and client. It's (supposed to be) multithreaded but i' ve run to a very serious problem and spend quite some time
but i still cannot handle it. Googled as well, still nothing.
The problem is as follows:

I have a function which runs all the time to recieve the packets, and when a packet is recieved it creates a new thread to process the data.
But when i connect with a second client, the second thread fails to init and returns NULL. I have this problem with SDL [b]and [/b]pthreads.
In SDL i get NULL from the thread and from pthread i get [EAGAIN] error which is "The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process PTHREAD_THREADS_MAX would be exceeded." When i run this macro i get 2k+ THREADS_MAX, so i can create 2k threads normaly.

It's not possible that i don't have resources since my machine is dual-core/2GB RAM. I' ve spend hours and hours without any result.
One more thing when i made a test application to see if the thread is created and running, it actually worked, but in this one i need it' doesn' t.
If you need code just tell me, although i doubt it' s error in my code, i checked quite many times and changed everything to very simple, and still..

Thanks in advance.
[/size]

Share this post


Link to post
Share on other sites
rip-off    10976
First of all, creating a thread to handle a packet is not something one would normally do, unless each individual packet creates a large amount of work. Do you mean creating a thread to handle a new client? Again, this is generally not a scalable solution, but for a small number of clients it might work.

Do you know how many threads you've actually created? The system might have a dynamically set the number of threads you can start (e.g. ulimit).
[quote]
If you need code just tell me, although i doubt it' s error in my code, i checked quite many times and changed everything to very simple, and still..
[/quote]
It could be an error in your code. Have you tried creating a small test program that just creates threads, and see how many it creates before it fails?

Share this post


Link to post
Share on other sites
JohnGreek    100
Actually the case is that when a packet that contains the data ex. "USER_REGISTER" it starts a new thread called RegisterThread which is responsible to handle the register operation,
so if at the same time 2 users try to register, it will handle them at the same time. But this is supposed to be done in the future as well, since the server will handle many users at the same
time in games. But you say it's a bad idea.. how i can handle 2 users try to register at the same time without blocking each other, or other operations at the same time? If you have any idea it could really help..
To answer your question i did make a small console and made 2 threads and guess what, it worked! That's why i'm so frustrated. I don't really see why it creates a problem because
i get the error from the create_thread function on both libraries, not when a new socket is init, so something happens at that part. One thing that comes in my mind is that i use the same port to create
all new sockets but i also added the REUSEADDR in the socket option. I hope you got an idea.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this