PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Using threads

Hi,

Has anybody used threads with PocketFrog?

I'm trying to kick off a thread to read data out of files and blit information to a surface that the game loop is blitting to the display.

I've tried this:
Code:

m_thread = CreateThread(NULL, 0, update, 0, &m_threadId);


where the update() function is defined as:

Code:
DWORD update(LPVOID lpvoid)


I get a compiler error, "error C2664: 'CreateThread' : cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned long (__cdecl *)(void *)'
None of the functions with this name in scope match the target type"

This is my first time to try any threaded programming, so I'm sure I'm doing something wrong. Anybody have any suggestions? I'm missing something in my definition of update?

Thanks,

Pam
Out of curiosity, if you type it:
unsigned long update(LPVOID lpvoid)

instead of:
DWORD update(LPVOID lpvoid)

what happens?

I haven't used threads with PF. Version 0.4.5 required it to be compiled as multi-threaded, but that's the closest I've come.

-John
Well, that gives me the error, "error C2664: 'CreateThread' : cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned long (__cdecl *)(void *)'
None of the functions with this name in scope match the target type"

Which leads me to the (__cdecl *) part. I don't know what that is.

Pam
Sorry. I misread the earlier post. It's saying it _wants_ the (__cdecl *)... Not sure if this'll fix it, but try adding an & in front of update. It wants a pointer to the function.
Code:
m_thread = CreateThread(NULL, 0, &update;, 0, &m;_threadId);
Ok, that gives me, "error C2276: '&' : illegal operation on bound member function expression"

I just know I'm missing something basic here.

Thanks,

Pam
From port.c in Windows CE Tools\wce300\MS Pocket PC\samples\win32\tty

Code:
// Create a read thread for reading data from the communication port.
if (hReadThread = CreateThread (NULL, 0, PortReadThread, 0, 0,
&dwThreadID))
{
CloseHandle (hReadThread);
}

...snip...

DWORD PortReadThread (LPVOID lpvoid)
{
...snip...
}



This is what I was basing my code on. Maybe it is because I'm trying to call a class member? hmmm.

Thanks,

Pam
The short version:

try (LPTHREAD_START_ROUTINE)update

The long version:

I'm using several threads in my project. I have even rewritten the Game class to use a thread to update the window so one can create more than one window (but I've heard that this only works in GDI mode since GAPI doesn't support windowed mode. Someone please verify this).

Anyways.. I used the following tutorial

http://www.flipcode.com/tutorials/tut_mthreading.shtml

and based my Thread class on the one presented in the tutorial. Every thread I create in the project is a class that inherits from my Thread class. This maybee a bit overkill for your purpose, but nevertheless its always a good way to encapsulate code in objects.

If you're interested in how I did it you can look at the source documentation and reference at

http://www.nada.kth.se/~t98_hes/cas/gui/html

The sourcefiles are there to, but you need to do a copy-paste from the html file reference.
also, where is your update beeing defined?? it MUST be a static function
Excellent. Thank you, Adde. I need to read all of that info and get an understanding of threading. Great info.

Thanks,

Pam
Is your update function a method of Game? As someone said earlier, the function must be static, which gave me fits in my current project (I have a thread that does all my graphics and sound loading at the same time GameLoop() executes to show a loading screen). Here's what I did:

Code:

// MyGame is of course the main game class derived from Game

// In GameInit(), I have:
HANDLE hThread = CreateThread(NULL, 0, LoadingThreadFunc, this, 0, NULL);
SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL);

// Then I have:
DWORD WINAPI MyGame::LoadingThreadFunc(LPVOID param) {
MyGame* pMG = (MyGame*)param;
return pMG->LoadingThreadMemberFunc();
}

// Finally, I have:

DWORD MyGame::LoadingThreadMemberFunc() {
// Loading code goes here
}

Don't know if that's applicable to your situation, but I hope it helps anyway. In any case, if the question of having a member function act as a thread function comes up, there's the answer.
I'm shure most of us understand what you ment, but as long as we're supplying code it might as well be correct. I'm not trying to be picky, just thought I'd clearify it a bit.

The thread function that you bind to your thread MUST be static, which means that it can't be defined inside a class (or perhapps it could if you define it as static but then all objects would try to run the same code and that's not what we want).

So, how do we get the thread to run a member function? Here's where the 4:th parameter to CreateThread() comes in. This parameter is sent to the thread function as a void pointer (called param in the code above). So if we pass our class to the CreateThread() function (i.e. send our this pointer as the 4:th parameter) we can retrieve it in the static thread function by explicit casting. The only thing left is to use the this pointer and call the member function.

So, to make the example code above work, one must remove the MyGame:: specifier before LoadingThreadFunc() and define it outside of the MyGame class. Just a minor typo by Frank I'm shure.

Anyways.. for more examples and more information about this, see the two links I provided above. The tutorial basicly contains the same thing as this note combined with FZammettis note but perhapps in a more structurized form (i.e. more concise) and the Thread class I wrote for my project could prove to be usefull even for you guys.

For the one who hates indirect links, here's the direct link to my Thread class:
http://www.student.nada.kth.se/~t98_hes ... ource.html
http://www.student.nada.kth.se/~t98_hes ... ource.html
I copied that code DIRECTLY out of my program, which I should point out works exactly as I indicated it does. I suggest giving it a try yourself, you'll see it works as shown.

Perhaps what I wrote wasn't clear however as I read it again... The two methods shown are in fact defined outside the class definition, which I think is what you were objecting to. If so it was my fault for not being clear.
Hmm, ok. Cool.

What I ment to say was that you don't have to define the LoadingThreadFunc() as part of your class at all. You can just define it at the top of your file (in which you create the thread).

BUT, having thought about this I must agree that your way is much better. Defining the LoadingThreadFunc() as a private static function will hide the function from the world, which is exactly what we want. Global functions (and members in perticular) should be avoided as far as possible.

So, the mistake was clearly mine and I will change the code of my Thread class immediately.

Thanks Frank.