Hi,
The following situation is given:
There is an Object Instance of a Serial Class , which has the abilitiy to read() and write() on com-port. The Serial Class itself has in its privates an instance of a communication library I use.
The read() write() methods above use methods of this com. library.
class Serial:
Serial()
{
...
reader = new threads(this,true);
writer = new threads(this,false);
reader->start();
writer->start();
...
}
read()
{
//use here the com library function and some winapi functions to read from the com port
}
write()
{
// same as above but or writing
}
private:
Seriallibrary Slib; // instance of foreign serial library
Thread-Class *reader,*writer;
class Serial:
Serial()
{
...
reader = new threads(this,true);
writer = new threads(this,false);
reader->start();
writer->start();
...
}
read()
{
//use here the com library function and some winapi functions to read from the com port
}
write()
{
// same as above but or writing
}
private:
Seriallibrary Slib; // instance of foreign serial library
Thread-Class *reader,*writer;
To copy to clipboard, switch view to plain text mode
What I wanted to do is following:
Create two threads, one for reading operations and one for writing operations. (asynchron communication)
They had to use the methods I provide with my Serial Class.
What I have done is:
I created a "Thread-Class" derived from QThread.
I put two pointers of Type "Thread-Class" to my Serial Class (private member).
The Ctor of the "Thread-Class" has a boolean, to decide if it is a reader or a writer thread.
In dependency of that boolean one of two possible loops will be started.
These two loops use the Serial Class functions provided for writing or reading (with pointer serialPTR).
I think the whole stuff I designed is wrong. Is every function call I make from the threads running in the context of my Serial Class-Instance ????
Threads:
Threads(parent *p,bool typeofThread)
{
//take the parent pointer (serialPTR), because I need it to call the provided functions for reading and writing
//set a local flag if I am a Reader Thread or a Writer Thread.
}
run()
{
if(readerFlagIsSet)
while(!threadDone)
{
serialPTR->read();
}
if(writerFlagIsSet)
while(!threadDone)
{
serialPTR->write();
}
}
// I mean the two function calls above from the thread to the serial-Class
Threads:
Threads(parent *p,bool typeofThread)
{
//take the parent pointer (serialPTR), because I need it to call the provided functions for reading and writing
//set a local flag if I am a Reader Thread or a Writer Thread.
}
run()
{
if(readerFlagIsSet)
while(!threadDone)
{
serialPTR->read();
}
if(writerFlagIsSet)
while(!threadDone)
{
serialPTR->write();
}
}
// I mean the two function calls above from the thread to the serial-Class
To copy to clipboard, switch view to plain text mode
Can you give me a hint to make that better?
Or is this way ok?
Bookmarks