gpt4 book ai didi

c++ - 使用 pthread_create 时出错

转载 作者:行者123 更新时间:2023-11-30 02:02:27 27 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
pthread Function from a Class

我是 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]

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com