简体   繁体   English

将Lambda传递给pthread_create?

[英]passing Lambda to pthread_create?

I search all the net for answer, but I didn't find any solution. 我搜索所有网络的答案,但我没有找到任何解决方案。 Can you please help. 你能帮忙吗? My problem is I am trying to send Lambda to another function and use Pthread library to run the lambda across several threads. 我的问题是我正在尝试将Lambda发送到另一个函数并使用Pthread库来跨多个线程运行lambda。 Next is the code: 接下来是代码:

  1    #include <iostream>
  2    #include <stdlib.h>
  3    #include <pthread.h>
  4    #include <vector>
  5 
  6    using namespace std;
  7 
  8 
  9   template<class InputIt, class Function>
 10    inline Function parallel_fun(InputIt first, InputIt last, Function f)
 11    {
 12         pthread_t threads[4];
 13 
 14       for (int i=0; first != last; ++first) {
 15 
 16          pthread_create(&threads[i], nullptr,f , nullptr);
 17 
 18           i++;
 19        }
 20 
 21      for (int i=0; i<4;i++) {
 22 
 23          pthread_join(threads[i],nullptr);
 24 
 25 
 26        }
 27 
 28 
 29 
 30 
 31    return f;
 32   }
 33 
 34 
 35    int main()
 36   {
 37    int z=90;
 38    vector<int> a(4);
 39     a[0]=1; a[1]=2;
 40     parallel_fun(a.begin(), a.end(), [=](void* data) -> void*
 41                     {
 42          cout<<"test"<<z<<endl;
 43            //do something
 44          });
 45 
 46 
 47 
 48 return 0;
 49 }

I compile using the following line: g++ -std=c++0x -pthread test.cpp -oa 我使用以下行进行编译: g++ -std=c++0x -pthread test.cpp -oa

and I receive this error: 我收到此错误:

test.cpp: In function ‘Function parallel_fun(InputIt, InputIt, Function) [with InputIt = __gnu_cxx::__normal_iterator<int*, std::vector<int> >, Function = main()::<lambda(void*)>]’:
test.cpp:44:11:   instantiated from here
test.cpp:16:10: error: cannot convert ‘main()::<lambda(void*)>’ to ‘void* (*)(void*)’ for argument ‘3’ to ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’

I am not sure about the new C++11 API. 我不确定新的C ++ 11 API。 I don't think I have enough time to learn new API. 我认为我没有足够的时间学习新的API。

This is your lucky day. 这是你的幸运日。 The C++11 API was strongly influenced by pthreads. C ++ 11 API受到pthreads的强烈影响。 There is nearly a mechanical translation. 几乎是机械翻译。 Here is your code cast in C++11: 这是您在C ++ 11中的代码转换:

#include <iostream>
#include <stdlib.h>
#include <thread>
#include <vector>

template<class InputIt, class Function>
inline
Function
parallel_fun(InputIt first, InputIt last, Function f)
{
    std::thread threads[4];
    for (int i=0; first != last; ++first)
    {
        threads[i] = std::thread(f);
        i++;
    }
    for (int i=0; i<4;i++)
    {
        threads[i].join();
    }
    return f;
}


int main()
{
    int z=90;
    std::vector<int> a(4);
    a[0]=1; a[1]=2;
    parallel_fun(a.begin(), a.end(), [=]()
                                      {
                                        std::cout<<"test" << z << std::endl;
                                        //do something
                                      });
}

Your alternative, is to figure out how std::thread gets implemented on top of pthreads, which trust me, is much more complicated than the C++11 translation shown above. 你的另一种方法是弄清楚如何在pthreads之上实现std::thread ,它相信我,比上面显示的C ++ 11转换要复杂得多。

There is no conversion from a lambda to a function pointer unless the lambda has no captures. 除非 lambda没有捕获, 否则没有从lambda到函数指针的转换。 (§5.1.2p6). (§5.1.2p6)。 So if you require the capture of z , you are out of luck. 所以如果你需要捕获z ,那你就不走运了。

The C interface expects you to use the void* argument for your closures. C接口希望您为闭包使用void*参数。 You could do that, which would be ugly (but C-like), or you could use the new C++11 thread support library, if your C++ environment supports it. 您可以这样做,这将是丑陋的(但类似于C),或者您可以使用新的C ++ 11线程支持库,如果您的C ++环境支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM