Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with Sharing resources among threads
QtWS25 Last Chance

Problem with Sharing resources among threads

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 743 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NarimanN2
    wrote on last edited by
    #1

    I wrote two classes called TextProducer and TextConsumer and they inherited from QThread. I want to Share some resources like Qchar between this two Threads, so I Created a header file called globs.h and put my resources in it.but I get this errors :

    textproducer.obj:-1: error: LNK2005: "bool atEnd" (?atEnd@@3_NA) already defined in main.obj
    textproducer.obj:-1: error: LNK2005: "class QChar * buffer" (?buffer@@3PAVQChar@@A) already defined in main.obj
    textproducer.obj:-1: error: LNK2005: "class QSemaphore availableData" (?availableData@@3VQSemaphore@@A) already defined in main.obj
    textproducer.obj:-1: error: LNK2005: "class QSemaphore freeSpace" (?freeSpace@@3VQSemaphore@@A) already defined in main.obj
    textconsumer.obj:-1: error: LNK2005: "bool atEnd" (?atEnd@@3_NA) already defined in main.obj
    textconsumer.obj:-1: error: LNK2005: "class QChar * buffer" (?buffer@@3PAVQChar@@A) already defined in main.obj
    textconsumer.obj:-1: error: LNK2005: "class QSemaphore availableData" (?availableData@@3VQSemaphore@@A) already defined in main.obj

    @#ifndef GLOBS_H
    #define GLOBS_H
    #include <QChar>
    #include <QSemaphore>

    const int bufferSize = 20;
    QChar buffer[bufferSize];
    QSemaphore freeSpace(bufferSize);
    QSemaphore availableData(0);
    bool atEnd = false;

    #endif // GLOBS_H
    @

    @#ifndef TEXTCONSUMER_H
    #define TEXTCONSUMER_H

    #include <QThread>
    #include <QtCore>

    class TextConsumer : public QThread
    {
    public:
    TextConsumer();
    void run();
    };

    #endif // TEXTCONSUMER_H
    @

    @#ifndef TEXTPRODUCER_H
    #define TEXTPRODUCER_H

    #include <QThread>

    class TextProducer : public QThread
    {
    public:
    TextProducer(const QString &text);
    void run();

    private:
    QString m_text;
    };

    #endif // TEXTPRODUCER_H
    @

    @#include "textconsumer.h"

    TextConsumer::TextConsumer() : QThread()
    {
    }

    void TextConsumer::run()
    {
    int i = 0;

    while (!atEnd || availableData.available())
    {
        availableData.acquire();
        qDebug() << buffer[i];
        i = (i + 1) % bufferSize;
        freeSpace.release();
    }
    

    }
    @

    @#include "textproducer.h"

    TextProducer::TextProducer(const QString &text) : QThread()
    {
    m_text = text;
    }

    void TextProducer::run()
    {
    for (int i = 0; i < m_text.length(); ++i)
    {
    freeSpace.acquire();
    buffer[i % bufferSize] = m_text[i];

        if (i == m_text.length() - 1)
        {
            atEnd = true;
        }
    
        availableData.release();
    }
    

    }
    @

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      The problem has nothing to do with threads.
      If you declare a variable in a header and then include that header in two different places then of course you've got a duplicated definition and it won't link because linker doesn't know which to choose.

      It's a bad idea to put data definitions in a header anyway. Either use extern or a singleton or a proper OOP data scheme where you put data in a class which you can share with eg. QSharedPointer.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved