【发布时间】:2012-10-29 19:41:25
【问题描述】:
我对 c++ 还很陌生,我正在做一个关于 TCP 的项目。
我需要创建一个线程,所以我用谷歌搜索并找到了这个。 http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
我遵循它的语法但遇到错误: 'void* (ns3::TcpSocketBase::)()' 类型的参数不匹配'void* ()(void)'
代码:
tcp-socket-base.h:
class TcpSocketBase : public TcpSocket
{
public:
...
void *threadfunction();
....
}
tcp-socket-base.cc:
void
*TcpSocketBase::threadfunction()
{
//do something
}
..//the thread was create and the function is called here
pthread_t t1;
int temp = pthread_create(&t1, NULL, ReceivedSpecialAck, NULL); //The error happens here
return;
...
任何帮助将不胜感激。谢谢!
编辑:
我接受了建议,并将线程函数设为非成员函数。
namespaceXXX{
void *threadfunction()
int result = pthread_create(&t1, NULL, threadfunction, NULL);
NS_LOG_LOGIC ("TcpSocketBase " << this << " Create Thread returned result: " << result );
void *threadfunction()
{
.....
}
}
但我得到了这个错误:
初始化“int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)”的参数 3 [-fpermissive]
【问题讨论】:
-
另外,考虑改用 boost::thread - 它会让你的生活更轻松。
标签: c++