Multiple threads working together?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
thomas_fogh
Posts: 21
Joined: Fri Jul 20, 2007 10:22 pm
Location: Denmark

Multiple threads working together?

Post by thomas_fogh »

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)
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

You need to google the word "semaphore".
I learned that out of my experience with the PSP.
If not actually, then potentially.
thomas_fogh
Posts: 21
Joined: Fri Jul 20, 2007 10:22 pm
Location: Denmark

Post by thomas_fogh »

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?
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

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... ;)
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

That wouldn't matter.

A semaphore is used to lock access to the data, when needed.
Vincent_M
Posts: 73
Joined: Tue Apr 03, 2007 4:16 am

Post by Vincent_M »

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.
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

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.
thomas_fogh
Posts: 21
Joined: Fri Jul 20, 2007 10:22 pm
Location: Denmark

Post by thomas_fogh »

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
Post Reply