C++ 多线程-thread调用不同类型的函数

目录

thread类的构造函数概述

1. 普通函数

2. 类成员函数

 3. 仿函数

4.匿名函数

5.引用作为参数来传递

 参考


thread类的构造函数概述

thread构造函数的参数是一个可变的参数模板,可用来调用各种函数且传递若干的参数。

std::thread(可调用函数 , 若干个参数)

其中可调用函数的类型有:

1) 普通函数

2) 成员函数

3) 重载operator()运算符,即仿函数

4) lambda表达式,即匿名函数

1. 普通函数

void fun0() {
	cout << "调用普通无参函数" << endl;
}
void fun1(int n , string s) {
	cout << s << "调用普通有参函数" << n;
}
int main() {
	thread t0(fun0);
	thread t1(fun1 , 1 , "abc");
	t0.join();
	t1.join();
    return 0;
}

输出:

2. 类成员函数

class A
{
public:
	void a(int n);

};

void A::a(int n) {
	cout << "A: " << n << endl;
}
int main() {
	A aClass;
	thread aa(&A::a, &aClass, 5);
	aa.join();
}

输出:

 3. 仿函数

仿函数(Functor)又称为函数对象(Function Object)是一个能行使函数功能的类。仿函数的语法几乎和我们普通的函数调用一样,不过作为仿函数的类,都必须重载 operator() 运算符。因为调用仿函数,实际上就是通过类对象调用重载后的 operator() 运算符。(为什么仿函数必须重载()运算符)

class StringAppend
{
public:
    //关键字explicit防止发生隐式类型转换
    //:为初始化列表(把str的值赋给ss),用于初始化类成员变量的值,此过程发生在类中赋值语句执行之前
    //如本例子中变量ss是被const修饰的,该变量就只能被初始化列表赋值,而不能用赋值语句=赋值
    explicit StringAppend(const string& str) : ss(str){}    
    void operator() (const string& str) const
    {
         cout<<str<<' '<<ss<<endl;
    }
private:
    const string ss;
};

int main()
{
    //调用构造函数StringAppend,将and world赋值给ss
    StringAppend myFunctor2("and world!");
    //重载运算符(),将两个字符串一起输出
    //通过重载类里的()运算符使其调用形式很像函数调用,故称之为仿函数
    thread t1(myFunctor2,"Hello");
	t1.join();
}

输出:

4.匿名函数

 

int main() {
    //[]实际就是匿名
	std::thread t1([]() {std::cout << "hello "; });
	t1.join();

	std::thread t2([](std::string str) {std::cout << str << std::endl; }, "world");
	t2.join();

	return 0;
}

输出:

5.引用作为参数来传递

class Fctor {
public:
	// 具有一个参数 是引用,报错
	void operator() (std::string& msg) {
		msg = "wolrd";
	}
};

int main() {
	Fctor f;
	std::string m = "hello";
    //ref()就是引用
	std::thread t1(f, ref(m));

	t1.join();
	std::cout << m << std::endl;
	return 0;
}

 参考

【C++11 多线程】仔细地将参数传递给线程(三) - fengMisaka - 博客园 (cnblogs.com)

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,可以使用多线程来执行任务,同时也可以使用回调函数来处理任务执行后的结果。如果回调函数需要在不同线程中被调用,需要考虑线程安全性和同步问题。 一种常见的做法是使用线程池来管理线程,每个线程都从任务队列中获取任务并执行。当任务完成后,线程调用回调函数来处理任务执行的结果。为了确保回调函数线程安全性,可以使用锁来保护共享资源。 以下是一个简单的示例代码: ```c++ #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <queue> using namespace std; class ThreadPool { public: ThreadPool(int numThreads) : stop(false) { for (int i = 0; i < numThreads; ++i) { threads.emplace_back([this] { while (true) { function<void()> task; { unique_lock<mutex> lock(mutex_); condition_.wait(lock, [this] { return stop || !tasks_.empty(); }); if (stop && tasks_.empty()) return; task = move(tasks_.front()); tasks_.pop(); } task(); } }); } } ~ThreadPool() { { unique_lock<mutex> lock(mutex_); stop = true; } condition_.notify_all(); for (thread& thread : threads) { thread.join(); } } template<class F> void enqueue(F&& f) { { unique_lock<mutex> lock(mutex_); tasks_.emplace(forward<F>(f)); } condition_.notify_one(); } private: vector<thread> threads; queue<function<void()>> tasks_; mutex mutex_; condition_variable condition_; bool stop; }; void callback(int result) { cout << "Result: " << result << endl; } int main() { ThreadPool pool(4); pool.enqueue([] { this_thread::sleep_for(chrono::seconds(1)); return 42; }, callback); return 0; } ``` 在上面的示例代码中,我们使用了一个线程池来管理线程。当需要执行任务时,我们可以将任务和回调函数一起放入任务队列中。每个线程都会从任务队列中获取任务并执行,当任务完成后,线程会将结果传递给回调函数。由于回调函数可能会被不同线程调用,我们需要使用锁来保护共享资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值