| View previous topic :: View next topic |
| Author |
Message |
smartie_on_computer
Joined: 09 May 2007 Posts: 28
|
Posted: Sat May 23, 2009 8:51 am Post subject: Using threads and Classes |
|
|
Hey,
Im back again with another annoying problem of mine...
i've created a thread in my program and a class that has private and public members.
the thread is created like this: | Code: | class Camera
{
public:
Camera();
~Camera();
int stop();
int start();
int fillBuffer(bool blitToScreen);
void Init(u8* c_buf,u8* c_work,u32* c_fbuf);
void destroy();
bool Started;
bool Error;
u8* buffer;
private:
PspUsbCamSetupVideoParam videoparam;
int LoadModules();
int UnloadModules();
int InitCamera();
int StartUsb();
int StopUsb();
int InitJpegDecoder();
int FinishJpegDecoder();
bool initiated;
u8* work;//[68*1024] __attribute__((aligned(64)));
u32* framebuffer;//[480*272] __attribute__((aligned(64)));
}; |
| Code: | video_thid = sceKernelCreateThread("video_thread", video_thread, 16, 16*1024, 0, NULL);
if (video_thid < 0)
{
printf("Cannot create video thread.\n");
//return -1;
}
if (sceKernelStartThread(video_thid, 0, NULL) < 0)
{
printf("Cannot start video thread.\n");
//return -1;
} |
that works fine, but in the thread, when i call a class member, my psp freezes: | Code: | int video_thread(SceSize args, void *argp){
while(true){
if(!WebCam.Error && WebCam.Started){
if(WebCam.FillBuffer(true)<0){
printf("There was an error filling the buffer...\n");
}
}
}else if(WebCam.Error){
printf("Webcam Error...\n");
sceKernelDelayThread(50000);
}
}
sceKernelExitDeleteThread(0);
return 0;
} |
the program freezes the moment it tries to access 'WebCam.Error' or 'WebCam.Started', i've even tried calling 'WebCam.FillBuffer(true)' and still nothing.
im not 100% clear with threads yet, but i cant see anything going wrong here.
Maybe you guys can see what im doing wrong?
Cheers
Roman |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Sat May 23, 2009 9:23 am Post subject: |
|
|
You're creating a kernel thread. If you want a user thread, be sure to use PSP_THREAD_ATTR_USER when creating the thread instead of 0. Kernel level stuff can't access user level stuff without setting k1.
| Code: | unsigned int k1;
k1 = pspSdkSetK1(0);
// do stuff here
pspSdkSetK1(k1);
|
|
|
| Back to top |
|
 |
coolkehon
Joined: 20 Oct 2008 Posts: 355
|
Posted: Sat May 23, 2009 9:42 am Post subject: |
|
|
| so if i make a kernel thread in my main eboot does that mean i can access kernel functions |
|
| Back to top |
|
 |
smartie_on_computer
Joined: 09 May 2007 Posts: 28
|
Posted: Sat May 23, 2009 10:09 am Post subject: |
|
|
| J.F. wrote: | You're creating a kernel thread. If you want a user thread, be sure to use PSP_THREAD_ATTR_USER when creating the thread instead of 0. Kernel level stuff can't access user level stuff without setting k1.
| Code: | unsigned int k1;
k1 = pspSdkSetK1(0);
// do stuff here
pspSdkSetK1(k1);
|
|
i've tried setting K1 and tried PSP_THREAD_ATTR_USER. and my psp still crashes... |
|
| Back to top |
|
 |
coolkehon
Joined: 20 Oct 2008 Posts: 355
|
Posted: Sat May 23, 2009 10:17 am Post subject: |
|
|
| no you have to do either or not both pspSdkSetK1 if in kernel thread or set the thread to a user thread not both or else your calling a kernel funct in a user thread |
|
| Back to top |
|
 |
coolkehon
Joined: 20 Oct 2008 Posts: 355
|
Posted: Sat May 23, 2009 10:18 am Post subject: |
|
|
| do you have enoph heap allocated for this thread |
|
| Back to top |
|
 |
smartie_on_computer
Joined: 09 May 2007 Posts: 28
|
Posted: Sat May 23, 2009 10:19 am Post subject: |
|
|
| yeah i figured that part, but no i didnt do both things at once, im not that stupid to do that lol |
|
| Back to top |
|
 |
smartie_on_computer
Joined: 09 May 2007 Posts: 28
|
Posted: Sat May 23, 2009 10:20 am Post subject: |
|
|
| coolkehon wrote: | | do you have enoph heap allocated for this thread |
Heap? |
|
| Back to top |
|
 |
smartie_on_computer
Joined: 09 May 2007 Posts: 28
|
Posted: Sat May 23, 2009 10:30 am Post subject: |
|
|
im pretty sure the heap size is fine...
the has a small amount of memory because the buffers are pointers to byte arrays defined in the main code. so the heap size should be fine. |
|
| Back to top |
|
 |
smartie_on_computer
Joined: 09 May 2007 Posts: 28
|
Posted: Sat May 23, 2009 10:58 am Post subject: |
|
|
Sorry, i will have to excuse myself here.
its working perfectly now, the reason why it was freezing was because i was not delaying the thread so it was looping the thread but not executing anything else.
so it works wonderfully now and thanks for your help J.F. and coolkehon!
Cheers
Roman |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Sat May 23, 2009 1:52 pm Post subject: |
|
|
| I just remembered something - creating a thread from a user thread never creates a kernel thread. That's in an old thread too. I was going to suggest bumping the size of the thread's stack. When you create a thread, many people just do it like the examples... which tend to use tiny stacks as the examples don't do much. If you have functions called by the thread with large local arrays, it would be easy to overflow the stack and create a fault. Good to hear you have it working, but check your stack size anyway to make sure it doesn't cause trouble later. :) |
|
| Back to top |
|
 |
coolkehon
Joined: 20 Oct 2008 Posts: 355
|
Posted: Sun May 24, 2009 7:56 am Post subject: |
|
|
| could you explain what a stack is for me i've seen on other sites but it would be helpful if described in noobs term |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Sun May 24, 2009 9:18 am Post subject: |
|
|
Anytime you call a function, the return address and arguments have to be stored somewhere. The stack is a last-in, first-out buffer (or first-in, last-out) where such things are stored. The function in turn needs a place to save registers that are used by the function, and a place for temporary (local) variables. The stack is used for that as well. So the stack is a block of memory somewhere that is used by the thread for this temporary storage of addresses and registers and arguments and local variables.
The majority of the space is used by local variables. Consider this:
| Code: | int foo(int bar)
{
int myarray[1000];
int i;
for (i=0; i<1000; i++)
myarray[i] = i*i + 35;
return myarray[bar];
} |
It's not how you would write such a function, but I'm making a point. The point is that function foo() has a local variable called myarray that needs memory for 1000 ints (4000 bytes). Local variables are on the stack, so when foo() is called, 4000 bytes are taken from the stack for that array. Now suppose you only allocated a block of memory that is 1024 bytes for the stack - you don't have 4000 bytes for myarray.
One of the values you pass to the CreateThread function is the size of the block of memory to allocate for the stack. It is up to you to make sure it's big enough to hold everything needed by the functions called. So if you have arrays as local variables (in particular), be sure to make the stack bigger than the total space they occupy. |
|
| Back to top |
|
 |
|