做推广适合哪些网站,英文网站建设需要准备什么,网站建设分期进行怎么入账,郑州中原区网站建设文章目录 #xff08;二#xff09;线程的创建、分离和链接创建线程#xff1a;示例线程的分离#xff08;detach#xff09;和连接#xff08;join#xff09; #xff08;二#xff09;线程的创建、分离和链接
创建线程#xff1a;示例
线程#xff08;Thread二线程的创建、分离和链接创建线程示例线程的分离detach和连接join 二线程的创建、分离和链接
创建线程示例
线程Thread是并发执行的基本单位。在C中std::thread用于创建和管理线程。当你想在线程中执行某个有参数或无参数的函数时你可以将该函数传递给std::thread创建的线程对象以下是一些示例。
示例1传递无参数函数
#include iostream
#include threadvoid print_hello() {std::cout Hello from thread!\n;
}int main() {// 创建一个 std::thread 类型的对象 t并传递 print_hello作为t构造函数的参数。std::thread t(print_hello);t.join(); // 等待线程结束下一节介绍其作用return 0;
}示例2传递带参数函数拷贝
#include iostream
#include threadvoid print_number(int num) {std::cout Number: num \n;
}int main() {int value 42;std::thread t(print_number, value); // 创建线程并传入参数value 被拷贝给线程 t.join(); // 等待线程结束return 0;
}示例3传递带参数函数使用引用或指针
如果你希望在线程中修改传入的参数值可以用std::ref来包装参数以便在创建线程时传递其引用。
#include iostream
#include threadvoid modify_number(int num) {num * 2;std::cout Modified number: num \n;
}int main() {int value 42;std::thread t(modify_number, std::ref(value)); // 创建线程并传入参数的引用t.join(); // 等待线程结束std::cout Value in main: value \n; // 验证值已被修改return 0;
}Modified number: 84
Value in main: 84示例4传递多个参数
你可以传递多个参数给线程函数。
#include iostream
#include threadvoid print_info(std::string name, int age) {std::cout Name: name , Age: age \n;
}int main() {std::string name Alice;int age 30;std::thread t(print_info, name, age); // 创建线程并传入多个参数t.join(); // 等待线程结束return 0;
}示例5传递类的成员函数和实例
你还可以传递类的成员函数和类的实例给线程。
#include iostream
#include threadclass Person {
public:void say_hello(const std::string greeting) {std::cout greeting from name \n;}std::string name Bob;
};int main() {Person person;// 当线程 t 开始执行时它会调用 person 对象的 say_hello 函数并以 Hello 作为参数。std::thread t(Person::say_hello, person, Hello); // 创建线程并传入成员函数和对象指针t.join(); // 等待线程结束return 0;
}线程的分离detach和连接join
在C中线程的分离detach和连接join是用于管理线程生命周期的两种主要方式。当创建一个线程时你可以选择让它独立运行即分离或者等待它完成执行即连接。
分离detach
当你调用std::thread::detach()方法时你告诉线程库你不再关心这个线程的结束时间并且你不需要获取它的返回值。线程将在后台独立运行直到其函数返回结束。
示例
#include iostream
#include thread
#include chrono
#include ctime
#pragma warning(disable: 4996)
void thread_task() { // 阻塞当前线程的执行直到指定的时间间隔(2s)过去 std::this_thread::sleep_for(std::chrono::seconds(2)); auto now std::chrono::system_clock::now(); std::time_t now_c std::chrono::system_clock::to_time_t(now); std::cout Hello from detached thread at std::ctime(now_c) std::endl;
} int main() { auto start_time std::chrono::system_clock::now(); std::time_t start_time_c std::chrono::system_clock::to_time_t(start_time); std::cout Main thread starts at std::ctime(start_time_c) std::endl; std::thread t(thread_task); // 创建线程 t.detach(); // 分离线程 // 主线程将不会等待t的结束而是继续执行 std::cout Main thread continues execution without waiting for the detached thread.\n; // 让主线程休眠3s以便观察分离线程的输出 std::this_thread::sleep_for(std::chrono::seconds(3)); auto end_time std::chrono::system_clock::now(); std::time_t end_time_c std::chrono::system_clock::to_time_t(end_time); std::cout Main thread ends at std::ctime(end_time_c) std::endl; return 0;
}在这个例子中t线程将开始执行thread_task函数并在调用t.detach()后立即返回主线程。主线程将继续执行而不需要等待t线程的完成。
Main thread starts at Fri May 3 23:02:06 2024Main thread continues execution without waiting for the detached thread.
Hello from detached thread at Fri May 3 23:02:08 2024Main thread ends at Fri May 3 23:02:09 2024开始和结束相差3秒。
连接join
当你调用std::thread::join()方法时你告诉线程库你想要等待这个线程完成执行后再继续下去。调用join()的线程将被阻塞直到被连接的线程执行完毕。
示例
#include iostream
#include thread
#include chrono
#include ctime
#pragma warning(disable: 4996)
void thread_task() { // // 休眠2sstd::this_thread::sleep_for(std::chrono::seconds(2)); auto now std::chrono::system_clock::now(); std::time_t now_c std::chrono::system_clock::to_time_t(now); std::cout Hello from joined thread at std::ctime(now_c) std::endl;
} int main() { auto start_time std::chrono::system_clock::now(); std::time_t start_time_c std::chrono::system_clock::to_time_t(start_time); std::cout Main thread starts at std::ctime(start_time_c) std::endl; std::thread t(thread_task); // 创建线程 // 主线程将在这里阻塞等待t的完成 t.join(); std::cout Main thread continues execution after the thread is joined.\n; std::this_thread::sleep_for(std::chrono::seconds(3)); auto end_time std::chrono::system_clock::now(); std::time_t end_time_c std::chrono::system_clock::to_time_t(end_time); std::cout Main thread ends at std::ctime(end_time_c) std::endl; return 0;
}在这个例子中t线程将开始执行thread_task函数而主线程在调用t.join()时将阻塞等待t线程完成。当t线程完成后主线程将继续执行。
Main thread starts at Fri May 3 23:11:45 2024Hello from joined thread at Fri May 3 23:11:47 2024Main thread continues execution after the thread is joined.
Main thread ends at Fri May 3 23:11:50 2024开始和结束相差5秒。