问题描述
我有一个简单的基于pthread的Threads类,它可以和标准的静态回调函数一起工作。
可以将Threads
: .cpp:27:26:error:从类型main(int,char * )::转换为无效类型 thread_cb()
需要处理泛型转换 void *
/ li>
我怀疑第二个问题可以用模板方法或者std :: function解决,但不知道如何。
#include< vector>
#include< iostream>
#include< pthread.h>
类线程
{
public:
Threads(){}
〜Threads(){}
private:
static void * thread_cb(void * v)
{
//下面也需要更改
void(* my_fptr)()=
reinterpret_cast< void *)()>(reinterpret_cast< long long>(v));
my_fptr();
return nullptr;
}
public:
template< typename CALLBACK>
void spawn(CALLBACK cb)
{
pthread_t t;
void * p =(void *)(cb); // problem here
pthread_create(& t,nullptr,thread_cb,p);
m_threads.push_back(t);
}
void join_all()
{
for(auto& p:m_threads)
pthread_join(p,nullptr)
}
private:
std :: vector< pthread_t> m_threads;
};
static void my_cb()
{
std :: cerr<< bar< std :: endl;
}
int main(int argc,char ** argv)
{
Threads t;
t.spawn(my_cb); // ok
t.spawn([](){std :: cerr<<foo<< std :: endl;}); // not ok
t.join_all();
return 0;
}
函数指针转换。 ...因为它可能,我强烈推荐 std :: thread
。
template< typename CALLBACK>
void spawn(CALLBACK cb)
{
pthread_t t;
// void * p =(void *)(cb);
// pthread_create(& t,nullptr,thread_cb,p);
void(* pfn)()= cb; //函数指针指向`void()`
pthread_create(& t,nullptr,thread_cb,reinterpret_cast< void *>(pfn));
m_threads.push_back(t);
}
I have a simple Threads class based on pthreads which works fine with a standard static callback function.
Is it possible to generalize Threads to work with lambdas, too?
problems:
sandbox.cpp:27:26: error: invalid cast from type ‘main(int, char*)::’ to type ‘void’
thread_cb()
needs to deal with generically castingvoid*
back into something callable
I suspect the second problem may be solved with template methods or maybe std::function, but not sure how.
#include <vector>
#include <iostream>
#include <pthread.h>
class Threads
{
public:
Threads() { }
~Threads() { }
private:
static void *thread_cb( void *v )
{
// following will also need to change
void (*my_fptr)() =
reinterpret_cast<void(*)()>(reinterpret_cast<long long>(v));
my_fptr();
return nullptr;
}
public:
template<typename CALLBACK>
void spawn( CALLBACK cb )
{
pthread_t t;
void *p = (void*)( cb ); // problem here
pthread_create( &t, nullptr, thread_cb, p );
m_threads.push_back( t );
}
void join_all()
{
for ( auto& p : m_threads )
pthread_join( p, nullptr );
}
private:
std::vector< pthread_t > m_threads;
};
static void my_cb()
{
std::cerr << "bar" << std::endl;
}
int main(int argc, char** argv)
{
Threads t;
t.spawn( my_cb ); // ok
t.spawn( []() { std::cerr << "foo" << std::endl; } ); // not ok
t.join_all();
return 0;
}
You can use "lambda to function pointer" conversion. ...be that as it may, I strongly recommend std::thread
.
template<typename CALLBACK>
void spawn( CALLBACK cb )
{
pthread_t t;
// void *p = (void*)( cb );
// pthread_create( &t, nullptr, thread_cb, p );
void (*pfn)() = cb; // function pointer to `void()`
pthread_create( &t, nullptr, thread_cb, reinterpret_cast<void*>(pfn) );
m_threads.push_back( t );
}
这篇关于泛化C ++ 11 Threads类使用lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!