Y
Yu
I have found that the static object is initialised at the time when
the shared libary is loaded. The initialisation caused the invocation
of the constructor.
May I know of any way that I can initialize the static object without
invoking the constructor?
Below is the sample coding.
Header file ASURegistrationManager.h
#include "ASURegistration.h"
class ASURegistrationManager
{
public:
static ASURegistration& GetASURegistration();
private:
static ASURegistration ASUReg; //The static object
ASURegistrationManager();
~ASURegistrationManager();
};
Header file ASURegistration.h
#include "ApplicationInterface.h"
class ASURegistration : public IApplicationInterface
{
public:
ASURegistration();
~ASURegistration();
};
Header file ApplicationInterface.h
class IApplicationInterface
{
public:
IApplicationInterface(); //This will spawn thread
~IApplicationInterface();
};
ASURegistration.cpp
#include "ASURegistration.h"
ASURegistration::ASURegistration() : IApplicationInterface()
{
}
ASURegistration::~ASURegistration()
{
}
ASURegistrationManager.cpp
#include "ASURegistrationManager.h"
ASURegistration ASURegistrationManager::ASUReg; //This initialization
invoke the IApplicationInterface constructor to create thread.
ASURegistrationManager::ASURegistrationManager()
{
}
ASURegistrationManager::~ASURegistrationManager()
{
}
ASURegistration& ASURegistrationManager::GetASURegistration()
{
return (ASURegistration &)ASUReg;
}
ApplicationInterface.cpp
#include "ApplicationInterface.h"
using namespace std;
void *APIProcessEvent(void *pvASUInterface)// Thread Function
{
while(true)
{
sleep(5);
}
return(0);
}
IApplicationInterface::IApplicationInterface()
{
pthread_t ProcessEventThread;
pthread_attr_t attr;
cout << "In IApplicationInterface\n";
if(pthread_attr_init(&attr) != 0)
{
std::cout<<"pthread_attr_init system call for thread
failed.\n";
}
if(pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM) != 0)
{
std::cout<<"pthread_attr_setscope system call for thread
failed.\n";
}
if(pthread_create(&ProcessEventThread,&attr,APIProcessEvent,NULL)
!= 0)
{
std::cout<<"Failed to create Process Event Thread.\n";
}
}
IApplicationInterface::~IApplicationInterface()
{
}
I have the cpp function that calls the static method
ASURegistrationManager::GetASURegistration().
#include <stdio.h>
#include "wrap.h"
#include "AClass.hpp"
#include "CppLibAPI.h" //This is just a combination header file of the
header files shown above.
using namespace std;
extern "C" {
void * AClass_new(int x)
{
ASURegistration& ASUReg =
ASURegistrationManager::GetASURegistration();
return ((void *)new AClass(x)); //AClass is a cpp class
}
}
The above cpp programs are compiled as a shared library .so to be used
by the C program.
The C program is as follow.
#include <stdio.h>
#include "wrap.h"
int main(void)
{
printf("main() starting\n");
return 0;
}
When I run the C program, the output is as follow:
In IApplicationInterface
main() starting
As you can see the IApplicationInterface constructor is called which
will spawn a thread before the C main function. The static method in
the AClass_new() cpp function caused the initialisation of the static
variable ASUReg which in turns invoke the IApplicationInterface
constructor. I need the thread to be spawned only at the point when
the cpp function AClass_new() is called instead of during program
initialisation. Is there other anyway to initialise the static object
such that the constructor is not invoked?
I have tried to use a static pointer. That will not cause the
invocation of the constructor at initialisation, because a pointer can
be initialised to null. But I have concerned on the static method's
return.
If I change the declaration to
static ASURegistration* ASUReg
so ASUReg can be initialised as
ASURegistration* ASURegistrationManager::ASUReg=NULL;
Is the following statement correct?
ASURegistration& ASURegistrationManager::GetASURegistration()
{
return (ASURegistration &)*ASUReg; //Will this cause any problem?
}
Thank you!
the shared libary is loaded. The initialisation caused the invocation
of the constructor.
May I know of any way that I can initialize the static object without
invoking the constructor?
Below is the sample coding.
Header file ASURegistrationManager.h
#include "ASURegistration.h"
class ASURegistrationManager
{
public:
static ASURegistration& GetASURegistration();
private:
static ASURegistration ASUReg; //The static object
ASURegistrationManager();
~ASURegistrationManager();
};
Header file ASURegistration.h
#include "ApplicationInterface.h"
class ASURegistration : public IApplicationInterface
{
public:
ASURegistration();
~ASURegistration();
};
Header file ApplicationInterface.h
class IApplicationInterface
{
public:
IApplicationInterface(); //This will spawn thread
~IApplicationInterface();
};
ASURegistration.cpp
#include "ASURegistration.h"
ASURegistration::ASURegistration() : IApplicationInterface()
{
}
ASURegistration::~ASURegistration()
{
}
ASURegistrationManager.cpp
#include "ASURegistrationManager.h"
ASURegistration ASURegistrationManager::ASUReg; //This initialization
invoke the IApplicationInterface constructor to create thread.
ASURegistrationManager::ASURegistrationManager()
{
}
ASURegistrationManager::~ASURegistrationManager()
{
}
ASURegistration& ASURegistrationManager::GetASURegistration()
{
return (ASURegistration &)ASUReg;
}
ApplicationInterface.cpp
#include "ApplicationInterface.h"
using namespace std;
void *APIProcessEvent(void *pvASUInterface)// Thread Function
{
while(true)
{
sleep(5);
}
return(0);
}
IApplicationInterface::IApplicationInterface()
{
pthread_t ProcessEventThread;
pthread_attr_t attr;
cout << "In IApplicationInterface\n";
if(pthread_attr_init(&attr) != 0)
{
std::cout<<"pthread_attr_init system call for thread
failed.\n";
}
if(pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM) != 0)
{
std::cout<<"pthread_attr_setscope system call for thread
failed.\n";
}
if(pthread_create(&ProcessEventThread,&attr,APIProcessEvent,NULL)
!= 0)
{
std::cout<<"Failed to create Process Event Thread.\n";
}
}
IApplicationInterface::~IApplicationInterface()
{
}
I have the cpp function that calls the static method
ASURegistrationManager::GetASURegistration().
#include <stdio.h>
#include "wrap.h"
#include "AClass.hpp"
#include "CppLibAPI.h" //This is just a combination header file of the
header files shown above.
using namespace std;
extern "C" {
void * AClass_new(int x)
{
ASURegistration& ASUReg =
ASURegistrationManager::GetASURegistration();
return ((void *)new AClass(x)); //AClass is a cpp class
}
}
The above cpp programs are compiled as a shared library .so to be used
by the C program.
The C program is as follow.
#include <stdio.h>
#include "wrap.h"
int main(void)
{
printf("main() starting\n");
return 0;
}
When I run the C program, the output is as follow:
In IApplicationInterface
main() starting
As you can see the IApplicationInterface constructor is called which
will spawn a thread before the C main function. The static method in
the AClass_new() cpp function caused the initialisation of the static
variable ASUReg which in turns invoke the IApplicationInterface
constructor. I need the thread to be spawned only at the point when
the cpp function AClass_new() is called instead of during program
initialisation. Is there other anyway to initialise the static object
such that the constructor is not invoked?
I have tried to use a static pointer. That will not cause the
invocation of the constructor at initialisation, because a pointer can
be initialised to null. But I have concerned on the static method's
return.
If I change the declaration to
static ASURegistration* ASUReg
so ASUReg can be initialised as
ASURegistration* ASURegistrationManager::ASUReg=NULL;
Is the following statement correct?
ASURegistration& ASURegistrationManager::GetASURegistration()
{
return (ASURegistration &)*ASUReg; //Will this cause any problem?
}
Thank you!