Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Problem with Sharing resources among threads

    General and Desktop
    2
    2
    672
    Loading More Posts
    • 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
      NarimanN2 last edited by

      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 Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        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 Reply Quote 0
        • First post
          Last post