马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
在 C++ 中,线程干系功能告急通过头文件提供的类和函数来实现,以下是一些常用的线程接口方法和利用本领:
std::thread类
构造函数:
可以通过传入可调用对象(如函数指针、函数对象、lambda 表达式等)来创建一个线程实例,启动一个新线程去实行对应的任务。比方:- #include <iostream>
- #include <thread>
- void hello() {
- std::cout << "Hello from thread!" << std::endl;
- }
- int main() {
- std::thread t(hello); // 创建线程t并开始执行hello函数
- t.join(); // 等待线程t执行完毕
- return 0;
- }
复制代码 这里std::thread t(hello)就是利用函数指针hello创建线程,新线程会实行hello函数里的代码。
join方法:
用于壅闭当火线程,直到被调用的线程实行完成。比如在上面的main函数中,t.join()会让main线程停息,等候t线程把hello函数实行完后再继续往下实行。
detach方法:
将线程分离,使得线程在配景独立运行,不再与创建它的std::thread对象关联。今后,无法再通过该std::thread对象对这个线程举行控制(比如不能再调用join了)。示例:- #include <iostream>
- #include <thread>
- void func() {
- // 线程执行的函数内容
- std::cout << "Thread is running independently." << std::endl;
- }
- int main() {
- std::thread t(func);
- t.detach(); // 分离线程t
- // 主线程继续执行其他操作,不用等待t线程结束
- std::cout << "Main thread continues." << std::endl;
- return 0;
- }
复制代码 线程传参
向线程函数通报参数时,必要包管参数在通报时是有效的,且在被调用函数实行期间一连有效(比如克制传引用指向临时对象等情况)。比方:- #include <iostream>
- #include <thread>
- void print_num(int num) {
- std::cout << "The number is: " << num << std::endl;
- }
- int main() {
- int num = 10;
- std::thread t(print_num, num); // 传递普通变量num作为参数
- t.join();
- return 0;
- }
复制代码 如果要通报类对象等更复杂的情况,要注意拷贝、移动语义等干系题目,确保参数通报的准确性。
线程 ID 获取
可以通过std::this_thread::get_id获取当火线程的线程 ID,大概通过std::thread对象的get_id成员函数获取对应的线程 ID,用于标识和区分差别线程。示例:- #include <iostream>
- #include <thread>
- void show_thread_id() {
- std::cout << "Thread ID: " << std::this_thread::get_id() << std::endl;
- }
- int main() {
- std::thread t(show_thread_id);
- std::cout << "Main thread ID: " << std::this_thread::get_id() << std::endl;
- t.join();
- return 0;
- }
复制代码 线程同步干系(比方互斥量等,用于办理多线程访问共享资源辩论题目)
std::mutex(互斥量):
通过lock和unlock方法对共享资源举行加锁息争锁,确保同一时候只有一个线程能访问被掩护的共享资源。比方:- #include <iostream>
- #include <thread>
- #include <mutex>
- std::mutex mtx;
- int shared_data = 0;
- void increment() {
- for (int i = 0; i < 1000; ++i) {
- mtx.lock(); // 加锁
- shared_data++;
- mtx.unlock(); // 解锁
- }
- }
- int main() {
- std::thread t1(increment);
- std::thread t2(increment);
- t1.join();
- t2.join();
- std::cout << "Shared data value: " << shared_data << std::endl;
- return 0;
- }
复制代码 也可以利用std::lock_guard等 RAII(Resource Acquisition Is Initialization)机制的类来更方便、安全地管理互斥量的生命周期,主动完成加锁息争锁,如:- #include <iostream>
- #include <thread>
- #include <mutex>
- std::mutex mtx;
- int shared_data = 0;
- void increment() {
- for (int i = 0; i < 1000; ++i) {
- std::lock_guard<std::mutex> guard(mtx); // 构造时加锁,析构时自动解锁
- shared_data++;
- }
- }
- int main() {
- std::thread t1(increment);
- std::thread t2(increment);
- t1.join();
- t2.join();
- std::cout << "Shared data value: " << shared_data << std::endl;
- return 0;
- }
复制代码 std::condition_variable(条件变量):
常和互斥量共同利用,用于线程间的同步,实现一个线程等候某个条件满足后再继续实行的功能。比如一个线程等候另一个线程修改共享资源到达某个条件后再举行后续利用,典范用法如下:- #include <iostream>
- #include <thread>
- #include <mutex>
- #include <condition_variable>
- std::mutex mtx;
- std::condition_variable cv;
- bool ready = false;
- void wait_for_signal() {
- std::unique_lock<std::mutex> lck(mtx);
- cv.wait(lck, []{ return ready; }); // 等待条件满足(ready为true)
- std::cout << "Received signal and continue." << std::endl;
- }
- void send_signal() {
- {
- std::lock_guard<std::mutex> lck(mtx);
- ready = true;
- }
- cv.notify_one(); // 通知等待在该条件变量上的一个线程
- }
- int main() {
- std::thread t1(wait_for_signal);
- std::thread t2(send_signal);
- t1.join();
- t2.join();
- return 0;
- }
复制代码 这些就是 C++ 中线程干系的一些告急接口方法及其根本利用方式,在实际多线程编程中,通常必要综合运用它们来实现高效、准确的并发步伐逻辑。
|