Skip to main content
gamedev.net
🔒 Locked

CreateThread()

Started by Sand_Hawk 0 replies 250+ views
Original Post
Sand_Hawk
Sand_Hawk
I am having a member function of my class CServer called ClientHandler. It just handles one client per thread. To made it work with CreateThread I had to make it static. But what happens if I spawn 4 threads? Do these threads share the data since the function is static or do they have their own data since it spawns a seperate thread with an own stack? Sand Hawk ---------------- -Earth is 98% full. Please delete anybody you can.
My Site
----------------(Inspired by Pouya)
Andrew Russell
Andrew Russell
as you said on TA:


HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes
DWORD dwStackSize,` `` `` `` ` // initial thread stack size
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
LPVOID lpParameter,` `` `` `` ` // argument for new thread
DWORD dwCreationFlags,` `` `` `` ` // creation flags
LPDWORD lpThreadId` `` `` `` ` // pointer to receive thread ID
);


What happens is your function you pass in lpStartAddress is called with the single parameter lpParameter, which can be a pointer to whatever you want.

The solution is to pass a pointer to the CServer in use to the ClientHandler function.

You can then either use the ClientHandler function to do your bidding on the pointer you sent, or use it to call a member function of the class. Just remember to cast properly.

For example:


static DWORD WINAPI ClientHandler(LPVOID lpParameter)
{
return ((CServer *) lpParameter)->Run();
}


And then use the function Run(), inside CServer to do what you want.


The advantage is that you can change CServer to CThread, and make Run a pure virtual method. Then inherate CServer and whatever other threaded stuff you need from CThread, and override Run().

Too easy.




Do not meddle in the affairs of moderators, for they are subtle and quick to anger.


ANDREW RUSSELL STUDIOS
Cool Links :: [ GD | TG | MS | NeHe | PA | SA | M&S | TA | LiT | H*R ]
Got Clue? :: [ Start Here! | Google | MSDN | GameDev.net Reference | OGL v D3D | File Formats | Go FAQ yourself ]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.