Welcome to Soft32 Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Threads, sockets, blocking calls

 
   Soft32 Home -> Mac -> Programmer Help RSS
Next:  ld error  
Author Message
Keith Wiley

External


Since: Apr 05, 2005
Posts: 237



(Msg. 1) Posted: Fri Apr 15, 2005 10:22 am
Post subject: Threads, sockets, blocking calls
Archived from groups: comp>sys>mac>programmer>help (more info?)

I have an old Classic Open Transport program that I'm trying to modernize
with sockets. This program runs a server that accepts an undetermined
number of connections from clients and then uses those clients as a
distributed computing platform. The way the program handled an unknown
number of clients was by using threads. In addition to the main
application thread, there was a single "server connection" thread that
waited for OT connection attempts. Each time one occurred, a new thread
would be created to handle that individual client's communications. This
is all modeled on Apple's SimpleServer demo, which I'm not sure is even on
the website anymore. I guess the OT calls were nonblocking, or at least
some of the calls were nonblocking. In particular, I think OTListen was
might have been nonblocking (I can't tell now because the program is too
old to debug, CW won't run it in a debugger, only standalone).

Anyway, I'm rewriting the program to use sockets with Mach-O. Everything
compiles and builds and runs fine. The equivalent of OTListen is accept.
The problem is, once the main connection server threads calls accept, the
program locks up waiting for a connection. It doesn't matter if I call
YieldToAnyThread right before calling accept, because eventually the
thread manager comes back and resumes this thread (which of course is
necessary to catch the next connection attempt), and then immediately
locks up in the accept call, and no other threads can execute either.

I kind of thought one good point of threads was to allow blocking calls to
go off by themselves and block all they want, but leave other threads
nonblocked and otherwise executing. Otherwise, what was the point of
using individual threads to handle the individual clients in the first
place? (or was there never a point and I shouldn't have done that
anyway?)

Am I missing something here, or is this going to be much much hard with
sockets than with OT? Basically, I need preemtive multitasking or
something like that. Can I tell accept to give up and return after some
set period of time, say 1/100th of a second or so?

________________________________________________________________________
Keith Wiley kwiley DeleteThis @cs.unm.edu http://www.unm.edu/~keithw

"Yet mark his perfect self-contentment, and hence learn his lesson,
that to be self-contented is to be vile and ignorant, and that to
aspire is better than to be blindly and impotently happy."
-- Edwin A. Abbott, Flatland
________________________________________________________________________
Back to top
Login to vote
Tom Harrington

External


Since: Aug 19, 2003
Posts: 1921



(Msg. 2) Posted: Fri Apr 15, 2005 11:42 am
Post subject: Re: Threads, sockets, blocking calls [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <Pine.LNX.4.60.0504151512260.3833.DeleteThis@chawla.cs.unm.edu>,
Keith Wiley <kwiley.DeleteThis@cs.unm.edu> wrote:

> Anyway, I'm rewriting the program to use sockets with Mach-O. Everything
> compiles and builds and runs fine. The equivalent of OTListen is accept.
> The problem is, once the main connection server threads calls accept, the
> program locks up waiting for a connection. It doesn't matter if I call
> YieldToAnyThread right before calling accept, because eventually the
> thread manager comes back and resumes this thread (which of course is
> necessary to catch the next connection attempt), and then immediately
> locks up in the accept call, and no other threads can execute either.

The way Apache used to handle this was to use select() to check whether
there was a waiting connection, and only call accept(). You can give
select() a timeout so as not to block for too long (you can set the
timeout to zero to effectively poll, or use something short).

With multiple threads you could then use a mutex to have each thread try
the socket in a round-robin fashion. Each would do something like:

get mutex
select()
if there' a new connection, accept()
release mutex

--
Tom "Tom" Harrington
Macaroni, Automated System Maintenance for Mac OS X.
Version 2.0: Delocalize, Repair Permissions, lots more.
See http://www.atomicbird.com/
Back to top
Login to vote
Alwyn

External


Since: Apr 26, 2005
Posts: 41



(Msg. 3) Posted: Fri Apr 15, 2005 5:52 pm
Post subject: Re: Threads, sockets, blocking calls [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Fri, 15 Apr 2005 16:22:14 -0600, Keith Wiley wrote:
>
> Am I missing something here, or is this going to be much much hard with
> sockets than with OT? Basically, I need preemtive multitasking or
> something like that. Can I tell accept to give up and return after some
> set period of time, say 1/100th of a second or so?

If you want pre-emptive threads, either use the POSIX interface or
Multi-Processing Services, not Thread Manager, which provides co-operative
multithreading.

It is also possible to test for an incoming connection request (with a
timeout) by using the 'select' system call.
<http://lib.daemon.am/Books/BGNET/html/advanced.html>

HTH

Alwyn
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Learning Sockets the Cocoa Way? - I want to learn, using Cocoa functions, to do the following (in psuedo-code): if(connect ("my.mailserver.com&quot...

How to Specify port # in BSD Sockets? - I'm using BSD's open() to get my host's socket. The trouble is passing it a specific port -- when I try the app, it ret...

ReceiveNextEvent Blocking - Hello, I'm having a mysterious event handling problem in a carbon shared library. I am using ReceiveNextEvent to propa...

Menus blocking NSInputStream? - Hi, I have my own class that I instantiate and it starts a tcp/ip connection using [NSStream getStreamsToHost:host po...

Using Run Loops - Blocking User Input? - I've been working for a few weeks on an application to write data to a USB device and receive input from that device. ...

C calls to set login item. - Hi, I'm trying to figure out how to get my application to start when a user logs in. It would seem that we need to us...
       Soft32 Home -> Mac -> Programmer Help All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum

Categories:
 Windows
 Linux
  Mac
 PDA


[ Contact us | Terms of Service/Privacy Policy ]