我的程序如下所示
#include <iostream>
#include <thread>
class A {
public:
void foo(int n ) { std::cout << n << std::endl; }
};
int main()
{
A a;
std::thread t1(&A::foo, std::ref(a), 100);
t1.join();
return 0;
}
当我使用下面的命令编译它时,我得到了错误
g++ -o main main.cc -lpthread -std=c++11
错误:
In file included from /usr/local/include/c++/4.8.2/thread:39:0,
from check.cc:2:
/usr/local/include/c++/4.8.2/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (A::*)(int)>(std::reference_wrapper<A>, int)>’:
/usr/local/include/c++/4.8.2/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (A::*)(int); _Args = {std::reference_wrapper<A>, int}]’
check.cc:13:42: required from here
/usr/local/include/c++/4.8.2/functional:1697:61: error:no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (A::*)(int)>(std::reference_wrapper<A>, int)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/local/include/c++/4.8.2/functional:1727:9: error:no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (A::*)(int)>(std::reference_wrapper<A>, int)>’
_M_invoke(_Index_tuple<_Indices...>)
^
发布于 2014-01-11 14:08:22
这里不是引用包装器的正确位置。然而,一个简单的指针就足够了,并达到了预期的结果:
std::thread t1(&A::foo, &a, 100);
发布于 2014-01-21 00:37:34
关于你的问题标题,我会使用lambda来构造线程。通过调用成员函数或绑定参数,使用或不使用引用。
std::thread t1([&] { a.foo(100); });
发布于 2015-10-03 09:08:29
好的,问题是ref(obj)返回一个对象的引用(别名),而不是一个指针(地址)!要使用线程,我们需要指针,而不是引用!下面是一个将函数指针与线程一起使用的方便程序:
#include <iostream>
#include "vector"
#include "string"
#include "thread"
#include "atomic"
#include "functional"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "assert.h"
using namespace std;
//__________________________Global variables_________________________________________________
atomic<int> var(0);
//__________________________class____________________________________________________________
class C
{
public:
C()
{}
static void addition (int a, int b)
{
for(int i= 0; i< a+b; i++)
var++;
}
void subtraction (int a, int b)
{
for(int i= 0; i< a+b; i++)
var--;
}
};
class D : std::atomic<int>
{
public:
D() : std::atomic<int>(0)
{}
void increase_member (int n)
{
for (int i=0; i<n; ++i)
fetch_add(1);
}
int get_atomic_val()
{
return this->load();
}
};
//________________________________functions________________________________________________
void non_member_add (int a, int b)
{
for(int i= 0; i< a+b; i++)
var++;
}
//__________________________________main____________________________________________________
int main ()
{
int a=1, b=5;
// (I)...........................................static public member function (with no inheritance).........................................
void (* add_member_func_ptr)(int,int) = C::addition; // pointer to a static public member function
//defining thread pool for ststic public member_add_ptr
vector<thread> thread_pool;
for (int i=0; i<5; i++)
{
thread_pool.push_back(thread(add_member_func_ptr,a,b));
}
for(thread& thr: thread_pool)
thr.join();
cout<<"static public member function (with no inheritance)\t"<<var<<endl;
//defining thread pool for ststic public member function
var=0;
thread_pool.clear();
for (int i=0; i<5; i++)
{
thread_pool.push_back(thread(C::addition,a,b)); //void (* add_member_func_ptr)(int,int) is equal to C::addition
}
for(thread& thr: thread_pool)
thr.join();
cout<<"static public member function (with no inheritance)\t"<<var<<endl;
// (II)..............................................non-static public member function (with no inheritance)...................................
C bar;
void (C::* sub_member_func_ptr)(int,int) = & C::subtraction; // pointer to a non-static public member function
var=0;
//defining thread pool for non-ststic public member function
thread_pool.clear();
for (int i=0; i<5; i++)
{
thread_pool.push_back(thread(sub_member_func_ptr,bar,a,b));
}
for(thread& thr: thread_pool)
thr.join();
cout<<"non-static public member function (with no inheritance)\t"<<var<<endl;
var=0;
//defining thread pool for non-ststic public member function
thread_pool.clear();
for (int i=0; i<5; i++)
{
thread_pool.push_back(thread(&C::subtraction,bar,a,b)); //void (C::* sub_member_func_ptr)(int,int) equals & C::subtraction;
}
for(thread& thr: thread_pool)
thr.join();
cout<<"non-static public member function (with no inheritance)\t"<<var<<endl;
// (III)................................................non-member function .................................................
void (* non_member_add_ptr)(int,int) = non_member_add; //pointer to a non-member function
var=0;
//defining thread pool for non_member_add
thread_pool.clear();
for (int i=0; i<5; i++)
{
thread_pool.push_back(thread(non_member_add,a,b));
}
for(thread& thr: thread_pool)
thr.join();
cout<<"non-member function\t"<<var<<endl<<endl;
// (IV)...........................................non-static public member function (with inheritance).........................
D foo;
void (D::* member_func_ptr) (int) = & D::increase_member; //pointer to a non-static public member function of a derived class
//defining thread pool for non-ststic public member function of a derived class
thread_pool.clear();
for (int i=0; i<5; i++)
{
thread_pool.push_back(thread(member_func_ptr,&foo,10)); //use &foo because this is derived class!
}
for(thread& thr: thread_pool)
thr.join();
cout<<"non-static public member function (with inheritance)\t"<<foo.get_atomic_val()<<endl;
//defining thread pool for non-ststic public member function
D poo;
thread_pool.clear();
for (int i=0; i<5; i++)
{
reference_wrapper<D> poo_ref= ref(poo);
D& poo_ref_= poo_ref.get(); //ref(obj) returns a reference (alias) to an object not a pointer(address)!
D* d_ptr= &poo; //to work with thread we need pointers not references!
thread_pool.push_back(thread(&D::increase_member, d_ptr,10)); //void (D::* member_func_ptr) (int) equals & D::increase_member;
}
for(thread& thr: thread_pool)
thr.join();
cout<<"non-static public member function (with inheritance)\t"<<poo.get_atomic_val()<<endl<<endl;
return 0;
}
https://stackoverflow.com/questions/21059115
复制相似问题