RE: Threading problem using AsyncSocket
RE: Threading problem using AsyncSocket
- Subject: RE: Threading problem using AsyncSocket
- From: "Matthew Youney" <email@hidden>
- Date: Fri, 15 Aug 2008 11:52:38 -0400
- Importance: Normal
Jonathan,
Thank you sooo much for your assistance. Here's what ended up working for
me:
1. I did update my AsyncSocket class as suggested. Thanks.
2. I determined that my runloop was not functioning as expected using your
suggestions. As you said, there is a lot of information here, and although
I am well-versed now, it was/is new to me (defending my ignorance). Again,
thanks!
3. I used the ThreadWorker class which has a nice technique for keeping the
runloop alive while in the worker thread. This is a very well done class
that also provides all of the hooks for GUI update, and other niceties.
I still have one issue:
I am instantiating my class in the worker thread (as required per AsyncSocet
docs). When I dealloc my object (which calls the socket.disconnect method),
the socket(s) do not disconnect properly, and I end up with a "socket leak".
If I do not dealloc, the disconnect operation occurs normally, and it
appears that the "garbage collection" autoreleases the object. Is this OK?
Seems kinda sloppy. Any suggestions?
Best regards,
Matt
Matthew
1. Are you using the latest AsyncSocket sources? cocoaasyncsocket - Google
Code <http://code.google.com/p/cocoaasyncsocket/>
2. AsyncSocket, though very good, is not perfect. I recently subclassed
AsyncSocket and encountered a fundamental problem (easily fixed however).
There could be unrecognised issues with the threading support as threading
AsyncSocket probably hasn't been attempted that much.
3. Are the CFSocket callback functions being called in AsyncSocket.m:
- static void MyCFSocketCallback (CFSocketRef sref, CFSocketCallBackType
type, CFDataRef address, const void *pData, void *pInfo);
- static void MyCFReadStreamCallback (CFReadStreamRef stream,
CFStreamEventType type, void *pInfo);
- static void MyCFWriteStreamCallback (CFWriteStreamRef stream,
CFStreamEventType type, void *pInfo);
Cave man debugging techniques should do the trick there.
4. Take a good hard look at AsyncSocket.m -
(void)doAcceptWithSocket:(CFSocketNativeHandle)newNative and the delegate
method -[theDelegate onSocket:self wantsRunLoopForNewSocket:newSocket].
5. Use CFRunLoopContainsSource to check that your socket has been
successfully added to the correct run loop.
Good luck!
Jonathan
FROM : Matthew Youney
DATE : Fri Aug 15 00:45:34 2008
Jonathan,
The reason I need to thread is not anything to do with AsyncSocket, which is
an excellent library that does not block. I have several time consuming
operations that must occur sequentially. These sequences need to run
(start/stop) asynchronous of each other, so I want these to run in separate
execution threads.
The "old school" way that I used to manage multiple asynchronous sequences
like this was to use complex state machines, but I am sure that you will
agree that separate threads is certainly the way to go.
I am just having difficulty getting my head around the run loop in the
worker threads. I have also tried the ThreadWorker library, that although
has some awesome features, does not let my AsyncSocket objects run properly.
And thanks, I appreciate any and all feedback,
Matt
-----Original Message-----
From: cocoa-dev-bounces+matt=youney.<email_removed>
[ mailto:cocoa-dev-bounces+matt=youney.<email_removed>]On Behalf Of
<email_removed>
Sent: Thursday, August 14, 2008 5:04 PM
To: <email_removed>
Subject: Re: Threading problem using AsyncSocket (Matthew Youney)
I don't know about your threading problem but surely the whole point
of AsyncSocket is the Async bit.
It is designed to attach itself to the runloop and remove the need for
blocking sockets etc on threads.
I use AsyncSocket extensively within the main thread and it can handle
multiple connection without any problem.
> Hello list,
> This is my first attempt at threading with Cocoa, and I am having
> difficu=
> lty
> with my classes that use AsyncSocket. I am using
> DetachNewThreadSelector=
> :
> to detach my =91worker=92 thread, and I am instantiating my class
> that us=
> es
> AsyncSocket from within this thread. The problem is that the
> AsyncSocket
> delegates are never called. This class works just fine from the main
> thread.
>
> - (IBAction)RunTestButtonPressed:(id)sender
> {
> [NSThread detachNewThreadSelector:
> @selector(threadFunction:)
> toTarget: self withObject: recipe];
> }
>
>
> - (void) threadFunction:(BeverageRecipe*)recipe
> {
> ClassContainingAsyncSocket* foo;
> int i=3D0;
>
> while (stuffTodo)
> {
> NSAutoreleasePool* autoReleasePool=3D[[NSAutoreleasePool alloc]init];
>
> foo=3D[[ ClassContainingAsyncSocket alloc]init];
>
> i=3D[foo readRegister:0]; //none of the AsyncSocket
> delegate=
> s
> =91fire!=92
>
> [autoReleasePool release];
>
>
> do more stuff=85.
> }
>
> }
>
Does anyone have any idea what I am missing? Something fundamentally
wro=
ng?
Thanks in advance for any and all assistance.
Matt
_______________________________________________
Cocoa-dev mailing list (<email_removed>)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to <email_removed>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden