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 -
//---------------------------------------------------------------------------
-
#ifndef ThreadH
-
#define ThreadH
-
#include <windows.h>
-
-
/*
-
Windows Definitions of the types
-
DWORD = unsigned long
-
SIZE_T = unsigned int
-
LPVOID = void*
-
*/
-
-
class Thread
-
{
-
public:
-
Thread(int* FunctionAddress, int* ParamList);
-
~Thread();
-
-
bool Execute(bool Suspend);
-
-
void StartThread();
-
-
protected:
-
static DWORD WINAPI ThreadMainFunction(void* Param);
-
private:
-
bool mSuspended;
-
int* mFunctionAddress;
-
int* mParamList;
-
unsigned long* mThreadId;
-
HANDLE mMyThread;
-
};
-
//---------------------------------------------------------------------------
-
#endif
-
source file -
//---------------------------------------------------------------------------
-
#pragma hdrstop
-
#include "Thread.h"
-
-
// This function is the constructor of the thread.
-
// It takes 2 arguments, both are a pointer.
-
// FunctionAddress is a pointer to the function beeing
-
// called.
-
// Paramlist is a the data passed as func arg.
-
Thread::Thread(int* FunctionAddress, int* ParamList)
-
{
-
// Assign the variables to our internal datamembers
-
mFunctionAddress = FunctionAddress;
-
mParamList = ParamList;
-
}
-
//---------------------------------------------------------------------------
-
Thread::~Thread()
-
{}
-
//---------------------------------------------------------------------------
-
// This function creates the thread and starts it.
-
bool Thread::Execute(bool Suspend = false)
-
{
-
// Store if we are suspended or not.
-
mSuspended = Suspend;
-
-
// Create the Thread.
-
if(true == Suspend)
-
{
-
mMyThread = ::CreateThread(0, 0, ThreadMainFunction, this, CREATE_SUSPENDED, mThreadId);
-
}
-
else
-
{
-
mMyThread = ::CreateThread(0, 0, ThreadMainFunction, this, 0, mThreadId);
-
}
-
-
// Return the assing result of myThread.
-
return (0 != mMyThread);
-
}
-
//---------------------------------------------------------------------------
-
// This function starts the thread if it hasn't started already.
-
void Thread::StartThread()
-
{
-
if((true == mSuspended) && (0 != mMyThread))
-
{
-
::ResumeThread(mMyThread);
-
}
-
}
-
//---------------------------------------------------------------------------
-
// This function is the real mainloop of the thread.
-
// Once this function returns the tread is ended.
-
// Because Windows only accepts functions in the format
-
// DWORD WINAPI FuncProc(LPVOID param), we create this
-
// signature and call the function ourselves from within
-
// this function.
-
// Function is declared static to prevent compiler errors about
-
// accessing data.
-
DWORD WINAPI Thread::ThreadMainFunction(void* Param)
-
{
-
// Cast our object back to a Thread
-
Thread* myself = static_cast<Thread*>(Param);
-
-
// Call the actuall function to be executed.
-
return (*(myself->mFunctionAddress)(myself->mParamList));
-
}
-
//---------------------------------------------------------------------------
-
#pragma package(smart_init)
-
Current output from compiler:
[BCC32 Error] Thread.cpp(68): E2314 Call of nonfunction
And that points to the following line: -
return (*(myself->mFunctionAddress)(myself->mParamList));
-
Bit clueless on the topic, so could someone explain it tome what I'm doing wrong?
1 1576
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 *)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
by: Sweety |
last post by:
hi,
Is main function address is 657.
its show in all compiler.
try it & say why?
bye,
|
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
|
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...
|
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;
| |
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
|
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);
}
|
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
|
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
|
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....
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |