Thread And Process Management example code in User library example code
Thread and process management example code
--------------------------------------------------------------------------------
TLS1dll: DLL implementing thread local storage (1)
Example code
Found in: examples\Base\ThreadsAndProcesses\TLS1
See TLS1exe: Thread local storage (1).
--------------------------------------------------------------------------------
TLS1exe: thread local storage (1)
--------------------------------------------------------------------------------
Example code
Found in: examples\Base\ThreadsAndProcesses\TLS1
// TLS1dll.cpp
//
// Copyright (C) Symbian Software Ltd 2000-2005. All rights reserved.
// DLL example program (1) to demonstrate Thread Local Storage (TLS).
// This DLL implements the two classes CSetter and CGeneral
//
// CSetter is used to set up the static text string, delete it, change it and display
// its content.
//
// CGeneral can show the content.
//
// The point being made here is that the text string is, effectively, static writeable data.
// It is accessible by all classes implemented in the DLL.
//
// CSetter is the only class which sets the static data in this example but, in general,
// any class implemented in the DLL can change/delete/fetch the data
// through the pointer in thread local storage - it is a matter of application design.
//
//
#include "TLS1dll.h"
#include
_LIT(KTxt1,"<>\n");
_LIT(KFormat1,"<%S>\n");
///////////////////////////////////////////////////////////////////////
//
// Class CSetter implementation
//
///////////////////////////////////////////////////////////////////////
// C++ constructor sets reference to the console in the
// initializer list.
// Body of constructor is empty.
// Constructor is exported because it is non-trivial
EXPORT_C CSetter::CSetter(CConsoleBase& aConsole)
: iConsole(aConsole)
{
}
// Destructor, deletes the static string
CSetter::~CSetter()
{
delete (HBufC*)Dll::Tls();
Dll::SetTls(NULL);
}
// Delete any existing static string; allocates a new HBufC
// and sets thread local storage to point to the HBufC.
EXPORT_C void CSetter::SetStaticTextL(const TDesC& aString)
{
delete (HBufC*)Dll::Tls();
HBufC* pD = aString.AllocL();
Dll::SetTls(pD);
}
// Show static text
EXPORT_C void CSetter::ShowStaticText() const
{
TDesC* text = ((TDesC*)Dll::Tls());
if (text)
iConsole.Printf(KFormat1, text);
else
iConsole.Printf(KTxt1);
}
///////////////////////////////////////////////////////////////////////
//
// Class CGeneral implementation
//
///////////////////////////////////////////////////////////////////////
// C++ constructor sets refrence to the console in the
// initializer list.
// Body of constructor is empty.
// Constructor is exported because it is non-trivial
EXPORT_C CGeneral::CGeneral(CConsoleBase& aConsole)
: iConsole(aConsole)
{
}
// Show static text
EXPORT_C void CGeneral::ShowStaticText() const
{
TDesC* text = ((TDesC*)Dll::Tls());
if (text)
iConsole.Printf(KFormat1, text);
else
iConsole.Printf(KTxt1);
}
//TLS1exe.cpp
// Copyright (C) Symbian Software Ltd 2000-2005. All rights reserved.
// An example to demonstrate Thread Local Storage (TLS)
//
// (See also the DLL implementation in TLS1dll.cpp)
// standard example header
#include "CommonFramework.h"
// dll header file
#include "TLS1dll.h"
//
// Common literal text
//
_LIT(KTxtNewLines,"\n\n");
// This function uses a statically loaded DLL to create a new CSetter object.
// It then uses this object to set up some static data accessed through
// thread-local storage.
// Thex example then shows how the static data can be accessed through
// the member functions of another class (CGeneral).
// All implementation code for these two classes is provided in the DLL
LOCAL_C void doExampleL()
{
// Introduction
_LIT(KTxtIntro,"Thread local storage example (1) \n\n");
console->Printf(KTxtIntro);
// Construct CSetter object, set up some static data via the
// thread local storage and use CSetter to show it.
CSetter* theSetter = new (ELeave) CSetter(*console);
CleanupStack::PushL(theSetter);
_LIT(KTxt1,"SOME STATIC TEXT");
theSetter->SetStaticTextL(KTxt1);
_LIT(KTxt2,"Static data set by CSetter object\n");
console->Printf(KTxt2);
_LIT(KTxt3,"Static data displayed by CSetter object...\n");
console->Printf(KTxt3);
theSetter->ShowStaticText();
// Construct a CGeneral object and see that it can show
// this static data. CGeneral knows nothing about the CSetter object
CGeneral* theGeneral = new (ELeave) CGeneral(*console);
CleanupStack::Pop();
_LIT(KTxt4,"Static data now accessed by CGeneral object...\n");
console->Printf(KTxt4);
theGeneral->ShowStaticText();
console->Printf(KTxtNewLines);
// Delete the CSetter object and then use the CGeneral object to
// try and show static data - there should be none.
delete theSetter;
_LIT(KTxt5,"Static data accessed again by CGeneral object after deletion of data...\n");
console->Printf(KTxt5);
theGeneral->ShowStaticText();
console->Printf(KTxtNewLines);
// tidy up before finishing the example
delete theGeneral;
}
--------------------------------------------------------------------------------
Description
This example shows the use of thread local storage as implemented by the statically linked DLL built by the TLS1dll example. TLS1exe is the executable which uses this DLL.
--------------------------------------------------------------------------------
Build Notes
TLS1dll must be built first.
--------------------------------------------------------------------------------
Classes used
DLL: thread relative DLL functions
--------------------------------------------------------------------------------
Security issues
The example requires no specific capabilities in order to run - and does not demonstrate any security issues.
--------------------------------------------------------------------------------
TLS1dll: DLL implementing thread local storage (1)
Example code
Found in: examples\Base\ThreadsAndProcesses\TLS1
See TLS1exe: Thread local storage (1).
--------------------------------------------------------------------------------
TLS1exe: thread local storage (1)
--------------------------------------------------------------------------------
Example code
Found in: examples\Base\ThreadsAndProcesses\TLS1
// TLS1dll.cpp
//
// Copyright (C) Symbian Software Ltd 2000-2005. All rights reserved.
// DLL example program (1) to demonstrate Thread Local Storage (TLS).
// This DLL implements the two classes CSetter and CGeneral
//
// CSetter is used to set up the static text string, delete it, change it and display
// its content.
//
// CGeneral can show the content.
//
// The point being made here is that the text string is, effectively, static writeable data.
// It is accessible by all classes implemented in the DLL.
//
// CSetter is the only class which sets the static data in this example but, in general,
// any class implemented in the DLL can change/delete/fetch the data
// through the pointer in thread local storage - it is a matter of application design.
//
//
#include "TLS1dll.h"
#include
_LIT(KTxt1,"<>\n");
_LIT(KFormat1,"<%S>\n");
///////////////////////////////////////////////////////////////////////
//
// Class CSetter implementation
//
///////////////////////////////////////////////////////////////////////
// C++ constructor sets reference to the console in the
// initializer list.
// Body of constructor is empty.
// Constructor is exported because it is non-trivial
EXPORT_C CSetter::CSetter(CConsoleBase& aConsole)
: iConsole(aConsole)
{
}
// Destructor, deletes the static string
CSetter::~CSetter()
{
delete (HBufC*)Dll::Tls();
Dll::SetTls(NULL);
}
// Delete any existing static string; allocates a new HBufC
// and sets thread local storage to point to the HBufC.
EXPORT_C void CSetter::SetStaticTextL(const TDesC& aString)
{
delete (HBufC*)Dll::Tls();
HBufC* pD = aString.AllocL();
Dll::SetTls(pD);
}
// Show static text
EXPORT_C void CSetter::ShowStaticText() const
{
TDesC* text = ((TDesC*)Dll::Tls());
if (text)
iConsole.Printf(KFormat1, text);
else
iConsole.Printf(KTxt1);
}
///////////////////////////////////////////////////////////////////////
//
// Class CGeneral implementation
//
///////////////////////////////////////////////////////////////////////
// C++ constructor sets refrence to the console in the
// initializer list.
// Body of constructor is empty.
// Constructor is exported because it is non-trivial
EXPORT_C CGeneral::CGeneral(CConsoleBase& aConsole)
: iConsole(aConsole)
{
}
// Show static text
EXPORT_C void CGeneral::ShowStaticText() const
{
TDesC* text = ((TDesC*)Dll::Tls());
if (text)
iConsole.Printf(KFormat1, text);
else
iConsole.Printf(KTxt1);
}
//TLS1exe.cpp
// Copyright (C) Symbian Software Ltd 2000-2005. All rights reserved.
// An example to demonstrate Thread Local Storage (TLS)
//
// (See also the DLL implementation in TLS1dll.cpp)
// standard example header
#include "CommonFramework.h"
// dll header file
#include "TLS1dll.h"
//
// Common literal text
//
_LIT(KTxtNewLines,"\n\n");
// This function uses a statically loaded DLL to create a new CSetter object.
// It then uses this object to set up some static data accessed through
// thread-local storage.
// Thex example then shows how the static data can be accessed through
// the member functions of another class (CGeneral).
// All implementation code for these two classes is provided in the DLL
LOCAL_C void doExampleL()
{
// Introduction
_LIT(KTxtIntro,"Thread local storage example (1) \n\n");
console->Printf(KTxtIntro);
// Construct CSetter object, set up some static data via the
// thread local storage and use CSetter to show it.
CSetter* theSetter = new (ELeave) CSetter(*console);
CleanupStack::PushL(theSetter);
_LIT(KTxt1,"SOME STATIC TEXT");
theSetter->SetStaticTextL(KTxt1);
_LIT(KTxt2,"Static data set by CSetter object\n");
console->Printf(KTxt2);
_LIT(KTxt3,"Static data displayed by CSetter object...\n");
console->Printf(KTxt3);
theSetter->ShowStaticText();
// Construct a CGeneral object and see that it can show
// this static data. CGeneral knows nothing about the CSetter object
CGeneral* theGeneral = new (ELeave) CGeneral(*console);
CleanupStack::Pop();
_LIT(KTxt4,"Static data now accessed by CGeneral object...\n");
console->Printf(KTxt4);
theGeneral->ShowStaticText();
console->Printf(KTxtNewLines);
// Delete the CSetter object and then use the CGeneral object to
// try and show static data - there should be none.
delete theSetter;
_LIT(KTxt5,"Static data accessed again by CGeneral object after deletion of data...\n");
console->Printf(KTxt5);
theGeneral->ShowStaticText();
console->Printf(KTxtNewLines);
// tidy up before finishing the example
delete theGeneral;
}
--------------------------------------------------------------------------------
Description
This example shows the use of thread local storage as implemented by the statically linked DLL built by the TLS1dll example. TLS1exe is the executable which uses this DLL.
--------------------------------------------------------------------------------
Build Notes
TLS1dll must be built first.
--------------------------------------------------------------------------------
Classes used
DLL: thread relative DLL functions
--------------------------------------------------------------------------------
Security issues
The example requires no specific capabilities in order to run - and does not demonstrate any security issues.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home