Skip to main content

Posts

Showing posts from 2014

C++11 Multithreading - How to avoid race conditions

This article presents how to avoid problem of Race conditions described in this article. As we already know race conditions can occur when two or more threads are trying to invoke some part of code or use the same resource (ex. variable) at the time. That can cause problem of unexpected results. We could see that problem in 'Race conditions' article where two threads was able to modify the same variable value which caused different program output every time we invoked it. In order to avoid that problem we should synchronize operations of our threads using multithreading mechanism called mutexes (abbr. Mutual exclusion ). Mutex is some kind of program flow control lock which which is able to make sure that some part of code locked by itself can be invoked by only one thread at the same time. Such locked part of code is called critical section . When other thread will try to go into critical section it will be blocked and will need to wait until previous thread

Software Adventure - new blog name

I decided to change name of this blog, because I am going to present not only C++ topics here. From today you can expect topics describing problems and features of following domains of software development: C++ programming language features, problems, issues C  programming language features, problems, issues Unix/Linux programming (ex. standard Linux C library, system calls etc.) Embedded Linux programming (problems and solutions related to Embedded Software Development) Software Development Design - Object Oriented Programming, Design Patterns etc. Software Development Life Cycle Sometimes I will also present some articles related to other programming languages like: Python, Java, JavaScript, HTML, CSS etc.  As you noticed range of topics in this blog has been significantly extended. It should this blog more interesting.

C++11 - User-defined literals

C++11 standard provides new way of customized literal constants called user defined literals . Thanks to that user can define own suffixes for standard literals (ex. digits) which defines it better and makes it better readable. Example of such suffixes could be: _meters, _kilograms, _squareMeters etc. For better understanding user-defined literals concept, let's take a look on the below example code: Output of that code is: In point one we can see concept of operator overloading function which should be used in order to define user-defined literals. In our example we are defining suffixex _meters and _squareMeters which will be used for imporve readability of code of calculations based on that units. Point II shows how we can use user-defined literals to assign values to variables. Such usage definitely makes code better readable and easier to understand. Point III, shows how we can use other suffix (_squareMeters) in comparison calculations. As we can see, our units a

C++11 - noexcept function specifier

Today I would like to present another new feature of C++11 standard - noexcept specifier. This specifier allows to specify function which does not throw any exception . It should be used be used wherever possible to notify user that function should be throw and it makes such function non-throwable self-documented (similar to usage const ). Its usage is similar to usage const function specifier. The difference is that while const function tries to modify data it we have compilation error. In case of noexcept function throws any exception, code compiles, however it std::terminate function is invoked when we achieve throwing exception in such function. To understand it better, take a look on below example: Output of this example is: In point I, we are defining function as non-throwable using noexcept specifier. Such function should not throw, however in our example we are throwing exception in order to present what happens when noexcept function tries to throw except

Advanced C++ - Stack unwinding

Stack unwinding is normally a concept of removing function entries from call stack (also known as Execution stack, Control stack, Function stack or Run-time stack). Call Stack is a stack data structure that stores active functions' addresses and helps in supporting function call/return mechanism. Every time when a function is called, an entry is made into Call stack which contains the return address of the calling function where the control needs to return after the execution of called function. This entry is called by various names like stack frame , activation frame or activation record. With respect to exception handling , stack Unwinding is a process of linearly searching function call stack to reach exception handler. When an exception occurs, if it is not handled in current function where it is thrown, the function Call Stack is unwound until the control reaches try block and then passes to catch block at the end of try block to handle exception. Also, in this proc

Advanced C++ - Exceptions

Exception handling is programming feature helpful for programmers to handle run time error conditions. Run time error conditions can be like division by zero, unable to allocate memory due to scarcity, trying to open a file which doesn’t exist etc. There are two types of exceptions. One is Standard exceptions provided by C++ Standard library and other type is User defined exceptions. Now, to handle any exception, we should understand 3 main block of exception statement which are as follows: throw block - in this block, we throw an exception. The operand of the throw statement decides the type of exception occurred. Once this line is executed, program jumps to catch block to handle the exception occurred. catch block - is the block where we handle exception. This is also called as exception handler and this block is written immediately after the try block as shown in example. This can be written similar to a normal function with at leas

ACCU 2014 Conference - Bristol, UK

Last week I participated in ACCU 2014 Conference in Bristol (UK). That was great and fruitful time for me where I find out lot of information about C++ and Software development in general, as well as I met few interesting people. I would like to share few information from that conference with you. During the conference we listen interesting speeches of many C/C++ experts such as: Howard Hinnant - he presened interesting speech about C++11 Move Semantics , presenting interesting short explanation how std::move actually work as well as detail presentation of generation of Special Member Function (class members which are automatically generated by compiler) for different cases with and without move constructor and move assignment operator. At the end he presented detail implementation of basic move constructor and move assignment operator . Detail explanation of move semantics will be presented on this blog soon. Anthony Williams - author of book C++ Concurrency in Action and d

C++ Multithreading - Race conditions

In the previous C++ Multithreading article I presented you how to pass parameters between threads. Take a detail look on the output of that example once again: In the first line of that output you can notice that output text from two threads is mixed. You are probably wondering why it happens? It is because we are not protecting resources which are shared by two threads (in this example cout stream is shared in both threads) which causes multithreading's phenomenon called race condition . Because threads switching and accessing to shared resources are managed by operating system we do not know when std::cout stream will be accessed by main thread and when it will be accessed by second thread. Therefore in the previous article I mentioned that output of the example can be little different on your computer than my output example. What's more it is possible that this output will be different for few consecutive invoking of the example on the same machine. It is beca

C++ Multithreading - Passing parameters to thread function and sleep thread

Today I am going to present you second article about new C++11 Multithreading feature. I am going to present you how we can pass parameters to thread function (by value and by reference) as well as show you how to delay thread processing for selected amount of time . In previous C++ Multithreading article we presented how to create thread function and run second thread in parallel to main thread. Now we are extending that example with usage of another multithreding features. Let take a look on the new example: Output of that example could be (it can be little other output on your machine because we are working with Multithreading which is not protected by phenomenon called race condition which I will explain in one of another articles): In point I and II we are creating new thread which will invoke function print() from point Ia. Difference to the example from previus article is that we are passing two parameters to our function. First parameter firstParam is passed b

Advanced C++ - virtual functions

Today I am going to explain some basic but very important C++ feature - polymorphism and inheritance which is based on virtual functions in C++. Virtual functions can be declared by preceding the function with virtual keyword. The importance of Virtual functions can be understood, when we design classes using inheritance. Virtual functions are special functions which are called using late binding concept. Late binding or dynamic binding means that the binding happens during run time using vtable (virtual table) to select the correct virtual method to be called as the code runs. Let us look at an example for getting more clarity on how Virtual functions work in reality: Output of that example is: Point 1 shows how virtual function is declared and defined. Point 2 shows the inheritance concept i.e class Derived is inherited from class Base using public access specifier. In point 3, virtual function of class Base is redefined in class Derived which leads to f

Advanced C++ - Mutable Class Field

Today I would like to present C++ class' feature called mutable class field . Mutable class field is class' field modifier which allows to change its value even if object of the class is declared as const . Take a look at the example: Output of this example is: In point I of that example we are defining object of TestClass . Note that this object is const . As you can see in point Ia this class has three different member fields ( constInt, mutableConstInt, nonConstInt ). Those variables are public for this example, but do not worry about encapsulation here. It is just omitted for simplify this example. As you can see one of this member fields is marked as mutable class file using mutable keyword ( mutableConstInt ). Such variable can be modified even if object of class TestClass is const . It will be explained in next points of this example. In point II we are printing default values of testObject object initialized in initialization list of TestClass' default c

QT 5.2 - Changes and future prediction - interesting article

I would like to share with you interesting article about QT 5.2 changes, integration with mobile platform and preditcions of future of QT framework. You can find this article here: http://meetingcpp.com/index.php/br/items/a-look-at-qt52.html Enjoy!

C++11 - Uniform initialization

C++11 standard provides new form of objects initialization which makes such initialization uniform for different structs and classes and simplify objects construction and initialization process. Thanks to the new process we can initialize many different classes using the same syntax. Please take a look on the example below: Output of this example is: In point I we are initializing class of two different objects using C++11 uniform initialization mechanism. On object is created based on struct type (pure struct type without any method - look point II). The second one is created based on MyClass class constructor. As you can see, despite that we are creating objects of two totally different types, those objects can be created the same way (uniform way). This is just uniform unitialization featuere provided in C++11 standard. Below we are initializating MyStruct object using external function getMyStruct() . You can take a look to the body of that function (point IV).

C++ in 2014 - Predictions

Today I would like to share with you interesting article about prediction of development C++ programming languages (and its well-known frameworks and libraries) in 2014. It is written by Jens Weller and I think it is very interesting for every C++ programmer and user. You can open this article by clicking on the image below: Enjoy!

Advanced C++ - Effective C++ - cheatsheet

I would like to share with you my summary of the "Effective C++" book by Scott Meyers . It is very interesting book for everyone who would like to improve C++ developments skills to be every effective. I highly recommend it. Quick summary of that book, which I prepared you can download after by clicking on below image. Summary of Effective C++ book by Damian Ziobro

Blog's new layout

As you noticed this blog has new layout from today. I hope you like it. I think new layout looks better and more modern than previous one. Please, write you opinion about new layout in comments. If you have some ideas how to make this blog better, all ideas are welcomed. Enjoy new layout and blog articles.

QT - foreach algoriithm with const references performance improvement

Today I would like to show you optimal way of using foreach QT algorithm . I will show you why we should pass elements of foreach algorithm by const reference instead of passing them by value. Let me explain it on the below example: Output of this example is: In point I we are creating 3 objects of MyClass class and push them to myClasses QList element. In point II we are using QT foreach algorithm to invoke getValue() method for each object from myClasses list. As you can see on output text for that part of code we are invoking copy constructor before and destructor after invoking getValue() function. It is because we are passing each myClasses list element to foreach algorithm by value. Therefore we are copying that element at the beginning of foreach loop step and removing them (destructing) at the end. This is inefficient solution, especially when class of object being copied is big. It decreases performance. of our application. Solution for that i

C++ Multithreading - Basic thread creation

C++11 standard introduces integrated sublibrary for multithreading . It has been moved from Boost library. However because multithreading is very important topic in software development I decided to make multithreading tutorial as separate section of this blog. I will not explain here theory about multithreading, but I will try to explain usage of multithreading based on thread library from C++11 standard. If theory explanation will be required for some aspects I will try to put links to separate articles explaining them. Introduction theory about multithreading you can find on Wikipedia article here This article will present basic threads creation and usage in C++. Code of example you can find here: Output of the example is (it can be little other - depends how thread will be invoked by OS): In point I we are creating new thread where print() function will be invoked. This thread will work in parallel to the main function (main application is second thread called main

Boost.DateTime - Selected date operations

Today I would like to present you operations related to date and time using Boost.DateTime library. This library contains set of classes, templates and algorithms prepared for date and time related tasks. This article focuses on date operations . Time operations will be presented in one of the future articles. Example of usage some basic Boost.DateTime date operations you can see here: Output of this example is (for day 2 January 2014): All below operations will be based on boost::gregorian namespace. We created alias dateTime for that namespace for more convenient use. In point I we are using universal_day() method for getting current day according to UTC time zone. Of course output of this and other date-related functions depends on date of invoking. Therefore you will get other output of your application every day you will invoke it. universal_day() function returns Boost   date class object which has lot of functions for date manipulation and displaying. In p