c++c++11lambdapthreads

passing Lambda to pthread_create?


I am trying to send Lambda to another function and use Pthread library to run the lambda across several threads:

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

using namespace std;

template<class InputIt, class Function>
inline Function parallel_fun(InputIt first, InputIt last, Function f)
{
    pthread_t threads[4];
    for (int i=0; first != last; ++first) {
        pthread_create(&threads[i], nullptr,f , nullptr);
        i++;
    }

    for (int i=0; i<4;i++) {
        pthread_join(threads[i],nullptr);
    }
    return f;
}

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

    return 0;
}

I compile using the following:

g++ -std=c++0x -pthread test.cpp -o a`

...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*)’

Solution

  • I am not sure about the new C++11 API. I don't think I have enough time to learn new API.

    This is your lucky day. The C++11 API was strongly influenced by pthreads. There is nearly a mechanical translation. Here is your code cast in 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.