Thread calling function from class problem

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

Moderators: cheriff, TyRaNiD

Post Reply
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Thread calling function from class problem

Post by Ghoti »

Hi folks,

I am trying to create a simple loadingscreen function. I do it with the sceKernelCreateThread function which create the loading sequence rendering code while the main thread remains loading up models/pictures/variable/files etc....

anyway... that was the idea :) As in the past I have some problem regarding function pointers and the like.

The second parameter of the function sceKernelCreateThread is pointer to a function. I try to do it like this:

Code: Select all

LoadingScreen::LoadingScreen() {
	// the loadingscreen is loaded
	thid_ = sceKernelCreateThread("LoadingThread", this->RunLoadingScreen(), 0x18, 0x10000, 0, NULL);
}


void LoadingScreen::KillLoadingScreen() {
	// shut down the loading screen again.
	sceKernelDeleteThread(thid_);
}

void LoadingScreen::RunLoadingScreen() {
	
	while(1) {
		// render loading screen


	}

}
Now calling this function returns an error with compiling.

Code: Select all

oadingScreen.cpp: In constructor 'LoadingScreen::LoadingScreen()':
LoadingScreen.cpp:6: error: invalid use of void expression
I remember from before that there is a way of using pointers to functions from classes but I do not know anymore how exactly and I could not find it with google :(

Hope you guys can shed some light on it !

greets Ghoti
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Re: Thread calling function from class problem

Post by Raphael »

Ghoti wrote:

Code: Select all

	thid_ = sceKernelCreateThread("LoadingThread", this->RunLoadingScreen(), 0x18, 0x10000, 0, NULL);
What this line does is call this->RunLoadingScreen() and try to submit its return value (which is void) as the second parameter to sceKernelCreateThread.
What you want to do is this:

Code: Select all

thid_ = sceKernelCreateThread("LoadingThread", RunLoadingScreen, 0x18, 0x10000, 0, NULL);
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

Hi thanks!

you are right but I also had to change the function to a static and like this:

Code: Select all

int LoadingScreen&#58;&#58;RunLoadingScreen&#40;SceSize args, void *argp&#41; &#123;
but what I do not understand is why, why does it need the args and argp? why does it have to be an int as return value ? I really want to understand it before I will use it for whatever.

greets ghoti

EDIT::EDIT
Is there a non kernel function for creating and using thread by the way ?
Cpasjuste
Posts: 214
Joined: Sun May 29, 2005 8:28 am

Post by Cpasjuste »

You can create and start threads in user mode the same way!
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

Ghoti wrote: but what I do not understand is why, why does it need the args and argp? why does it have to be an int as return value ?
Because that's the definition of a thread entry function, same as the main program entry function main( ... ).
Is there a non kernel function for creating and using thread by the way ?
sceKernelCreateThread is actually a user function. Just because it starts with sceKernel doesn't mean the function only works in kernel mode.
See the header files in pspsdk\src\user
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

Raphael wrote:
Ghoti wrote: but what I do not understand is why, why does it need the args and argp? why does it have to be an int as return value ?
Because that's the definition of a thread entry function, same as the main program entry function main( ... ).
Is there a non kernel function for creating and using thread by the way ?
sceKernelCreateThread is actually a user function. Just because it starts with sceKernel doesn't mean the function only works in kernel mode.
See the header files in pspsdk\src\user
Yes your right about the main function :) forgot about that, for the PSP I always just pass void to the main function.

I yes I should have found that in the header files, I just assumed without checking if there is kernel in the name it is a kernel function :s will not do that anymore :D
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

The reason it needs to be static is due to C++ calling conventions. There are some tricks you can do to fudge it but normally best to do something akin to:

Code: Select all

class MyClass
&#123;
public&#58;
    static int ThreadEntry&#40;int args, void *argp&#41; &#123;
         MyClass *this = *&#40;&#40;MyClass **&#41; argp;

         this->RunLoadingScreen&#40;&#41;;
         return 0;
    &#125;
&#125;

MyClass* p = new MyClass;
uid = sceKernelCreateThread&#40;"MyClassThread", MyClass&#58;&#58;ThreadEntry, ...&#41;;
sceKernelStartThread&#40;uid, sizeof&#40;p&#41;, &p&#41;;
pspZorba
Posts: 156
Joined: Sat Sep 22, 2007 11:45 am
Location: NY

Post by pspZorba »

Does it mean that the real signature of a thread entry is :

int threadEntry( int argc, void *argv[] )

rather than

int threadEntry( int argc, void *argv )
--pspZorba--
NO to K1.5 !
pspZorba
Posts: 156
Joined: Sat Sep 22, 2007 11:45 am
Location: NY

Post by pspZorba »

froget about the question.

I understood my mistake
--pspZorba--
NO to K1.5 !
Post Reply