Ah right - thats what happens when I read posts between compiles...start will be called when the condition is false.
Well, not sure about "not efficient".1) create a thread with a timer that emits a signal every 'x' miliseconds (avoiding the use of sleep()) and connected to a slot that reads data from serial device and print data in console if a global boolean variable is 'true' (meaning Im on the "loop" command); not efficient - timer is always emitting the signal
Since you are not working with interrupts, you can't avoid polling.
Running a timer might even be more efficient then a thread - as timer is triggered by a HW interrupt, while a thread is running all the time...
If what ever milliseconds are good enough for you (and since you are reading from a serial port you probably wont need anything faster than 10ms per polling) this is the easier, and simpler solution, and probably more efficient too, which I would tend to do.
In your code I don't see where you defined and allocated your mutexes.2) two mutexes to control/block the loop as needed; it works but if I call QThread::sleep() it the thread blocks (why?)
Probably you have a thread affinity problem, so that you lock the same mutex twice before you unlock it.
Also, you are misusing mutexes to what they are not intended.
Which explains the code that makes little sense if you use mutexes as you should:
Mutex is for protecting mutual exclusive data, not for conditional running.Qt Code:
start_mutex.lock(); start_mutex.unlock();To copy to clipboard, switch view to plain text mode
There are other mechanisms for that, such as semaphores or wait conditions.
Just locking and immediately unlocking a mutex makes no sense if you are not protecting anything between the lock()/unlock(), and that code should be the same if you just delete both lines.
I know what you wanted to achieve but that is the wrong way to go about it.
As you are clearly not quite proficient with threading concepts, stick to the timer solution.
It is a good solution, and a simple one.






Reply With Quote

Bookmarks