Skip to main content

Posts

Showing posts with the label multithreading

Bash - parallel operations

Today I would like to present useful tool for every Linux developer - bash parallel mode. Bash parallel mode allows to invoke multiple bash tasks (ex. functions, linux applications etc.) in parallel, which could save time of processing them. What's is more, usage of bash parallel mode is very simple and does not require lot of work. Let's explain it based on the examples. We are starting with bash script working in seqence mode (one function invoked after previous one). We defind function task() which is later invoked in for loop 10 times. Invoking of each instance of function takes SLEEP_TIME=2 seconds. So script invokation takes 10x2s == 20 seconds. Now, lets change above script in order to work in parallel mode: Please note few small, but important changes between above scripts: < invokation of function task has '&' character at the end. It means that function will be invoked in background and will not block main thread of script. functio...

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...

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...

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...