Senin, 14 Juli 2014

Xathrya Sabertooth

Xathrya Sabertooth


C++ Reserved Words

Posted: 14 Jul 2014 03:33 AM PDT

C++ reserves some words for its own use and for use in C++ libraries. Thus, we shouldn’t use a reserved word as an identifier in a declaration. Reserved words comes in two categories: keywords and alternative tokens.

This article will list C++ reserved words based on C++11 standard.

C++ Keywords

Keywords are identifiers that form the vocabulary or a programming language. By combining the keyword and following the grammar of C++, we do code with C++. These vocabulary may not be used for other purpose, such as serving as variable names.

The following list show C++’s keywords:

alignasexportsizeof
alignofexternstatic
asmfalsestatic_assert
autofloatstatic_cast
boolforstruct
breakfriendswitch
casegototemplate
catchifthis
charinlinethread_local
char16_tintthrow
char32_tlongtrue
classmutabletry
constnamespacetypedef
constexprnewtypeid
const_castnoexcepttypename
continuenullptrunion
decltypeoperatorunsigned
defaultprivateusing
deleteprotectedvirtual
dopublicvoid
doubleregistervolatile
dynamic_castreinterpret_castwchar_t
elsereturnwhile
enumshort
explicitsigned

Alternative Tokens

In addition to keywords, C++ has some alphabetic alternative representations of operators, termed alternative tokens. These, too, are reserved.

Token
Meaning
and&&
and_eq&=
bitand&
bitor|
compl~
not!
not_eq!=
or||
or_eq|=
xor^
xor_eq^=

Notes

The meaning of “auto” keyword has changed in C++11. In C++11 standard, the auto keyword is used to declare a variable with type inferred from the expression or value assigned to it.

In addition to keywords, there are two identifiers with special meaning, which maybe used as names of objects or functions, but have special meaning in certain contexts.

override  final

Also, each name that contains a double underscore __ or begins with an underscore followed by an uppercase letter is always reserved to the implementation and should not be used as an identifier. Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace; such names may be used as identifiers in user-defined namespaces, as names of class members, etc.

The name posix is reserved for a future top-level namespace. The behavior is undefined if a program declares or defines anything in that namespace.(since C++11)

C++11 Concurrency: Part 1 – Introduction to Threading in Standard Library

Posted: 03 Jul 2014 04:12 PM PDT

C++ is growing, as per new standard C++11 now it supports threading. C++11 introduce a new thread library. The new standard define utilities for starting and managing threads. It also contains utilities for synchronization like mutexes and other locks, atomic variables, and other utilities. We will get through this so called C++11 threading by series of articles “C++11 Concurrency”.

To compile the sample code, we need a C++11 compiler. In my case, I use GCC 4.8.1 (on Slackware Linux) or MinGW counterpart (on Windows 8.1). To activated C++11 support features, we need to pass the “-std=c++11″ option to GCC, for example:

g++ filename.cpp -o program -std=c++11

C++ Concurrency

see <atomic>, <condition_variable>, <future>, <mutex>, and <thread> for reference.

Starting from C++11, there are five more headers added to the standard library to support concurrency. They are:

  • atomic
  • condition_variable
  • future
  • mutex
  • thread

These headers are used for multithreading and concurrency. Before the language standard, we would use OS facilities such as POSIX thread or libraries like OpenMP and MPI to enable concurrency using C++.

The <atomic> header defines some definition to allow us using atomic operation. It encapsulate a value whose access is guaranteed to not cause data races and can be used to synchronize memory access among different threads.

The <condition_variable> header defines classes used as condition variable for synchronization.

The <future> header facilitates synchronization access to value.

The <mutex> header is used for allowing mutual exclusion (mutex) of concurrent execution of critical sections of code.

The <thread> header defines thread class, used for spawning threads on C++ and also manage threads as we like.

Starting Threads

see also: Multithreading Support in C++11

Starting a new thread is very easy. We need to create an instance of a std::thread and supply a function and thread will automatically be started and executing the function. The function we talk about can be a function pointer or even a lambda expression.

So first, let’s write a simple code to demonstrate the power of C++ thread.

#include <thread>  #include <iostream>    using std::cout;  using std::endl;  using std::thread;    void hello(){      cout << "Hello from thread " << endl;  }    int main(){      thread t1(hello);      t1.join();        return 0;  }

In the above snippet, we pass a function pointer to thread.

All the threads utilities can be found in the <thread> header. As we see, when constructing a thread instance, we supply a function which will be executed on the fly. The join() method is called to force current thread to wait for the other one. In other word, main thread won’t finish until t1 finish it’s task. If we omit this call, the result is undefined.

We can, however, create more than one thread. To distinguish each of thread, the std::thread class provide a get_id() method which returns an unique id for this thread. We can get a reference to the current thread by std::this_thread variable. Let’s dive in example:

#include <thread>  #include <iostream>  #include <vector>    using std::cout;  using std::endl;  using std::thread;  using std::vector;    void hello(){      cout << "Hello from thread " << this_thread::get_id() << endl;  }    int main(){      vector<thread> threads;        for (int i = 0; i < 5; ++i){          threads.push_back(thread(hello));      }        for (auto& thread : threads){          thread.join();      }        return 0;  }

Sample output:

Hello from thread 2  Hello from thread 4  Hello from thread 3  Hello from thread 5  Hello from thread 6

Our program will start thread one after one and then store them into a vector. This is common strategy to handle several threads.

The threads might interleave. We have no way to control the order of execution of threads. A thread can be preempted at any moment which might lead us to produce following result:

Hello from thread Hello from thread 6  Hello from thread 3  Hello from thread 5  2  Hello from thread 4

So, there is no certainty in which function will be executed first. All decision are taken by OS. The Operating System will decide when resource are available for our threads and execute them on whatever CPU is least occupied.

Aside from function pointer, we can also use lambda expression. Lambda execution is very useful when the executing code is very small thus we don’t necessary need to create a function just for that. We can rewrite the previous code as this one:

#include <thread>  #include <iostream>  #include <vector>    using std::cout;  using std::endl;  using std::thread;  using std::vector;    int main(){      vector<thread> threads;        for (int i = 0; i < 5; ++i){          threads.push_back(thread([]() {              cout << "Hello from thread " << this_thread::get_id() << endl;          }));      }        for (auto& thread : threads){          thread.join();      }        return 0;  }

Threading in Action

Now we will illustrate the power of parallel programming in C++11. We will do something simple yet interesting: parallel merge sort.

Tidak ada komentar:

Posting Komentar