首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >std::线程参数在转换为rvalue后必须是可调用的。

std::线程参数在转换为rvalue后必须是可调用的。
EN

Stack Overflow用户
提问于 2021-04-26 14:39:22
回答 2查看 2.9K关注 0票数 1

我正在尝试创建两个线程,它们可以处理浮点数组。Howere编译器给出了以下错误:

代码语言:javascript
运行
复制
/usr/include/c++/9/thread: In instantiation of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(bool*, float*, float*, float*); _Args = {bool*, float (*)[10], float (*)[10], float (*)[10]}; <template-parameter-1-3> = void]’:
main.cpp:16:66:   required from here
/usr/include/c++/9/thread:120:44: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
  120 |           typename decay<_Args>::type...>::value,
      |                                            ^~~~~
/usr/include/c++/9/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (*)(bool*, float*, float*, float*), bool*, float (*)[10], float (*)[10], float (*)[10]> >’:
/usr/include/c++/9/thread:131:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(bool*, float*, float*, float*); _Args = {bool*, float (*)[10], float (*)[10], float (*)[10]}; <template-parameter-1-3> = void]’
main.cpp:16:66:   required from here
/usr/include/c++/9/thread:243:4: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<void (*)(bool*, float*, float*, float*), bool*, float (*)[10], float (*)[10], float (*)[10]> >::__result<std::tuple<void (*)(bool*, float*, float*, float*), bool*, float (*)[10], float (*)[10], float (*)[10]> >’
  243 |    _M_invoke(_Index_tuple<_Ind...>)
      |    ^~~~~~~~~
/usr/include/c++/9/thread:247:2: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<void (*)(bool*, float*, float*, float*), bool*, float (*)[10], float (*)[10], float (*)[10]> >::__result<std::tuple<void (*)(bool*, float*, float*, float*), bool*, float (*)[10], float (*)[10], float (*)[10]> >’
  247 |  operator()()
      |  ^~~~~~~~
/usr/include/c++/9/thread: In instantiation of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(float*); _Args = {float (*)[10]}; <template-parameter-1-3> = void]’:
main.cpp:17:43:   required from here
/usr/include/c++/9/thread:120:44: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
  120 |           typename decay<_Args>::type...>::value,
      |                                            ^~~~~
/usr/include/c++/9/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (*)(float*), float (*)[10]> >’:
/usr/include/c++/9/thread:131:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(float*); _Args = {float (*)[10]}; <template-parameter-1-3> = void]’
main.cpp:17:43:   required from here
/usr/include/c++/9/thread:243:4: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<void (*)(float*), float (*)[10]> >::__result<std::tuple<void (*)(float*), float (*)[10]> >’
  243 |    _M_invoke(_Index_tuple<_Ind...>)
      |    ^~~~~~~~~
/usr/include/c++/9/thread:247:2: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<void (*)(float*), float (*)[10]> >::__result<std::tuple<void (*)(float*), float (*)[10]> >’
  247 |  operator()()
      |  ^~~~~~~~

我以为我忘记了dma线程的一些ctor参数,但事实并非如此。这是我的代码:

代码语言:javascript
运行
复制
#include <iostream>
#include <cstdint>
#include <thread>

void dma_service(bool*, float*, float*, float*);
void live_audio(float*);
void live_draw(float*);


    
    constexpr int d_size = 10;
    float data[d_size];
    float pwm_cp[d_size];
    float fft_cp[d_size];
    bool drdy = 0;
    std::thread dma_thrd(dma_service, &drdy, &data, &pwm_cp, &fft_cp);
    std::thread audio_thrd(live_draw, &pwm_cp);


int main(){
    for (auto i = 0; i < d_size; i++)
    {
        data[i] = 0;
        pwm_cp[i] = 0;
    }
    printf("Hello, World!\n");
    drdy = 1;

    int k = 3;
    while(k-->0)
    {
        audio_thrd.join();
        printf("\n\n\n");
        
        for (auto i = 0; i < d_size; i++)
        {
            data[i]++;
        }
        drdy = 1;
    }

}

void dma_service(bool* flag, float* d_array, float* pwm_array, float* fft_array) {
    while (!flag) ;

    for (auto i = 0; i < d_size; i++)
    {
        pwm_array[i] = d_array[i];
        fft_array[i] = d_array[i];
    }

    flag = 0;

}

void live_audio(float* pwm_array) {
    dma_thrd.join();
    printf("Audio Array:\n");
    for (auto i = 0; i < d_size; i++)
    {
        printf("PWM_CP[%d] %f\n", i, pwm_array[i]);
    }
}

我对lambda函数没有太多的经验,所以我不知道这里是否需要它们(并且很乐意避免它们)。谢谢你的建议!

EN

Stack Overflow用户

发布于 2021-04-26 14:49:51

要具有匹配的参数,您需要:

代码语言:javascript
运行
复制
constexpr int d_size = 10;
float data[d_size];
float pwm_cp[d_size];
float fft_cp[d_size];
bool drdy = 0;

void dma_service(bool*, float*, float*, float*);
std::thread dma_thrd(dma_service, &drdy, &data[0], &pwm_cp[0], &fft_cp[0]);

演示

代码语言:javascript
运行
复制
constexpr int d_size = 10;
float data[d_size];
float pwm_cp[d_size];
float fft_cp[d_size];
bool drdy = 0;

void dma_service(bool*, float(*)[10], float(*)[10], float(*)[10]);
std::thread dma_thrd(dma_service, &drdy, &data, &pwm_cp, &fft_cp);
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67268682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档