-1

I have a problem with class multithreading in c++ my code is the next but i have many error and i dont know why ... i hope that one person can help me and explain my error. Ty

  1. Le fichier Task.hpp :

    #ifndef TASK_HPP_
    #define TASK_HPP_
    
    
    #include "thread.hpp"
    
    
    class Task
    {
    public:
        Task(int id);
        virtual ~Task();
        void setThread(Thread *_thread);
    
        void virtual work();
    
    protected:
        int taskID;
        Thread *thread;
    };
    
    #endif /* !TASK_HPP_ */
    

Le fichier Thread.hpp :

#ifndef THREAD_HPP_
#define THREAD_HPP_

#include "pool.hpp"

class Thread
{
    public:
        Thread();
        void setPool(Pool *_pool);

        void setId(int _id);
        int getId();
        void start();
        void terminate();
        static void* work(void *param);

    private:
        pthread_t pthread;
        Pool *pool;
        int id;
        bool isWorking = false;
};

#endif /* !THREAD_HPP_ */

Le fichier Pool.hpp :

#ifndef POOL_HPP_
#define POOL_HPP_

#include "thread.hpp"
#include "task.hpp"

class Pool
{
    public:
        static pthread_mutex_t taskQueue_lock;
        static pthread_cond_t taskQueue_cond;
        Pool();
        Pool(int num);
        virtual ~Pool();
        void initThreads();
        void assignJob(Task *_job_);
        bool loadJob(Task *&_job_);
        bool end();

    private:
        std::queue<Task*> taskQueue;
        int numOfThreads;
        std::vector<Thread*> threads;
        bool isTerminated = false;
};

#endif /* !POOL_HPP_ */

Compilation :

c++  -g -I ./includes/  -c -o sources/pool.o sources/pool.cpp
In file included from sources/pool.cpp:1:
In file included from ./includes/thread.hpp:13:
In file included from ./includes/pool.hpp:14:
./includes/task.hpp:20:24: error: unknown type name 'Thread'; did you mean 'std::thread'?
        void setThread(Thread *_thread);
                       ^~~~~~
                       std::thread
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:285:24: note: 'std::thread' declared here
class _LIBCPP_TYPE_VIS thread
                       ^
In file included from sources/pool.cpp:1:
In file included from ./includes/thread.hpp:13:
In file included from ./includes/pool.hpp:14:
./includes/task.hpp:30:9: error: unknown type name 'Thread'; did you mean 'std::thread'?
        Thread *thread;
        ^~~~~~
        std::thread
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:285:24: note: 'std::thread' declared here
class _LIBCPP_TYPE_VIS thread
                       ^
In file included from sources/pool.cpp:1:
In file included from ./includes/thread.hpp:13:
./includes/pool.hpp:32:28: error: expected expression
        std::vector<Thread*> threads;
                           ^
./includes/pool.hpp:32:21: error: use of undeclared identifier 'Thread'
        std::vector<Thread*> threads;
                    ^
./includes/pool.hpp:33:27: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
        bool isTerminated = false;
                          ^
In file included from sources/pool.cpp:1:
./includes/thread.hpp:34:24: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
        bool isWorking = false;
                       ^
2 warnings and 4 errors generated.
make: *** [sources/pool.o] Error 1
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • Have you tried to interpret the error message? The error "unknown type name 'Thread'; did you mean 'std::thread'?" means that you attempt to use `Thread` class without specifying its namespace (`std`). – Tsyvarev Mar 02 '19 at 16:29
  • Doesn't look like C – qrdl Mar 02 '19 at 16:38
  • The question title is misleading as this has nothing to do with multithreading as you are in the compile phase. – cquezel Mar 02 '19 at 16:38

2 Answers2

0

You have a problem with circular dependencies in your header.

Task.hpp
   #include "thread.hpp"
      Thread.hpp                <——————————-
         #include "pool.hpp"               ^
             Pool.hpp                      |    Loop
                include "thread.hpp" ——-——->
Net_x
  • 21
  • 4
0

I think you should check your dependencies

Your dependencies are:

task -> thread
thread -> pool
pool -> thread task

you're compilling pool. Pool requires thread and thread requires pool. To get arround circular dependencies, you need to use a forward declaration. See C circular dependency

#ifndef TASK_HPP_
#define TASK_HPP_


class Thread; // forward declaration


class Task
{
public:
    Task(int id);
    virtual ~Task();
    void setThread(Thread *_thread);

    void virtual work();

protected:
    int taskID;
    Thread *thread;
};

#endif /* !TASK_HPP_ */
cquezel
  • 3,859
  • 1
  • 30
  • 32