• No results found

C++ Development .8 Namespaces

Static Libraries, Shared Libraries, and Plug-Ins

INCLUDE_CPLUS_DEMANGLER

5 C++ Development .8 Namespaces

GNU Compiler

The GNU C++ compiler options controlling multiple instantiation of templates are:

-fimplicit-templates

This option is the default. Template instantiations and out-of-line copies of inline functions are put into special linkonce sections. Duplicate sections are merged by the linker, so that each instantiated template appears only once in the output file.

-fno-implicit-templates

This is the option for explicit instantiation. Using this strategy explicitly instantiates any templates that you require.

5.7.2 Run-Time Type Information

Both compilers support Run-time Type Information (RTTI), and the feature is enabled by default. This feature adds a small overhead to any C++ program containing classes with virtual functions.

For the Wind River Compiler, the RTTI language feature can be disabled with the -Xrtti-off flag.

For the GNU compiler, the RTTI language feature can be disabled with the -fno-rtti flag.

5.8 Namespaces

Both the Wind River and GNU C++ compilers supports namespaces. You can use namespaces for your own code, according to the C++ standard.

The C++ standard also defines names from system header files in a namespace called std. The standard requires that you specify which names in a standard header file you will be using.

The following code is technically invalid under the latest standard, and will not work with this release. It compiled with a previous release of the GNU compiler, but will not compile under the current releases of either the Wind River or GNU C++ compilers:

#include <iostream.h>

int main() {

cout << "Hello, world!" << endl;

}

The following examples provide three correct alternatives illustrating how the C++ standard would now represent this code. The examples compile with either the Wind River or the GNU C++ compiler:

// Example 1

#include <iostream>

int main() {

std::cout << "Hello, world!" << std::endl;

}

// Example 2

#include <iostream>

using std::cout;

using std::endl;

int main() {

cout << "Hello, world!" << endl;

}

// Example 3

#include <iostream>

using namespace std;

int main() {

cout << "Hello, world!" << endl;

}

5.9 C++ Exception Handling

C++ exception handling is provided by the INCLUDE_CPLUS_LANG component.

To disable exception handling for user code, do the following:

1. Define the environment variable CXX_STL_VARIANT=abridged before creating the VxWorks Image Project (VIP). This ensures that the correct (exception-disabled) libraries are linked against.

2. Add -mc++-abr for GNU or -Xc++-abr for Diab to the compiler options.

5.10 C++ Demo Example

For a sample C++ application, see

installDir/vxworks-6.x/target/usr/apps/samples/cplusplus/factory.

6

Multitasking

6.1 Introduction 95

6.2 About Tasks and Multitasking 96 6.3 VxWorks System Tasks 99 6.4 Task Scheduling 104

6.5 Task Creation and Management 108 6.6 Task Error Status: errno 117

6.7 Task Exception Handling 118 6.8 Shared Code and Reentrancy 118

6.1 Introduction

Modern real-time systems are based on the complementary concepts of

multitasking and intertask communications. A multitasking environment allows a real-time application to be constructed as a set of independent tasks, each with its own thread of execution and set of system resources. Tasks are the basic unit of scheduling in VxWorks. All tasks, whether in the kernel or in processes, are subject to the same scheduler (VxWorks processes are not themselves scheduled).

For information about POSIX thread support for VxWorks, see 9. POSIX Facilities.

NOTE: This chapter provides information about facilities available for real-time processes. For information about facilities available in the VxWorks kernel, see the corresponding chapter in the VxWorks Kernel Programmer’s Guide.

6.2 About Tasks and Multitasking

VxWorks tasks are the basic unit of code execution in the operating system itself, as well as in applications that it executes as processes. In other operating systems the term thread is used similarly. (For information about VxWorks support for POSIX threads, see 9.13 POSIX Threads, p.179).

Multitasking provides the fundamental mechanism for an application to control and react to multiple, discrete real-world events. The VxWorks real-time kernel provides the basic multitasking environment. On a uniprocessor system multitasking creates the appearance of many threads of execution running concurrently when, in fact, the kernel interleaves their execution on the basis of a scheduling policy.

Each task has its own context, which is the CPU environment and system resources that the task sees each time it is scheduled to run by the kernel. On a context switch, a task’s context is saved in the task control block (TCB).

A task’s context includes:

a thread of execution; that is, the task’s program counter

the tasks’ virtual memory context (if process support is included)

the CPU registers and (optionally) coprocessor registers

stacks for dynamic variables and function calls

I/O assignments for standard input, output, and error

a delay timer

a time-slice timer

kernel control structures

signal handlers

error status (errno)

debugging and performance monitoring values

Note that—consistent with the POSIX standard—all tasks in a process share the same environment variables (unlike kernel tasks, which each have their own set of environment variables).

For more information about virtual memory contexts, see the VxWorks Kernel Programmer’s Guide: Memory Management.

6.2.1 Task States and Transitions

The kernel maintains the current state of each task in the system. A task changes from one state to another as a result of activity such as certain function calls made by the application (for example, when attempting to take a semaphore that is not available) and the use of development tools such as the debugger.

NOTE: The POSIX standard includes the concept of a thread, which is similar to a task, but with some additional features. For details, see 9.13 POSIX Threads, p.179.

6 Multitasking