Results 1 to 14 of 14

Thread: Thread Synchronization Problem

  1. #1
    Join Date
    Nov 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Thread Synchronization Problem

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Thread Synchronization Problem

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread Synchronization Problem

    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

  4. #4
    Join Date
    Nov 2009
    Location
    Siauliai, LT
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread Synchronization Problem

    you dont need to call run concurenlty. just call it. and two threads starts to do their work.
    I came. I saw. I clicked.

  5. #5
    Join Date
    Nov 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread Synchronization Problem

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Thread Synchronization Problem

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Nov 2009
    Location
    Siauliai, LT
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread Synchronization Problem

    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.
    I came. I saw. I clicked.

  8. #8
    Join Date
    Nov 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread Synchronization Problem

    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

  9. #9
    Join Date
    Nov 2009
    Location
    Siauliai, LT
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread Synchronization Problem

    since you expecting sequential execution of your "threads", not side by side, maybe it is more resonable just call two functions one after another?
    I came. I saw. I clicked.

  10. #10
    Join Date
    Nov 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread Synchronization Problem

    will Mutex or semaphore can do this job

  11. #11
    Join Date
    Nov 2009
    Location
    Siauliai, LT
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread Synchronization Problem

    mutex or semaphore can lock data being modified by thread, so the answer is - it depends on what you want to do.
    I came. I saw. I clicked.

  12. #12
    Join Date
    Nov 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread Synchronization Problem

    i tried with mutex but not working...
    Unfortunately not getting any feasible solution...

    Thanks 4 helping me

  13. #13
    Join Date
    Nov 2009
    Location
    Siauliai, LT
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread Synchronization Problem

    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?
    Last edited by lonepsycho; 13th November 2009 at 15:27.
    I came. I saw. I clicked.

  14. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Thread Synchronization Problem

    @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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Problem with threads
    By kvnlinux in forum Newbie
    Replies: 7
    Last Post: 21st August 2009, 18:29
  2. postEvent thread problem
    By jhamers in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2008, 11:36
  3. Replies: 11
    Last Post: 7th July 2006, 14:09
  4. Thread Problem
    By qball2k5 in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2006, 18:31
  5. Problem building Qt4.1.0 with thread support on windows XP
    By pavithra in forum Installation and Deployment
    Replies: 1
    Last Post: 1st April 2006, 12:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.