Monthly Archives: March 2014

Simple Example Using Shared Variable in QT Multi-thread with QMutex

Simple Example Using shared variable in QT multi-thread with QMutex

Today, in this post, I want to share my simple example code to explain how to use shared variables in QT multi-thread environments with QMutex.

In the example, first I will generate an Int variable to be shared.

Then, I will make two threads. Each thread has their own ID. and they will try to change the shared number with their ID number.

After changing the shared number in their threads to be same with its ID, the thread will check if the shared number is really same with the ID. If the other thread changes the shared variable just before the check, it will print out an error message.

Without Mutex, it will give us many error message, and with Mutex, we can remove this collision error.

You can download full package of source code from here  <Download>.

Also, you can just see and understand by watching the below codes and results with or without Mutex.

main.cpp

#include <QtCore/QCoreApplication>
#include "mythread.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QMutex mMutex;

    int sharedVar=10;
    qDebug() << "sharedVar :"<< sharedVar;

    MyThread mThread1(&mMutex,1,&sharedVar);
    MyThread mThread2(&mMutex,2,&sharedVar);

    mThread1.start();
    mThread2.start();

    return a.exec();
}

mythread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QtCore>
#include <QMutex>

class MyThread : public QThread
{
private:
    QMutex* mutex;
public:
    MyThread(QMutex* mu, int myNum, int* sharedNum);
    int myNumber;
    int* num;


protected:
    void run();

};

#endif // MYTHREAD_H

mythread.cpp

#include "mythread.h"
#include <QtCore>
#include <QDebug>
#include <QMutexLocker>

MyThread::MyThread(QMutex* mu, int myNum, int* sharedNum)
{
    mutex= mu;
    myNumber = myNum;
    num = sharedNum;

}

void MyThread::run(){

    qDebug() << "Thread " << myNumber << "num: " << num << "*num: " << *num;
    for (int i=0;i<5;i++){

        mutex->lock(); // To avoid collision

        qDebug() << i << "Before Change, My Number : "<< myNumber << "*num" << *num  ;
        (*num) = myNumber;
        usleep(10);
        if ( *num != myNumber ){
            qDebug() << "! collision ! at Thread" <<  myNumber ;
        }
        qDebug() << i << "After  Change, My Number : "<< myNumber << "*num" << *num  ;

        mutex->unlock(); // Let's release the lock
        usleep(1);

    }
}


QMutexTest.pro

QT       += core
QT       -= gui

CONFIG   += console
SOURCES += main.cpp \
    mythread.cpp
HEADERS += \
    mythread.h

 

Result ( with Mutex)

QMutex

 

Result ( If I remove mutex part)QMutex2

 

I wish everybody understands this one. If you want to study more deeply, I recommend to read this post. http://stackoverflow.com/questions/8971168/how-to-use-qmutex

If you cannot understand, please leave me a reply.

 

—————————————————————————————————————————–

I am Youngmok Yun, and writing about robotics theories and my research.

My main site is http://youngmok.com, and Korean ver. is  http://yunyoungmok.tistory.com.

—————————————————————————————————————————–

 

 

Terminator installation and setting as a default terminal program

I strongly recommend to use Terminator as a default terminal program in Linux.

It has many great functions such as horizontal/vertical tab split.

First. Installation.

$ sudo apt-get install terminator

Second, Setting as a default terminal program

gconftool --type string --set /desktop/gnome/applications/terminal/exec terminator

It is done!!!

Good luck

Monte Carlo Integration with a simple example

In this article, I will explain Monte Carlo Integration.

How can we do the “Integration”?

In many cases, the integration is not easy in an analytical method.

The Monte Carlo Integration method is a numerical integration method.

Let’s think about the below example. MCI1

 

The goal of this integration is to find the area of pink region.

The key idea of the Monte Carlo integration is to find \hat{f}} to represent f. See below.

MCI2Then, How can we find the \hat{f}} ? the Monte Carlo Integration method uses “Expectation method” (Average) See below

MCI3

 

With the random sampling method, we can get the \hat{f} by calculating the mean value.

This is a very useful way especially for the calculation of Bayesian posterior.

 

The below is an example of Monte Carlo Integration.

I will solve this problem  \int^2_{-1}x dx

>> N=10000;
>> 3*sum(rand(N,1)*3-1)/N

ans =

1.5202

Here \hat{f} is < sum(rand(N,1)*3-1)/N > and the range of the integration is 3.

Then, good luck

For more detail, I recommend to read the below article.

http://web.mit.edu/~wingated/www/introductions/mcmc-gibbs-intro.pdf

 

—————————————————————————————————————————–

I am Youngmok Yun, and writing about robotics theories and my research.

My main site is http://youngmok.com, and Korean ver. is  http://yunyoungmok.tistory.com.

—————————————————————————————————————————–