Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding qthreads - sharing data between threads

I'm starting to understanding the mechanism of using threads, but I guess I'm stuck,

If I understood, I have to create my own class, release the run() method and then create the thread.

the problem is that my thread has to read from the gui(main thread) some variables, and them using them, it will create some other variables, which the mainwindow will read and plot.

the thing is that I’m receiving a bluetooth connection, which has to be always active in a thread, but the gui has to be plotting values read from this thread.

this is the function which needs to be on a separated thread:

// Listen to the device for data
void gui::listen_device()
{
    unsigned char buf[10];
    unsigned char crcval;
    fd_set readmask;
    struct timeval tv;

    tv.tv_sec = 0;
    tv.tv_usec = 28000;

    memset (buf, 0, 10);

    int v = 0, v1 = 0, v2 = 0;

    while(1)
    {
        int i;
        FD_ZERO (&readmask);
        FD_SET (sock, &readmask);
        if (select (255, &readmask, NULL, NULL, &tv) > 0)
        {
            if (FD_ISSET (sock, &readmask))
            {
                int numb;
                numb  = 0;

                numb = recv (sock, buf, 10, MSG_WAITALL);

                crcval = BP_CRC8 (buf, 9);

                // 8 bits
                if (ui->comboBox->currentIndex() == 0)
                {
                    if (crcval == buf[9])
                    {
                        s++;
                        // Print of counter
                        printf ("%d ->", buf[0]);
                        fprintf (data, "%d,", buf[0]);

                        for (int i = 1; i < 9; i++)
                        {
                            v = buf[i];
                            printf ("%d,", v);
                            fprintf (data, "%d,", v);
                        }

                        printf ("\n");
                        fprintf (data, "\n");

                        //fprintf(data, "s: %d, f: %d\n", s,f);
                    }

                    else
                    {
                        f++;
                    }

                }

                // 12 bits
                else if (ui->comboBox->currentIndex() == 1)
                {
                    if (numb == 14)
                    {
                        // Print of counter
                        printf ("%d,", buf[0]);
                        fprintf (data, "%d,", buf[0]);
                        for (i = 1; i < numb - 1; i += 3)
                        {
                            v1 = buf[i] | ((buf[i + 1] & 0x0F) << 8);
                            v2 = buf[i + 2];
                            v2 = (v2 << 4) | ((buf[i + 1] & 0xf0) >> 4);
                            printf ("%d,%d,", v1, v2);
                            fprintf (data, "%d,%d,", v1, v2);
                        }

                        printf ("\n");
                        fprintf (data, "\n");
                    }
                }
            }
        }
    }
}

and some of this variables read here are global, as sock, data, and the ui->combobox.

I want the bufto be shared with the mainwindow.

Any advice?

UPDATE:

why is this wrong?

void QMyThread::run()
{
    listen_device();
}
like image 952
SamuelNLP Avatar asked Jul 24 '26 17:07

SamuelNLP


1 Answers

Here's a nice article about using threads in Qt
http://qt-project.org/doc/qt-5.0/qtcore/thread-basics.html

like image 66
spiritwolfform Avatar answered Jul 27 '26 06:07

spiritwolfform



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!