gpt4 book ai didi

c++ - C++ 中的 Posix 线程

转载 作者:太空狗 更新时间:2023-10-29 23:38:32 26 4
gpt4 key购买 nike

如何在 linux c++ 中实现 posix 线程。将 smme 程序另存为“.c 并使用 c 编译器运行是可以的。但在 c++ 中它给出错误..

我想我在编译时犯了错误是否有任何要包含的标记,例如 c++ 的“-lpthread”

有人可以发送有效代码吗...?

实际上这是我的代码

int cooperbussman :: startlistenthread()
{
if(pthread_create(&m_thread,0,&packetreadertask,0)<0)
{
cout<<"Unable to create the thread Startlistenthread\n";
return -1;
}
return 1;

我得到的错误是

cooperbussman.cpp: In member function âint cooperbussman::startlistenthread()â:cooperbussman.cpp:76: error: invalid conversion from âvoid* (*)()â to âvoid* (*)(void*)âcooperbussman.cpp:76: error:   initializing argument 3 of âint pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)â

最佳答案

您的packetreadertask 函数必须是一个将单个void * 作为参数的函数。这是重要的错误信息:

cooperbussman.cpp:76: error: invalid conversion from âvoid* (*)()â to âvoid* (*)(void*)â

Your function is declared something like this:

void *packetreadertask();

它必须在哪里:

void *packetreadertask(void *);

关于c++ - C++ 中的 Posix 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/580363/

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