c++多线程函数调用方法
时间: 2024-05-31 16:03:51 浏览: 27
以下是两种C++多线程函数调用方法的例子:
1. 使用类成员函数
```cpp
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();
return 0;
}
```
2. 使用lambda表达式
```cpp
// 普通函数
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;
}
```
相关问题
c++多线程与函数指针
好的,您的问题是:
在 C 中如何使用多线程和函数指针?
答案:
在 C 中,使用多线程需要使用线程库,例如 pthread。使用 pthread 库可以通过创建线程来实现多线程编程。下面是一个使用 pthread 创建线程的例子:
```c
#include <stdio.h>
#include <pthread.h>
void *my_func(void *arg) {
printf("Hello from thread!\n");
return NULL;
}
int main() {
pthread_t tid;
pthread_create(&tid, NULL, my_func, NULL);
pthread_join(tid, NULL);
printf("Thread finished.\n");
return 0;
}
```
在上面的例子中,`pthread_create` 函数用于创建一个新的线程,`my_func` 函数是新线程将要执行的函数。`pthread_join` 函数用于等待新线程结束。
关于函数指针,在 C 中我们可以通过定义函数指针变量来调用函数。例如:
```c
#include <stdio.h>
void my_func(int arg) {
printf("my_func is called with arg=%d\n", arg);
}
int main() {
void (*func_ptr)(int);
func_ptr = my_func;
func_ptr(123);
return 0;
}
```
在上面的例子中,我们定义了一个函数指针变量 `func_ptr`,并将其赋值为 `my_func` 函数。然后,我们可以直接调用函数指针变量来调用 `my_func` 函数。
c++ 不同线程中调用回调函数
在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;
}
```
在上面的示例代码中,我们使用了一个线程池来管理线程。当需要执行任务时,我们可以将任务和回调函数一起放入任务队列中。每个线程都会从任务队列中获取任务并执行,当任务完成后,线程会将结果传递给回调函数。由于回调函数可能会被不同的线程调用,我们需要使用锁来保护共享资源。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)