Advertisement

Help with passing function pointers...

Started by July 01, 2000 06:43 PM
2 comments, last by Cobalt 24 years, 5 months ago
Hi there, I am trying to write a class to encapsulate simple multithreading stuff. Right now, I want to be able to pass the function to my thread class via a pointer through the class'' constructor function. I don''t know if I said that all right, but here are some code snipets (Most of my comments are where my questions lie)... /* My function, that I want to be its own thread... */ DWORD WINAPI count(LPVOID n) { return (DWORD)n; } ========================= /* In main(), this instantiates a new thread object, and I want to be able to give it the count function name, as a pointer to the function... */ thread *newThread; newThread = new thread(count); ========================= The Constructor of thread ========================= thread(DWORD *threadFunction) /* This is where the problem lies I think, I don''t know how to make it recognize count as the type of pointer it is... */ { threadID = 0; threadHandle = NULL; threadIsRunning = false; begin(threadFunction); /* And is this the way to pass the pointer of the function to another function??? */ } Well, the error I get in VC++ goes something like this: D:\Documents\Programming\Projects\thread Class Test\main.cpp(109) : error C2664: ''thread::thread(unsigned long *)'' : cannot convert parameter 1 from ''unsigned long (__stdcall *)(void *)'' to ''unsigned long *'' I hope you all can help, thanks a bunch!
Try changing thread(DWORD *threadFunction) to this:
thread(unsigned long threadFunction(void *))

I think that will do what you want it to..

// CHRIS
// CHRIS [win32mfc]
Advertisement
Well, that kinda did something. I changed it to what you said, but I still get an error...

--------------------Configuration: thread Class Test - Win32 Debug--------------------
Compiling...
main.cpp
D:\Documents\Programming\Projects\thread Class Test\main.cpp(109) : error C2664: ''thread::thread(unsigned long (__cdecl *)(void *))'' : cannot convert parameter 1 from ''unsigned long (void *)'' to ''unsigned long (__cdecl *)(void *)''
Error executing cl.exe.

thread Class Test.exe - 1 error(s), 0 warning(s)

It looks to me like the WINAPI specifier in fron of the count function (it is, DWORD WINAPI count(LPVOID n)) is the thing that is screwing it up. If I change the WINAPI to __cdecl, or remove the WINAPI, it compiles. However, at this point it is useless, cause threads have to have WINAPI in front of the function... Any more suggestions?
quote: Original post by Cobalt
        =========================The Constructor of thread=========================thread(DWORD *threadFunction)  /* This is where the problem lies I think, I don't know how to make it recognize count as the type of pointer it is... */{	threadID = 0;	threadHandle = NULL;	threadIsRunning = false;	begin(threadFunction); /* And is this the way to pass the pointer of the function to another function??? */}[/source] <hr height=1 noshade></BLOCKQUOTE></font> remove the * infront of the function you are passingFunction names are already pointers.thread(DWORD threadfunction);create a [source]typedef FuncPtr DWORD threadFunc();        

and use it

btw, a DWORD *is* an unsigned long (on a 32 bit machine)

Edited by - Magmai Kai Holmlor on July 2, 2000 2:21:19 AM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement