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)
Multiple threads working together?
-
- Posts: 21
- Joined: Fri Jul 20, 2007 10:22 pm
- Location: Denmark
-
- Posts: 21
- Joined: Fri Jul 20, 2007 10:22 pm
- Location: Denmark
-
- Posts: 376
- Joined: Wed May 10, 2006 11:31 pm
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.
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.
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.
-
- Posts: 21
- Joined: Fri Jul 20, 2007 10:22 pm
- Location: Denmark