gpt4 book ai didi

c++ - 类型 'void* (...)(void*)' 的参数与 'void* (*)(void*) 不匹配

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

我目前正在 GNU Radio 上开发一个 bloc,我想使用一个线程。该线程用于从 UDP 套接字获取数据,因此我可以在我的 GNU Radio 集团中使用它。 “一般工作”功能是执行所有信号和数据处理的功能。

主源文件是这样组织的:

namespace gr {
namespace adsb {

out::sptr
out::make()
{
return gnuradio::get_initial_sptr
(new out_impl());
}

/*
* UDP thread
*/
void *task_UdpRx (void *arg)
{
while(true)
{
printf("Task UdpRx\n\r");
usleep(500*1000);
}
pthread_exit(NULL);
}

/*
* The private constructor
*/
out_impl::out_impl()
: gr::block("out",
gr::io_signature::make(1, 1, sizeof(int)),
gr::io_signature::make(1, 1, sizeof(char)))
{
pthread_t Thread_UdpRx;

//Thread init
if(pthread_create(&Thread_UdpRx, NULL, task_UdpRx, NULL))
{
err("Pthread error");
}
else
{
printf("UDP thread initialization completed\n\r");
}
}

/*
* Our virtual destructor.
*/
out_impl::~out_impl()
{
}

void out_impl::forecast (int noutput_items, gr_vector_int &ninput_items_required)
{
ninput_items_required[0] = noutput_items;
}

int out_impl::general_work (int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
{
const int *in = (const int *) input_items[0];
char *out = (char *) output_items[0];

// Do <+signal processing+>
for(int i = 0; i < noutput_items; i++)
{
printf("General work\n\r");
}/* for < noutput_items */

// Tell runtime system how many input items we consumed on
// each input stream.
consume_each (noutput_items);

// Tell runtime system how many output items we produced.
return noutput_items;
} /* general work */

} /* namespace adsb */
} /* namespace gr */`

我遇到的问题是,当我尝试编译时,出现此错误:

In constructor ‘gr::adsb::out_impl::out_impl()’:
error: argument of type ‘void* (gr::adsb::out_impl::)(void*)’ does not match ‘void* (*)(void*)’

此错误指的是行,它涉及 task_UdpRx :

if(pthread_create(&Thread_UdpRx, NULL, task_UdpRx, NULL))

有人知道吗?

如有需要,请随时询问更多详细信息。我显示的代码是我能做的最短的代码,以便您尽可能地理解。

谢谢!

最佳答案

您不能将指向成员函数的指针传递给 pthread_create() .

你需要一个免费的功能:

void *thread_start(void *object)
{
gr::adsb::out_impl *object = reinterpret_cast<gr::adsb::out_impl *>(object);
object.task_UpdRx(0);
return 0;
}

左右,您需要将此函数传递给 pthread_create()并通过 this指针作为数据参数:

if (pthread_create(&Thread_UdpRx, NULL, thread_start, this))

我保留滥用 reinterpret_cast<>() 的权利— 替换正确的转换符号。我对在构造函数中启动线程持保留意见,但这是应该起作用的方案的概要。


2014-06-25代码更新后

代码仍然不是 MCVE。我不能复制它并编译它。缺少许多 header 。类型 outout_impl未定义/声明。可能会遗漏一些 GNU Radio 继承内容。

这是我的 MCVE 版本。它并不完美,因为它不会重现您的问题。它可以在带有 GCC 4.9.0 的 Ubuntu 12.04 LTS 衍生版本上干净地编译。它假设你有一个标题 <err.h>定义函数 err() .我不确定 MCVE 是否需要析构函数(删除它会消除另外 5 行左右)。

来源

#include <cstdio>
#include <pthread.h>
#include <unistd.h>
#include <err.h>

namespace gr
{
namespace adsb
{
class out_impl
{
public:
out_impl();
virtual ~out_impl();
};

void *task_UdpRx(void *arg)
{
while (true)
{
printf("Task UdpRx %p\n\r", arg);
usleep(500*1000);
}
pthread_exit(NULL);
}

out_impl::out_impl()
{
pthread_t Thread_UdpRx;

if (pthread_create(&Thread_UdpRx, NULL, task_UdpRx, NULL))
err(1, "Pthread error");
else
printf("UDP thread initialization completed\n\r");
}

out_impl::~out_impl()
{
}

}
}

编译

$ g++ --version
g++ (GCC) 4.9.0
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ make gr.o
g++ -g -O3 -std=c++11 -Wall -Wextra -Werror -c gr.cpp
$ nm -C -g gr.o
0000000000000010 T gr::adsb::task_UdpRx(void*)
0000000000000050 T gr::adsb::out_impl::out_impl()
0000000000000050 T gr::adsb::out_impl::out_impl()
0000000000000040 T gr::adsb::out_impl::~out_impl()
0000000000000000 T gr::adsb::out_impl::~out_impl()
0000000000000000 T gr::adsb::out_impl::~out_impl()
0000000000000000 V typeinfo for gr::adsb::out_impl
0000000000000000 V typeinfo name for gr::adsb::out_impl
U vtable for __cxxabiv1::__class_type_info
0000000000000000 V vtable for gr::adsb::out_impl
U operator delete(void*)
U err
U printf
U pthread_create
U usleep
$

后续步骤

这个问答可以从这里开始的几种方式:

  1. 问题被放弃或关闭。
  2. 您获取 MCVE 代码并添加内容,直到出现原始编译错误。
  3. 你使用你的非编译代码并删除一些东西,直到你到达一个 MCVE,你可以从中删除任何东西而不删除错误。最好,代码不应引用在常规 Linux 安装中找不到的任何 header 。如果必须,您需要确定从哪里可以获得它们 — 是的,如果必须的话,我可能可以找到 GNU Radio,但我不需要这样做。

关于c++ - 类型 'void* (...)(void*)' 的参数与 'void* (*)(void*) 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24310733/

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