Page 1 of 1

Threaded Server Question

Posted: Tue Aug 19, 2003 12:45 am
by Karbon
Is there any way with PB to create a thread for each connection to the server? The biggest problem I see with this is the ability to do event handling within the thread for just the single connection.. I don't have a clue if it's even possible..

Does anyone have any ideas?

Thanks!

Posted: Tue Aug 19, 2003 8:10 am
by Tranquil
Why do you want this? Why dont you handle all requests in a mainloop? Actions that takes more time I create a thread from the mainloop. But I set up a maximum- threads-value to avoid server hang up due to less CPU capacitis.

Are you using event-based networking or pooling? If you are using pooiling you should use threads, otherwise a mainloop with some threads (if needed) should be okay or?

Mike

Posted: Tue Aug 19, 2003 5:28 pm
by Karbon
Ahh.. My socket buddy Mike... Hi Mike :-)

My concern is that the server blocks while processing each request from a user. Some of the requests users send will take a little while to process (some will grab info from the database etc etc). With the current model the server will only serve 1 request at a time - this could be a big problem when we get more and more users connecting... Each users would have to wait for every other user's request to complete before they got any response form the server.. I'm really worried about that totally trashing the speed of this thing as a multi-user server!

I thought about just spinning off threads for certain kinds of requests and that seems possible, though a little messy. I'm used to fork()ing or creating a thread for each connection from my previous UNIX socket programming experience so that's why I ask :-)

To answer your question of events or polling, I'm using the events right now. That might just have to change - which means I'll probably go ahead and write the server in C unless you can using the polling method from within PB easily.. I'll have to get out the windows socket reference and look around!

Posted: Wed Aug 20, 2003 11:28 am
by Tranquil
Server-Tasks on different users which takes many CPU time (eg searching a database etc) will be prozessed in a thread. Thats the best you can do but becomes hard to realize if strings are needed.

That makes server-coding in PB very very difficult. I'm still waiting for string-save threads since version 3.30 to complete our file-sharing tool. Our client is completely finished we only need to code the server which is atm not possible in PB. :-(

Mike

Posted: Wed Aug 20, 2003 2:54 pm
by Karbon
Yes, the thread safety issues are a concern..

Check out this code and see what you think. It's based on the code you sent me forever ago - same basic flow and function. I used memory buffers instead of string type vars so it would be thread safe.. It is more of a pain but at least its a step in the right direction!

http://www.purebasic.org/comments.php?id=P20_0_1_0

Posted: Thu Aug 21, 2003 2:14 pm
by Tranquil
Thanks for this source. I noticed your source in the Tipps and tricks section.

But in my server source its not possible to implement couse there are to much datas to handle and I dont want to rewrite all. I will wait for the next PB Version which hopefully supports thread save strings.

Mike

Posted: Thu Aug 21, 2003 2:16 pm
by Karbon
Yes, thread safe strings will make things a lot easier!

*grins at Fred*

:-)

Posted: Fri Nov 14, 2003 4:28 pm
by Holborn
I beg of you, please make thread-safe strings a reality. I beg of you.

Posted: Mon Nov 17, 2003 2:37 am
by Paul Dwyer
Why stop at strings? :roll: