Results 1 to 9 of 9

Thread: Question about threading basics

  1. #1
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Question about threading basics

    Hi,

    Can someone telle me what the difference is between following (from the view of a programmer and from the view of the operating system):

    1) creating other Threads in an existing Thread-Instance of a Thread-Class

    2) creating two instances of a Thread-Class

    ThreadClass a[0] = new ThreadClass()
    ThreadClass a[1] = new ThreadClass()
    What is the difference between both when we look at memory usage?

    Would the threads of number (2) disturb each other, while accessing variables?
    The second one sounds like two seperate "processes", which have seperate memory.

    I have no example for number (1)
    Can someone show me an example how I can create a second Thread in an existing Thread instance like:

    ThreadClass b = new ThreadClass();



    (Thread-Class = derived from QThread)


    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about threading basics

    Quote Originally Posted by donglebob View Post
    What is the difference between both when we look at memory usage?
    None.

    Quote Originally Posted by donglebob View Post
    Would the threads of number (2) disturb each other, while accessing variables?
    If these are the same variables, then yes.

    Quote Originally Posted by donglebob View Post
    The second one sounds like two seperate "processes", which have seperate memory.
    Threads share the same address space, as they exist within a single process.

    Quote Originally Posted by donglebob View Post
    I have no example for number (1)
    It's the same as for situation (2). It doesn't matter where in code you create threads --- it always will be within some thread.

  3. #3
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about threading basics

    Thanks for your answers.

    I was a bit irritated when i started to read about threading in Qt.
    I thought always that there is another special way to create threads not just the instantiating way.

    Another question:

    I have a Thread-Class A which I wrote for serial communication, which has write and read operations etc..for overlapped IO.

    Now I wanted to create two threads with this Class A.
    The first one for Reading (listens whole time on the line and does sth.).
    The second one for Writing, when it gets work to do from somewhere.

    I have no idea, how i have to write my run() method for both functionalities (in one Thread Class which has both abilities)

    How can i do this?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about threading basics

    If your threads do two different things, then why don't you create two different thread classes? Anyway you can pass some parameter to the constructor that will tell the thread how it should behave and then check it in run().

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about threading basics

    Quote Originally Posted by donglebob View Post
    I have a Thread-Class A which I wrote for serial communication, which has write and read operations etc..for overlapped IO.

    Now I wanted to create two threads with this Class A.
    The first one for Reading (listens whole time on the line and does sth.).
    The second one for Writing, when it gets work to do from somewhere.

    I have no idea, how i have to write my run() method for both functionalities (in one Thread Class which has both abilities)
    Why not have reading and writing in the same thread (instance)? You won't be reading and writing all the time and in addition to that it is very doubtful your device can read and write at the same time (I'm not talking about full-duplex but about actually doing both operations at once) so probably doing it in two threads will only make you run into trouble.

  6. #6
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about threading basics

    Hi,

    i am testing right now the above problem.
    I created two instances of my thread class.

    ThreadClass a = new ThreadClass(this,true);
    ThreadClass b = new ThreadClass(this,false);
    a->start();
    b->start();
    The thread class has a simple private int variable i.

    I have done following:
    Both threads check what kind of boolean comes with the ctor.
    If true thread a does i++ in run.
    If false thread b does i-- in run ;

    void run(){

    while(!done){
    if(itsA){
    i++;
    qDebug("I am A with i %d",i);
    }

    if(itsB){
    i--;
    qDebug("I am B with i %d",i);
    }

    }

    }
    I output the value of i with qDebug.
    They don*t disturb each other. Why?

    I thought they are sharing this variable?

    Can somebody explain it to me?

    thanks

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about threading basics

    Quote Originally Posted by donglebob View Post
    They don*t disturb each other. Why?
    Pure luck. And you probably have a single processor in your machine which makes your luck have greater chance of manifesting itself.

    Edit: I think I may have written my reply without reading your post carefully. If the thread class has a private member variable "i", then each of the instances of the class has an own instance of this variable, thus they don't interfere each other.
    Last edited by wysota; 28th August 2008 at 14:12.

  8. #8
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about threading basics

    Hmm,

    Are instance variables always protected from other threads?

    What can I do when I want that two threads want to use the same variable(s)?

    Years ago I used pthreads. There I had to protect the variables.
    Why is it so different?

  9. #9
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question about threading basics

    Quote Originally Posted by donglebob View Post
    Are instance variables always protected from other threads?
    You created two objects. If 'i' is a normal class member, then each created object has its own instance of the variable. This has nothing to do with threads and protecting variables. This is just how classes and objects work in C++.

    Quote Originally Posted by donglebob View Post
    What can I do when I want that two threads want to use the same variable(s)?
    You can make the 'i' member a static member. Then there would be only one instance of 'i', no matter how many objects you create.
    The Wheel weaves as the Wheel wills.

Similar Threads

  1. Newbie threading question
    By deepayan in forum Qt Programming
    Replies: 17
    Last Post: 16th April 2007, 00:25

Tags for this Thread

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.