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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线程的编程

發(fā)布時間:2023/12/13 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线程的编程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

完整代碼?

#include <sys/shm.h> #include <iostream> #include <unistd.h> #include <pthread.h>void * child1(void *arg){pthread_t tid = pthread_self();printf("1 thread %lu \n",tid);}int main(int argc,char* argv[]) {int result{};pthread_t a_thread{};pthread_t tid = pthread_self();printf("mean thread %lu \n",tid);result = pthread_create(&a_thread, nullptr,child1, nullptr);if (result !=0){printf("Thread create failed!");}return 0; }

  • 如果主線程 不延時,直接退出,子線程還未創(chuàng)建就結(jié)束了

線程終止

  • pthread_join()? ? 等待線程退出,第二個參數(shù)是線程退出返回的數(shù)值
#include <sys/shm.h> #include <iostream> #include <unistd.h> #include <pthread.h> #include <cstring>void * child1(void *arg){pthread_t tid = pthread_self();printf("1 thread %lu \n",tid);}void * thread_function(void *arg);int main(int argc,char* argv[]) {int result{};std::string message{"Hello World"};pthread_t a_thread{};pthread_t tid = pthread_self();printf("mean thread %lu \n",tid);result = pthread_create(&a_thread, nullptr,thread_function, (void *)(message.c_str()));if (result !=0){printf("Thread create failed!");exit(EXIT_FAILURE);}printf("Waiting for thread to finish!\n");void * thread_result;result = pthread_join(a_thread,&thread_result);if (result !=0){printf("Thread join failed!");exit(EXIT_FAILURE);}printf("Thread joined,it returned %s\n",(char *)thread_result);printf("Message is now %s \n",message.c_str());exit(EXIT_SUCCESS);return 0; }void * thread_function(void *arg){char * message = (char *)arg;printf("thread_function is running.Argument was %s\n",message);sleep(3);strcpy(message,"Bye!");pthread_exit((void *) "Thank you for the cpu time!"); }

  • 多線程 全局變量? 共享;注意同步 和 互斥

代碼 生產(chǎn)者 消費者模型

#include <sys/shm.h> #include <iostream> #include <unistd.h> #include <pthread.h> #include <cstring>#define BUFFER_SIZE 16 #define OVER -1struct prodcons {int buffer[BUFFER_SIZE];//設(shè)置緩沖區(qū)數(shù)組pthread_mutex_t lock;//互斥鎖int read_pos,write_pos;//讀寫位置pthread_cond_t not_empty;//緩沖區(qū)非空信號pthread_cond_t not_full;//緩沖區(qū)非滿信號 };struct prodcons buffer;void init(struct prodcons *b){pthread_mutex_init(&b->lock, nullptr);pthread_cond_init(&b->not_empty, nullptr);pthread_cond_init(&b->not_full, nullptr);b->read_pos = 0;b->write_pos = 0; }void put(struct prodcons *b,int data){pthread_mutex_lock(&b->lock);//等待緩沖區(qū)非滿while ((b->write_pos + 1) % BUFFER_SIZE == b->read_pos ){printf("wait for not full!\n");pthread_cond_wait(&b->not_full,&b->lock);}//寫數(shù)據(jù)并且指針前移b->buffer[b->write_pos] = data;b->write_pos++;if (b->write_pos >= BUFFER_SIZE)b->write_pos = 0;//設(shè)置緩沖區(qū)非空的信號pthread_cond_signal(&b->not_empty);pthread_mutex_unlock(&b->lock); }int get(struct prodcons *b){int data;pthread_mutex_lock(&b->lock);//等待緩沖區(qū)非空while (b->write_pos == b->read_pos){printf("wait for not empty!");pthread_cond_wait(&b->not_empty,&b->lock);}//讀數(shù)據(jù)并且指針前移data = b->buffer[b->read_pos];b->read_pos++;if (b->read_pos >= BUFFER_SIZE)b->read_pos = 0;//設(shè)置緩沖區(qū)非滿的信號pthread_cond_signal(&b->not_full);pthread_mutex_unlock(&b->lock);return data; }void * producer(void *data){for (int i = 0; i < 10; ++i) {printf("put --> %d\n",i);put(&buffer,i);}put(&buffer,OVER);printf("producer stopped!\n");return nullptr; }void * consumer(void *data){int d{};while (1){d = get(&buffer);if (d == OVER)break;printf("%d->get\n",d);}printf("consumer stopped!");return nullptr; }int main(int argc,char* argv[]) {pthread_t th_a{},th_b{};void *ret_val;struct prodcons x{};init(&x);pthread_create(&th_a, nullptr, reinterpret_cast<void *(*)(void *)>(producer), 0);pthread_create(&th_b, nullptr,reinterpret_cast<void *(*)(void *)>(consumer), 0);//等待生產(chǎn)者和消費者結(jié)束pthread_join(th_a,&ret_val);pthread_join(th_b,&ret_val);return 0; }

參考鏈接

  • 線程相關(guān)函數(shù)(2)-pthread_self()獲取調(diào)用線程ID
  • pthread_join的使用
  • pthread_cond_wait()用法分析

總結(jié)

以上是生活随笔為你收集整理的线程的编程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。