How to use a Shared-Variable

62 Views Asked by At

I want to know your opinion on how I coded my QThread.

this part of my code is for handle a PWM for LED display.

The purpose of my QThread is to work as smoothly as possible while changing a value in the QThread else i can watch on LED display a very fast break during the value change, example when i use QMutex.

this is the reason why I chose to use the SIGNAL / SLOT way When I need to change the brightness that depends on the time or value of a resistance, I use the emit SIGNAL_FUNCTION (qint32 new_value)

Is there a risk of crashing my application using my method? is there another way more correct to handle this kind of procedure?

I enclose my code below.

#include "luminosite.h"

LUMINOSITE::LUMINOSITE(QObject *parent, qint32 P_PIN_LUMINOSITE) :
    QThread(parent)
{
    PIN_LUMINOSITE = P_PIN_LUMINOSITE;
    VALEUR = 1;
}

void LUMINOSITE::run()
{
    qint32 TICK = 0;

    while(true)
    {
        if (TICK == 0)
        {
            digitalWrite(PIN_LUMINOSITE, LOW);
        }
        else if (TICK == VALEUR)
        {
            digitalWrite(PIN_LUMINOSITE, HIGH);
        }
        TICK++;
        if (TICK == 33)
        {
            TICK = 0;
        }
        delayMicroseconds(1);
    }
}

void LUMINOSITE::CHANGEMENT_VALEUR(qint32 P_VALEUR) // SLOT
{
    VALEUR = P_VALEUR;
}
1

There are 1 best solutions below

0
On

If your question is only "SIGNAL/SLOT are they thread safe ?", read that : http://doc.qt.io/qt-5/threads-qobject.html#signals-and-slots-across-threads

It only depend on the connection type you choose.

You need to choose between Qt::QueuedConnection and Qt::BlockingQueuedConnection