473,803 Members | 3,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function Pointers

Airslash
221 New Member
Touchy subject I know, but I'm currently trying some stuff to understand Threads better in C++.

I've read the Windows API documentation regarding Threads, and want to build a Thread class that can run any kind of function as a seperate thread by utilising function pointers.
This is the code I currently have so far:

header file
Expand|Select|Wrap|Line Numbers
  1. //---------------------------------------------------------------------------
  2. #ifndef ThreadH
  3. #define ThreadH
  4. #include <windows.h>
  5.  
  6. /*
  7.     Windows Definitions of the types
  8.     DWORD = unsigned long
  9.     SIZE_T = unsigned int
  10.     LPVOID = void*
  11. */
  12.  
  13. class Thread
  14. {
  15.     public:
  16.         Thread(int* FunctionAddress, int* ParamList);
  17.         ~Thread();
  18.  
  19.         bool Execute(bool Suspend);
  20.  
  21.         void StartThread();
  22.  
  23.     protected:
  24.         static DWORD WINAPI ThreadMainFunction(void* Param);
  25.     private:
  26.         bool mSuspended;
  27.         int* mFunctionAddress;
  28.         int* mParamList;
  29.         unsigned long* mThreadId;
  30.         HANDLE mMyThread;
  31. };
  32. //---------------------------------------------------------------------------
  33. #endif
  34.  
source file
Expand|Select|Wrap|Line Numbers
  1. //---------------------------------------------------------------------------
  2. #pragma hdrstop
  3. #include "Thread.h"
  4.  
  5. // This function is the constructor of the thread.
  6. // It takes 2 arguments, both are a pointer.
  7. // FunctionAddress is a pointer to the function beeing
  8. // called.
  9. // Paramlist is a the data passed as func arg.
  10. Thread::Thread(int* FunctionAddress, int* ParamList)
  11. {
  12.     // Assign the variables to our internal datamembers
  13.     mFunctionAddress = FunctionAddress;
  14.     mParamList = ParamList;
  15. }
  16. //---------------------------------------------------------------------------
  17. Thread::~Thread()
  18. {}
  19. //---------------------------------------------------------------------------
  20. // This function creates the thread and starts it.
  21. bool Thread::Execute(bool Suspend = false)
  22. {
  23.     // Store if we are suspended or not.
  24.     mSuspended = Suspend;
  25.  
  26.     // Create the Thread.
  27.     if(true == Suspend)
  28.     {
  29.         mMyThread = ::CreateThread(0, 0, ThreadMainFunction, this, CREATE_SUSPENDED, mThreadId);
  30.     }
  31.     else
  32.     {
  33.         mMyThread = ::CreateThread(0, 0, ThreadMainFunction, this, 0, mThreadId);
  34.     }
  35.  
  36.     // Return the assing result of myThread.
  37.     return (0 != mMyThread);
  38. }
  39. //---------------------------------------------------------------------------
  40. // This function starts the thread if it hasn't started already.
  41. void Thread::StartThread()
  42. {
  43.     if((true == mSuspended) && (0 != mMyThread))
  44.     {
  45.         ::ResumeThread(mMyThread);
  46.     }
  47. }
  48. //---------------------------------------------------------------------------
  49. // This function is the real mainloop of the thread.
  50. // Once this function returns the tread is ended.
  51. // Because Windows only accepts functions in the format
  52. // DWORD WINAPI FuncProc(LPVOID param), we create this
  53. // signature and call the function ourselves from within
  54. // this function.
  55. // Function is declared static to prevent compiler errors about
  56. // accessing data.
  57. DWORD WINAPI Thread::ThreadMainFunction(void* Param)
  58. {
  59.     // Cast our object back to a Thread
  60.     Thread* myself = static_cast<Thread*>(Param);
  61.  
  62.     // Call the actuall function to be executed.
  63.     return (*(myself->mFunctionAddress)(myself->mParamList));
  64. }
  65. //---------------------------------------------------------------------------
  66. #pragma package(smart_init)
  67.  
Current output from compiler:
[BCC32 Error] Thread.cpp(68): E2314 Call of nonfunction

And that points to the following line:
Expand|Select|Wrap|Line Numbers
  1. return (*(myself->mFunctionAddress)(myself->mParamList));
  2.  
Bit clueless on the topic, so could someone explain it tome what I'm doing wrong?
Jan 11 '10 #1
1 1576
Airslash
221 New Member
got the code compiled now, just testing needs to be done if what I wrote actually works....

had to change the declaration of the void* to the function adress into void* (*mFunctionAddr ess)(void *)
Jan 11 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3343
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to substitute the set<Data*> with a hash_set<Data*>: typedef hash_set<const Data*, hash<const Data*>, eqData> DataPointerHashSet; // typedef set<Data*> DataPointerHashSet; // (see complete code below) But it doesn't work! Everything is fine...
89
6538
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
3
2309
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void *, void*))(numeric ? numcmp : strcmp) ); I guess what interests me is the nameless function pointer and then the
41
10067
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed...
12
5472
by: Bill Pursell | last post by:
The following code generates a compiler warning when compiled with gcc -pedantic: typedef (*FUNC)(int); FUNC f; void * get_f(void) { return &f;
57
5684
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p expects type 'void *; but argument 2 has type 'void
54
24546
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
4
2507
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
4
3518
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token vector_static_function.c:21: error: expected constructor, destructor, or type conversion before '.' token
32
5685
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC and lccwin32 for example) which I consider to be perfectly reasonable: you can cast every kind of function pointer to a void pointer and void pointers to any kind of function pointer. This follows the general "generics through void scheme" of C....
0
9700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10546
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10292
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7603
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.