forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Multiple threads working together?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
thomas_fogh



Joined: 20 Jul 2007
Posts: 21
Location: Denmark

PostPosted: Fri Mar 07, 2008 9:41 pm    Post subject: Multiple threads working together? Reply with quote

Hi,
I want to have a thread (or just main()?) to handle all user input and UI updates and a thread handling network input/output.
I know how to create threads, but how do I send commands/date between the two?
And should I create a thread for the "main"?

Thanks!

BR, Thomas Fogh
(I'm making a MPD client)
Back to top
View user's profile Send private message
Art



Joined: 09 Nov 2005
Posts: 647

PostPosted: Fri Mar 07, 2008 9:52 pm    Post subject: Reply with quote

You need to google the word "semaphore".
I learned that out of my experience with the PSP.
_________________
If not actually, then potentially.
Back to top
View user's profile Send private message
thomas_fogh



Joined: 20 Jul 2007
Posts: 21
Location: Denmark

PostPosted: Fri Mar 07, 2008 10:09 pm    Post subject: Reply with quote

Ok, thanks! I'll have a look at sceKernelCreateSema() and related functions.
So I guess I just allocate a "shared" buffer and let the semaphore decide if it's available, right?
Back to top
View user's profile Send private message
Vincent_M



Joined: 03 Apr 2007
Posts: 73

PostPosted: Sat Mar 08, 2008 3:04 am    Post subject: Reply with quote

There is also a ghetto way of doing this as well. You can make the data you want access to global, but keep in mind that there are restrictions... ;)
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Sat Mar 08, 2008 3:35 am    Post subject: Reply with quote

That wouldn't matter.

A semaphore is used to lock access to the data, when needed.
Back to top
View user's profile Send private message
Vincent_M



Joined: 03 Apr 2007
Posts: 73

PostPosted: Sat Mar 08, 2008 4:08 am    Post subject: Reply with quote

So my method of using global variables is not the best method? Semaphores just restricts access, but does not give access? The article I read in Wikipedia gave me the same impression. So then the data would have to be global, and the semaphores would be used to restrict the access of the data. I could see use in that for large-scaled projects.
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sat Mar 08, 2008 7:58 pm    Post subject: Reply with quote

Urm it all depends what you are trying to do :P Global variables are the simplest method of inter-thread communication available and depending what the variables are you might be able to get away with not even locking them (but using a semaphore as a mutex is the best way although if you _are_ ghetto you would disable interrupts, hehe :P).

Anyway the PSP has loads of thread primitives, you have event flags which you can use to signal events (duh!) when for example data is available. The advantage of using something like that is your consumer thread only needs to run when there is something to do as when it calls sceKernelWaitEventFlag it will go into a wait state which makes the thread take no CPU cycles. You can also do everything in one go using message boxes or message pipes which will send distinct messages to another thread, so you could say create a message box into which you post a string to display on the screen.

Then there is thread sleeping and waking, thread suspension and resuming, vtimers, thread locked memory allocations (VPL and FPL) and other stuff. Check out the thread module in the SDK (pspthreadman.h), it even has the odd example and there is at the least a message box sample in the SDK.

And don't forget in all this that PSP threads are co-operative, if you don't put threads into a wait state at some point you will be wasting an awful lot of processor time for nothing, so don't poll global variables, at least not more than once a frame.
Back to top
View user's profile Send private message
thomas_fogh



Joined: 20 Jul 2007
Posts: 21
Location: Denmark

PostPosted: Tue Mar 11, 2008 1:05 am    Post subject: Reply with quote

Thanks, TyRaNiD!
I want to use sceKernelReceiveMbxCB() to make the network thread sleep unless the main thread sends a message to be transmitted over the wlan or data is received from the wlan. I can't figure out how to register a callback for the network rx though...
BR, Thomas Fogh
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group