c++多线程
多线程其实非常简单
多线程是多任务处理的一种特殊形式,多任务处理允许让电脑同时运行两个或两个以上的程序。一般情况下,两种类型的多任务处理:基于进程和基于线程。
创建线程
| #include <pthread.h> |
| pthread_create (thread, attr, start_routine, arg) |
| |
| |
| |
| |
| |
| |
| |
终止线程
| #include <pthread.h> |
| pthread_exit (status) |
| |
| |
| |
| |
| |
| |
实例
| #include <iostream> |
| #include <cstdlib> |
| #include <pthread.h> |
| using namespace std; |
| #define NUM_THREADS 5 |
| void *PrintHello(void *threadid) |
| { |
| int tid = *((int*)threadid); |
| cout << "线程ID, " << tid << endl; |
| pthread_exit(NULL); |
| } |
| int main () |
| { |
| pthread_t threads[NUM_THREADS]; |
| int indexes[NUM_THREADS]; |
| int rc; |
| int i; |
| for( i=0; i < NUM_THREADS; i++ ){ |
| cout << " 创建线程, " << i << endl; |
| indexes[i] = i; |
| rc = pthread_create(&threads[i], NULL, PrintHello, (void *)&(indexes[i])); |
| if (rc){ |
| cout << "无法创建线程," << rc << endl; |
| exit(-1); |
| } |
| } |
| pthread_exit(NULL); |
| } |
向线程传递参数
| #include <iostream> |
| #include <cstdlib> |
| #include <pthread.h> |
| using namespace std; |
| #define NUM_THREADS 5 |
| struct thread_data{ |
| int thread_id; |
| char *message; |
| }; |
| void *PrintHello(void *threadarg) |
| { |
| struct thread_data *my_data; |
| my_data = (struct thread_data *) threadarg; |
| cout << "Thread ID : " << my_data->thread_id ; |
| cout << " Message : " << my_data->message << endl; |
| pthread_exit(NULL); |
| } |
| int main () |
| { |
| pthread_t threads[NUM_THREADS]; |
| struct thread_data td[NUM_THREADS]; |
| int rc; |
| int i; |
| for( i=0; i < NUM_THREADS; i++ ){ |
| cout <<"main() : creating thread, " << i << endl; |
| td[i].thread_id = i; |
| td[i].message = (char*)"This is message"; |
| rc = pthread_create(&threads[i], NULL, |
| PrintHello, (void *)&td[i]); |
| if (rc){ |
| cout << "Error:unable to create thread," << rc << endl; |
| exit(-1); |
| } |
| } |
| pthread_exit(NULL); |
| } |
这个实例演示了如何通过结构传递多个参数。
连接和分离线程
| |
| pthread_join (threadid, status) |
| pthread_detach (threadid) |
这个实例演示了如何使用 pthread_join() 函数来等待线程的完成。
| #include <iostream> |
| #include <cstdlib> |
| #include <pthread.h> |
| #include <unistd.h> |
| using namespace std; |
| #define NUM_THREADS 5 |
| void *wait(void *t) |
| { |
| int i; |
| long tid; |
| tid = (long)t; |
| sleep(1); |
| cout << "Sleeping in thread " << endl; |
| cout << "Thread with id : " << tid << " ...exiting " << endl; |
| pthread_exit(NULL); |
| } |
| int main () |
| { |
| int rc; |
| int i; |
| pthread_t threads[NUM_THREADS]; |
| pthread_attr_t attr; |
| void *status; |
| pthread_attr_init(&attr); |
| pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| for( i=0; i < NUM_THREADS; i++ ){ |
| cout << "main() : creating thread, " << i << endl; |
| rc = pthread_create(&threads[i], NULL, wait, (void *)&i ); |
| if (rc){ |
| cout << "Error:unable to create thread," << rc << endl; |
| exit(-1); |
| } |
| } |
| pthread_attr_destroy(&attr); |
| for( i=0; i < NUM_THREADS; i++ ){ |
| rc = pthread_join(threads[i], &status); |
| if (rc){ |
| cout << "Error:unable to join," << rc << endl; |
| exit(-1); |
| } |
| cout << "Main: completed thread id :" << i ; |
| cout << " exiting with status :" << status << endl; |
| } |
| |
| cout << "Main: program exiting." << endl; |
| pthread_exit(NULL); |
| } |
c++11后的标准的线程库
| #include <iostream> |
| |
| #include <thread> |
| |
| std::thread::id main_thread_id = std::this_thread::get_id(); |
| |
| void hello() |
| { |
| std::cout << "Hello Concurrent World\n"; |
| if (main_thread_id == std::this_thread::get_id()) |
| std::cout << "This is the main thread.\n"; |
| else |
| std::cout << "This is not the main thread.\n"; |
| } |
| |
| void pause_thread(int n) { |
| std::this_thread::sleep_for(std::chrono::seconds(n)); |
| std::cout << "pause of " << n << " seconds ended\n"; |
| } |
| |
| int main() { |
| std::thread t(hello); |
| std::cout << t.hardware_concurrency() << std::endl; |
| std::cout << "native_handle " << t.native_handle() << std::endl; |
| t.join(); |
| std::thread a(hello); |
| a.detach(); |
| std::thread threads[5]; |
| |
| std::cout << "Spawning 5 threads...\n"; |
| for (int i = 0; i < 5; ++i) |
| threads[i] = std::thread(pause_thread, i + 1); |
| std::cout << "完成生成线程,现在正等待加入:\n"; |
| for (auto &thread : threads) |
| thread.join(); |
| std::cout << "All threads joined!\n"; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?