Threads Question

Recommended Posts

I have a class that when started will spawn a certain number of threads (depending on input at object istantiation).
The class has a function called StartThreads. Inside of there I am calling the CreateThread method. I am having trouble getting CreateThread() to recognize the thread procedure I am passing to it. Here is the function call.
cBaseServer::threadProc is of course a member of the class and the function I need the thread to use.

The void* parameter passed to threadProc is just temporary. I have a struct member in the class that I will pass in as a parameter, but I seem to get even more errors when I do not pass a void* to the actual function. I have read that because C++ is so strongly typed that it is difficult (but possible) to create a thread within an object. I know it can be done, I have just not found a snipet of code showing the exact method of casting.
Any help on this one?
Thanks,
Webby

0

Share this post

Link to post

Share on other sites

That won''t work unless threadProc is static, and perhaps not even then... It will have issues if there is a this pointer being passed implicitly to the threadproc If you''re doing this to make some kind of server that has to receive from multiple clients, have you looked at the select() function? It''s a no-threads way to do it.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets.""Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

The constructer here creates a thread. The two parameters are the initial function for the thread to start in, and a parameter to pass to that function. The constructor chooses to pass a pointer to the object being constructed (this). ThreadLauncher knows that the void* passed is really a foo, so it casts it and then calls EventLoop.

For safety, you could add a dynamic_cast<> to ThreadLauncher() to make sure you''re really looking at a foo*.

Tony

0

Share this post

Link to post

Share on other sites

I''ve looked at select and it won''t work for what I am trying to do. There has to be a way to do this. Makes no real sense why it wouldn''t be allowed by the language. It''s just a matter of casting correctly.

Share this post

Link to post

Share on other sites

I know it''s ugly, but I bet a C-style cast would work like a charm reinterpret_cast always drives me nuts...

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets.""Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Share this post

Link to post

Share on other sites

Here is some example thread code from the MSDN Library.With a few changes, this code works perfectly in a simple main program with the ThreadFunc() defined outside of main as usual.However, if I wrap this into a class by making the default constructor to the tread setup and such as below and pass it the address of the ThreadFunc() as normal then I get the error stated previously:C2664: 'CreateThread' : cannot convert parameter 3 from 'unsigned long (void)' to 'unsigned long (__stdcall *)(void *)'I get this same error whether I call the thread function directly or if I go through the process of making the DispatchThread and calling the real thread through the parameter as in the example below.

Can anyone out there see the problem with this? It has to be a simple way of casting the function pointer but I'll be dashed if I can figure it out.

except I make the function ThreadDispatch outside of the class.It compiled. I'm sure is has bugs but I'm fixing to start running some tests on it in just a few minutes.

I would still like a way to have CreateThread accept a pointer to a function inside of the same class so that I can keep everything inside of once nice box. I'd also like to be able to make ThreadDispatch private so that outsiders cannot have access to it. Simply including it as private generated even more nasty errors I can not sort out at this time.

So, until I find more information of these casting problems I will have to deal with a partially closed black box.

I have found websites that say this sort of thing is possible but I'll be darned if I can make anything from the sites work for my benefit.Here's the link if anyone is interested in helping.

Share this post

Link to post

Share on other sites

Ahh, no didn''t catch that. I saw the comment about static and while that did work I don''t think it would work correctly. So I guess I''ll be simply leaving the calling functions as global though I''d still rather not. But if there''s no other way then oh well.

I can go back to not needing the Dispatch function and can simply put my actual functions outside of the class and let CreateThread have access to them. I''ll just pass in the argument structure directly as well.

I guess it''s only a small break in the encapsulation. Essentially, the whole class I am creating will require no other user unput other than to create the object by passing in the desired parameters. After that it will function without no other input or access from the user. Just a simple declaration and voila, functioning network setup with however many reveiving threads as desired.

It works with a pool of worker threads to service incoming packets. I MIGHT later on add the functionality of having it sleep and wake threads as necessary based on an average number of incoming packets per unit time.

The only user input I may add later would be the ability to interactively add or remove threads to the pool at run time but I may not even need that functionality.