日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++基础学习(12)--(多线程、Web编程)

發布時間:2023/12/13 c/c++ 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++基础学习(12)--(多线程、Web编程) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 目錄
    • 1.多線程
    • 2.web編程

目錄

1.多線程


#include <iostream> // 必須的頭文件 #include <pthread.h>using namespace std;#define NUM_THREADS 5// 線程的運行函數 void* say_hello(void* args) {cout << "Hello Runoob!" << endl;return 0; }int main() {// 定義線程的 id 變量,多個變量使用數組pthread_t tids[NUM_THREADS];for(int i = 0; i < NUM_THREADS; ++i){//參數依次是:創建的線程id,線程參數,調用的函數,傳入的函數參數int ret = pthread_create(&tids[i], NULL, say_hello, NULL);if (ret != 0){cout << "pthread_create error: error_code=" << ret << endl;}}//等各個線程退出后,進程才結束,否則進程強制結束了,線程可能還沒反應過來;pthread_exit(NULL); }


#include <iostream> #include <cstdlib> #include <pthread.h>using namespace std;#define NUM_THREADS 5struct 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); }


#include <iostream> #include <cstdlib> #include <pthread.h> #include <unistd.h>using namespace std;#define NUM_THREADS 5void *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;// 初始化并設置線程為可連接的(joinable)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); }

當上面的代碼被編譯和執行時,它會產生下列結果:
main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
main() : creating thread, 4
Sleeping in thread
Thread with id : 4 …exiting
Sleeping in thread
Thread with id : 3 …exiting
Sleeping in thread
Thread with id : 2 …exiting
Sleeping in thread
Thread with id : 1 …exiting
Sleeping in thread
Thread with id : 0 …exiting
Main: completed thread id :0 exiting with status :0
Main: completed thread id :1 exiting with status :0
Main: completed thread id :2 exiting with status :0
Main: completed thread id :3 exiting with status :0
Main: completed thread id :4 exiting with status :0
Main: program exiting.

#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";elsestd::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); // move-assign threadsstd::cout << "Done spawning threads. Now waiting for them to join:\n";for (auto &thread : threads)thread.join();std::cout << "All threads joined!\n"; }

2.web編程



總結

以上是生活随笔為你收集整理的c++基础学习(12)--(多线程、Web编程)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。