0

试图在 Linux 中使用我在 Windows 中创建的线程类。为其选择 Netbeans,并在我指定Linker > Additional Library Directories/usr/local/boost_1_53_0
Under的项目设置中Libraries,我指定boost_thread-mt
UnderC++ Compiler > Include Directories我指定/usr/local/boost_1_53_0

thread.h并且Thread.cpp是我的文件。Thread.hpp属于升压。

显示的错误在我包含的行中#include <boost/thread/thread.hpp>,错误是:

/usr/local/boost_1_53_0/boost/bind/bind.hpp: In member function ‘void  
boost::_bi::list1<A1>::operator()(boost::_bi::type<void>, F&, A&, int)
[with F = unsigned int (**)(void*), A = boost::_bi::list0, A1 =  
boost::_bi::value<void*>]’: In file included from  
/usr/local/boost_1_53_0/boost/bind.hpp:22:0,  
                 from /usr/local/boost_1_53_0/boost/thread/detail/thread.hpp:29,  
                 from /usr/local/boost_1_53_0/boost/thread/thread.hpp:22,  
                 from ../../../TSDK-master/src/Core/source/thread.h:72,  
                 from ../../../TSDK-master/src/Core/source/Thread.cpp:95:  
/usr/local/boost_1_53_0/boost/bind/bind_template.hpp:20:59:    
instantiated from ‘boost::_bi::bind_t<R, F, L>::result_type  
boost::_bi::bind_t<R, F, L>::operator()() [with R = void, F = unsigned  
int (**)(void*), L = boost::_bi::list1<boost::_bi::value<void*> >,  
boost::_bi::bind_t<R, F, L>::result_type = void]’  
/usr/local/boost_1_53_0/boost/thread/detail/thread.hpp:117:17:    
instantiated from ‘void boost::detail::thread_data<F>::run() [with F =  
boost::_bi::bind_t<void, unsigned int (**)(void*),  
boost::_bi::list1<boost::_bi::value<void*> > >]’  
../../../TSDK-master/src/Core/source/Thread.cpp:202:50:   instantiated  
from here /usr/local/boost_1_53_0/boost/bind/bind.hpp:253:9: error:  
‘boost::_bi::unwrapper<F>::unwrap [with F = unsigned int  
(**)(void*)]((* & f), 0l)’ cannot be used as a function  

我完全不知道是什么原因造成的。有人可以帮忙吗?

编辑:
thread.h 的一部分

#include <boost/thread/thread.hpp> 
#include <cstdio>

class Thread
{
protected:
    boost::thread*            m_Thread;
    unsigned int id;
private:
    bool                      m_pause;
    boost::mutex              m_pause_mutex;
    boost::condition_variable m_pause_changed;
    bool threadCreated;

public:

    Thread();
    Thread(unsigned int id);
    ~Thread();
    bool Start();
    bool CreateThread(unsigned int Function(void* pParam),void *pParam);
    void Terminate();
    unsigned int myThreadProc(void* param);
    bool IsRunning();
    bool IsStarted();

protected:
    virtual unsigned int ThreadProc() = 0;
};

和thread.cpp的一部分

#include "thread.h"
Thread::Thread() : m_Thread(NULL), m_pause(true), threadCreated(false), id(NULL)
{
}//Thread ctor

Thread::Thread(unsigned int _id) : m_Thread(NULL), id(_id), threadCreated(false), m_pause(true)
{}

Thread::~Thread()
{
    if (NULL != m_Thread) { delete m_Thread; }
}

unsigned int Thread::GetThreadID() { return id; }//GetThreadID

bool Thread::Start()
{
    m_Thread = new boost::thread(&Thread::myThreadProc, this, this);
    threadCreated = true;
    return threadCreated;
}

bool Thread::CreateThread(unsigned int Function(void* pFunctionParam),void *pParam)
{
    //Receives Thread Function,pParam as Arguments and starts Thread.
    m_Thread= new boost::thread(&Function,pParam);
    threadCreated = true;
    return threadCreated;
}

void Thread::Terminate()
{
    if (NULL != m_Thread) {
        m_Thread->join();
        m_Thread=0;
    }
    else {printf("No thread was created\n");}
}

unsigned int Thread::myThreadProc(void* param)
{
    Thread* ourBoostThread = (Thread*) param;
    int ret = ourBoostThread->ThreadProc();
    return ret;
}//_ThreadProc

bool Thread::IsRunning() { return !m_pause; }
bool Thread::IsStarted() { return threadCreated; }

第 202 行是bool Thread::IsStarted() { return threadCreated; }

4

1 回答 1

2
m_Thread= new boost::thread(&Function,pParam);

Function实际上是一个指向函数的指针,尽管它看起来不像。如果声明一个函数类型的函数参数,那么它实际上是一个函数指针;就像声明为数组的函数参数实际上是指针一样。

指向该函数指针的指针也是如此&Function,它不能像函数一样被调用。将其更改为 just Function,即可。

于 2013-05-07T08:16:50.123 回答