我在C ++中遇到类多线程问题

时间:2019-03-02 16:14:59

标签: c++ multithreading

我在C ++中遇到类多线程的问题,我的代码是下一个,但是我有很多错误,我不知道为什么...我希望有人可以帮助我并解释我的错误。 Ty

  1. 任务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_ */
    

文件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_ */

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_ */

编译:

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

2 个答案:

答案 0 :(得分:0)

您的标头中存在循环依赖项。

Task.hpp
   #include "thread.hpp"
      Thread.hpp                <——————————-
         #include "pool.hpp"               ^
             Pool.hpp                      |    Loop
                include "thread.hpp" ——-——->

答案 1 :(得分:0)

我认为您应该检查依赖性

您的依赖项是:

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

您正在填充池。池需要线程,而线程需要池。为了获得周围的循环依赖关系,您需要使用前向声明。参见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_ */