PDA

View Full Version : Question about threading basics



donglebob
21st August 2008, 13:50
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

jacek
21st August 2008, 17:22
What is the difference between both when we look at memory usage?
None.


Would the threads of number (2) disturb each other, while accessing variables?
If these are the same variables, then yes.


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.


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.

donglebob
21st August 2008, 23:37
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?

jacek
22nd August 2008, 02:33
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().

wysota
22nd August 2008, 11:16
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.

donglebob
28th August 2008, 13:08
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

wysota
28th August 2008, 14:58
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.

donglebob
29th August 2008, 10:36
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?

danadam
29th August 2008, 19:40
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++.



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.