{"id":552,"date":"2023-09-16T15:48:01","date_gmt":"2023-09-16T07:48:01","guid":{"rendered":""},"modified":"2023-09-17T00:42:12","modified_gmt":"2023-09-16T16:42:12","slug":"c-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%90%8c%e6%ad%a5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/552.html","title":{"rendered":"C++ \u591a\u7ebf\u7a0b\u540c\u6b65"},"content":{"rendered":"

\u5728C++\u4e2d\uff0c\u591a\u7ebf\u7a0b\u540c\u6b65\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u6027<\/strong><\/span>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u591a\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\uff1a<\/p>\n

\u4e92\u65a5\u9501\uff1a\u4e92\u65a5\u9501\u662f\u4e00\u79cd\u6700\u57fa\u672c\u7684\u540c\u6b65\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u4fdd\u8bc1\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b<\/strong><\/span>\u53ef\u4ee5\u8bbf\u95ee<\/strong><\/span>\u5171\u4eab\u8d44\u6e90\u3002\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u5f97\u4e86\u4e92\u65a5\u9501\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5c31\u5fc5\u987b\u7b49\u5f85\u8be5\u7ebf\u7a0b\u91ca\u653e\u9501\u540e\u624d\u80fd\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u3002<\/p>\n

\u6761\u4ef6\u53d8\u91cf\uff1a\u6761\u4ef6\u53d8\u91cf\u662f\u4e00\u79cd\u540c\u6b65\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u8ba9\u7ebf\u7a0b\u5728\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u65f6\u7b49\u5f85<\/strong><\/span>\uff0c\u800c\u4e0d\u662f\u5fd9\u7b49\u5f85\u3002\u6761\u4ef6\u53d8\u91cf\u901a\u5e38\u4e0e\u4e92\u65a5\u9501\u4e00\u8d77\u4f7f\u7528<\/strong><\/span>\uff0c\u5f53\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\uff0c\u7ebf\u7a0b\u4f1a\u91ca\u653e\u4e92\u65a5\u9501\u5e76\u7b49\u5f85\u6761\u4ef6\u53d8\u91cf\uff0c\u5f53\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c\u7ebf\u7a0b\u4f1a\u91cd\u65b0\u83b7\u5f97\u4e92\u65a5\u9501\u5e76\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n

\u4fe1\u53f7\u91cf\uff1a\u4fe1\u53f7\u91cf\u662f\u4e00\u79cd\u540c\u6b65\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u63a7\u5236\u591a\u4e2a\u7ebf\u7a0b\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee<\/span>\u3002\u4fe1\u53f7\u91cf\u6709\u4e00\u4e2a\u8ba1\u6570\u5668<\/strong><\/span>\uff0c\u5f53\u8ba1\u6570\u5668\u4e3a0<\/strong><\/span>\u65f6\uff0c\u7ebf\u7a0b\u4f1a\u88ab\u963b\u585e<\/strong><\/span>\uff0c\u5f53\u8ba1\u6570\u5668\u5927\u4e8e0<\/strong><\/span>\u65f6\uff0c\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab<\/strong><\/span>\u8d44\u6e90\u5e76\u5c06\u8ba1\u6570\u5668\u51cf1<\/strong><\/span>\u3002<\/p>\n

\u5c4f\u969c\uff1a\u5c4f\u969c\u662f\u4e00\u79cd\u540c\u6b65\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u8ba9\u591a\u4e2a\u7ebf\u7a0b\u5728\u67d0\u4e2a\u70b9\u4e0a\u7b49\u5f85<\/strong><\/span>\uff0c\u76f4\u5230\u6240\u6709\u7ebf\u7a0b\u90fd\u5230\u8fbe\u8be5\u70b9\u540e\u624d\u7ee7\u7eed\u6267\u884c\u3002\u5c4f\u969c\u901a\u5e38\u7528\u4e8e\u5206\u9636\u6bb5\u7684\u4efb\u52a1\uff0c\u6bcf\u4e2a\u9636\u6bb5\u90fd\u9700\u8981\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210\u540e\u624d\u80fd\u8fdb\u5165\u4e0b\u4e00\u9636\u6bb5\u3002<\/p>\n<\/p>\n

\u4ee5\u4e0a\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u591a\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\uff0c\u4e0d\u540c\u7684\u573a\u666f\u9700\u8981\u9009\u62e9\u4e0d\u540c\u7684\u540c\u6b65\u673a\u5236\u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u548c\u6b63\u786e\u6027\u3002<\/p>\n

\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5e76\u5199\u51fa\u4f8b\u5b50<\/p>\n

1. \u4e92\u65a5\u9501\uff08mutex\uff09<\/strong><\/span><\/p>\n

\u4e92\u65a5\u9501\u662f\u4e00\u79cd\u6700\u57fa\u672c\u7684\u540c\u6b65\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u4fdd\u8bc1\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u3002\u5728C++\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528std::mutex\u6765\u5b9e\u73b0\u4e92\u65a5\u9501\u3002<\/p>\n

```c++
#include <iostream>
#include <thread>
#include <mutex><\/p>\n

std::mutex mtx;<\/p>\n

void print(int num) {
\n
    mtx.lock();
    std::cout << \"Thread \" << num << \" is printing.\" << std::endl;
    mtx.unlock();
}<\/p>\n

int main() {
\n
    std::thread t1(print, 1);
    std::thread t2(print, 2);
    t1.join();
    t2.join();
    return 0;
}
```<\/p>\n

2. \u6761\u4ef6\u53d8\u91cf\uff08condition variable\uff09<\/strong><\/span><\/p>\n

\u6761\u4ef6\u53d8\u91cf\u662f\u4e00\u79cd\u9ad8\u7ea7\u7684\u540c\u6b65\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u8ba9\u7ebf\u7a0b\u5728\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3\u65f6\u624d\u7ee7\u7eed\u6267\u884c\u3002\u5728C++\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528std::condition_variable\u6765\u5b9e\u73b0\u6761\u4ef6\u53d8\u91cf\u3002<\/p>\n

```c++
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable><\/p>\n

std::mutex mtx;
std::condition_variable cv;
bool ready = false;<\/p>\n

void print(int num) {
\n
    std::unique_lock<std::mutex> lock(mtx);
    while (!ready) {
\n
        cv.wait(lock);
    }
    std::cout << \"Thread \" << num << \" is printing.\" << std::endl;
}<\/p>\n

int main() {
\n
    std::thread t1(print, 1);
    std::thread t2(print, 2);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    ready = true;
    cv.notify_all();
    t1.join();
    t2.join();
    return 0;
}
```<\/p>\n

3. \u4fe1\u53f7\u91cf\uff08semaphore\uff09<\/strong><\/span><\/p>\n

\u4fe1\u53f7\u91cf\u662f\u4e00\u79cd\u5e38\u7528\u7684\u540c\u6b65\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u63a7\u5236\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u7684\u7ebf\u7a0b\u6570\u91cf\u3002\u5728C++\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528std::semaphore\u6765\u5b9e\u73b0\u4fe1\u53f7\u91cf\u3002<\/p>\n

```c++
#include <iostream>
#include <thread>
#include <semaphore.h><\/p>\n

sem_t sem;<\/p>\n

void print(int num) {
\n
    sem_wait(&sem);
    std::cout << \"Thread \" << num << \" is printing.\" << std::endl;
    sem_post(&sem);
}<\/p>\n

int main() {
\n
    sem_init(&sem, 0, 1);
    std::thread t1(print, 1);
    std::thread t2(print, 2);
    t1.join();
    t2.join();
    sem_destroy(&sem);
    return 0;
}
```<\/p>\n

4. \u5c4f\u969c\uff08barrier\uff09<\/strong><\/span><\/p>\n

\u5c4f\u969c\u662f\u4e00\u79cd\u540c\u6b65\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u8ba9\u591a\u4e2a\u7ebf\u7a0b\u5728\u67d0\u4e2a\u70b9\u4e0a\u7b49\u5f85\uff0c\u76f4\u5230\u6240\u6709\u7ebf\u7a0b\u90fd\u5230\u8fbe\u8fd9\u4e2a\u70b9\u540e\u624d\u7ee7\u7eed\u6267\u884c\u3002\u5728C++\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528std::barrier\u6765\u5b9e\u73b0\u5c4f\u969c\u3002<\/p>\n

```c++
#include <iostream>
#include <thread>
#include <barrier><\/p>\n

std::barrier bar(2);<\/p>\n

void print(int num) {
\n
    std::cout << \"Thread \" << num << \" is waiting.\" << std::endl;
    bar.wait();
    std::cout << \"Thread \" << num << \" is printing.\" << std::endl;
}<\/p>\n

int main() {
\n
    std::thread t1(print, 1);
    std::thread t2(print, 2);
    t1.join();
    t2.join();
    return 0;
}<\/p>\n","protected":false},"excerpt":{"rendered":"C++ \u591a\u7ebf\u7a0b\u540c\u6b65\u5728C++\u4e2d\uff0c\u591a\u7ebf\u7a0b\u540c\u6b65\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u6027_c++\u591a\u7ebf\u7a0b\u540c\u6b65","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/552"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=552"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/552\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}