PDA

View Full Version : Thread Synchronization Problem



sijithjames
13th November 2009, 12:29
Here i want to execute a thread through two different function.
Here i am getting synchronization problem. I want both task to execute concurrently.

functionA calling thread1 (Incrementing X value)
functionB calling thread1 (Decrementing X value)


How can i do it.. Plz give some suggestions and help.

high_flyer
13th November 2009, 12:50
What do you mean by "functionA calling thread1"
Do you mean you want to start thead1 in the main thread when functionA is called?

What exactly is the problem you are having?
Can you show relevant code?

Don't expect to be taught thread programming on a forum, but if you have a specific question, then please provide all information needed to help you.

sijithjames
13th November 2009, 13:02
Here i created a thread and created two instance of that thread. i want both object to call the run function cuncurrently

threadNew = new ServerThread(this);
threadNew1 = new ServerThread(this);

class ServerThread : public QThread
{
Q_OBJECT

public:
ServerThread(QObject *parent);
~ServerThread();

protected:
void run();

};

Here threadNew and threadNew1 are calling the run function but not in synchronized manner

How can i do this

lonepsycho
13th November 2009, 13:29
you dont need to call run concurenlty. just call it. and two threads starts to do their work.

sijithjames
13th November 2009, 13:34
I have two object of same thread

1. First threadNew have to call the run function
2. Then threadNew1 have to call the run function

Both have to call the run function alternatively, is it possible

Now what happening is both object is calling hte run function but calling order we cant predict.. Each time its calling in different order

i want treadNew-- treadNew1-- treadNew-- treadNew1 in this order

high_flyer
13th November 2009, 13:46
its hard to understand from your post what it is you want.
Please show the code that depicts your problem.

The code you posted shows no problem.
By the way,run() should be protected, you call start() to start the thread.

lonepsycho
13th November 2009, 13:55
since threading is being managed by OS, I doubt that you can predict which starts first. unless you make your main thread pause between calls, or ensure that the first thread is started before starting next thread.

sijithjames
13th November 2009, 13:58
secondCheck.cpp

#include "secondcheck.h"
#include <qobject.h>
#include <QMutex>
extern QMutex mutex;
secondCheck::secondCheck(QObject *parent)
: QThread(parent)
{
threadNew1 = new ServerThread(this);
exeThread();
}

void secondCheck:: exeThread()
{
mutex.lock();
printf(" secondCheck Started++++++++++++++++++++++\n");
threadNew1->start();
printf(" Second Task Ended++++++++++++++++++++++\n");
mutex.unlock();
}

secondCheck::~secondCheck()
{

}
secondcheck.h
#ifndef SECONDCHECK_H
#define SECONDCHECK_H

#include <QObject>
#include "serverthread.h"
#include <QtGui/QMainWindow>

#include <QThread>
#include <QMutex>

class secondCheck : public QThread
{
Q_OBJECT

public:
ServerThread *threadNew1;
secondCheck(QObject *parent);
~secondCheck();
void exeThread();
QMutex mutex;

private:

};

#endif // SECONDCHECK_H
serverthread.cpp

#include "serverthread.h"

QMutex mutex;

ServerThread::ServerThread(QObject *parent)
: QThread(parent)
{

}

void ServerThread::run()
{
printf("Inside Thread @@@@@@@@@@@@@@@@@@@\n");
}

ServerThread::~ServerThread()
{

}
serverthread.h
#ifndef SERVERTHREAD_H
#define SERVERTHREAD_H

#include <QThread>
#include <QMutex>

class ServerThread : public QThread
{
Q_OBJECT

public:
ServerThread(QObject *parent);
~ServerThread();

protected:
void run();

};
mutex_sample.cpp

#include "mutex_sample.h"
#include <QMutexLocker>
#include <QThread>
#include <QMutex>

extern QMutex mutex;


#define ITER 10
int global = 0;

mutex_sample::mutex_sample(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
threadNew = new ServerThread(this);
taskCheck= new secondCheck(this);
taskCheck->exeThread();
taskOne();
}


void mutex_sample:: mainTask(void)
{

threadNew->start();

}

void mutex_sample::taskOne(void)
{
mutex.lock();
printf("First Task Started......................\n");
mainTask();
printf("First Task Ended......................\n");
mutex.unlock();
}


mutex_sample::~mutex_sample()
{

}
mutex_sample.h
#ifndef MUTEX_SAMPLE_H
#define MUTEX_SAMPLE_H

#include <QtGui/QMainWindow>
#include "ui_mutex_sample.h"
#include <QMutexLocker>
#include <QThread>
#include "serverthread.h"
#include "secondcheck.h"


class mutex_sample : public QMainWindow
{
Q_OBJECT

public:

mutex_sample(QWidget *parent = 0, Qt::WFlags flags = 0);
~mutex_sample();
ServerThread *threadNew;
secondCheck *taskCheck;
//threadn thdn;
void mainTask(void);
void taskOne(void);
void taskTwo(void);

private:
Ui::mutex_sampleClass ui;
mutable QMutex mutex;
};


#endif // MUTEX_SAMPLE_H

Output
Here i am getting the printf inside run in irragular form

Expected ans
secondCheck Started++++++++++++++++++++++
Inside Thread @@@@@@@@@@@@@@@@@@@
Second Task Ended++++++++++++++++++++++
First Task Started......................
Inside Thread @@@@@@@@@@@@@@@@@@@
First Task Ended......................
secondCheck Started++++++++++++++++++++++
Inside Thread @@@@@@@@@@@@@@@@@@@
Second Task Ended++++++++++++++++++++++
First Task Started......................
Inside Thread @@@@@@@@@@@@@@@@@@@
First Task Ended......................
secondCheck Started++++++++++++++++++++++
Inside Thread @@@@@@@@@@@@@@@@@@@
Second Task Ended++++++++++++++++++++++
First Task Started......................
Inside Thread @@@@@@@@@@@@@@@@@@@
First Task Ended......................

please help

lonepsycho
13th November 2009, 14:04
since you expecting sequential execution of your "threads", not side by side, maybe it is more resonable just call two functions one after another?

sijithjames
13th November 2009, 14:06
will Mutex or semaphore can do this job

lonepsycho
13th November 2009, 14:08
mutex or semaphore can lock data being modified by thread, so the answer is - it depends on what you want to do.

sijithjames
13th November 2009, 14:12
i tried with mutex but not working...
Unfortunately not getting any feasible solution...

Thanks 4 helping me

lonepsycho
13th November 2009, 14:17
to find a solution you need to know a problem, yet in this thread there is only you who knows what the problem is :) if you would be so kind to describe it maybe I or anybody else on this forum could provide a solution to your problem. Mutex can only lock your data for reading, writing or reading writing, yet it does not affect any other thread execution in any way if it does not require to read, write or read-write the same data. so you can not be sure that your threads are executed in sync (that is main idea of threads - be apart), so you can not see where you can put use for mutex in your task. from what you have written it seems that you don't need to use threads at all, or simply check thread status wheather is it running or not, or maybe you should check QFuture out?

high_flyer
13th November 2009, 14:53
@sijithjames
Several things:

- Your code makes very little sense.
At least I, still can't make out what it is you want to achieve.
Maybe if you state the objective of your code, or your task,we might help you.
I have the feeling it doesn't even have to be threaded.

- Your code shows that you do not understand how threads work in general, and what a mutex is used for, and I think you have even less idea how QThread should be used (there are some points you have to pay attention to when you using QThread, specially thread affinity)

- On one hand, you are using threads, on the other you state that the problem is that:

Now what happening is both object is calling hte run function but calling order we cant predict.. Each time its calling in different order
So it seems you want to have sequential execution order, not parallel execution.
For sequential excution you don't need threads!


Also, the fact you see the printf() output not in a sequential way, doesn't mean that they are not called sequentially.
It could be very well, that for many reasons, the order of printf() calling, and the output you see is not same.

I suggest you start with the basics, and read about threaded programming, and QThread.
Probably most of your problems will be solved that way.


And please use the code tags for posting code, its very hard to read code like that.