C++ 线程编程

函数thread

thread* t = new thread(,);

mutex

#include 
#include 
#include 
#include 

std::mutex mtx;

void printMultipleChar(int times, char c);

int main(_In_ int argc, _In_reads_(argc) _Pre_z_ char** argv, _In_z_ char** envp) {
    std::cout << "This is showing mutex work in thread" << std::endl;
    std::thread t1(bind(printMultipleChar, std::placeholders::_1, std::placeholders::_2), 50, '$');
    std::thread t2(bind(printMultipleChar, std::placeholders::_1, std::placeholders::_2), 50, '3');
    t1.join();
    t2.join();
    getchar();
    return 0;
}

void printMultipleChar(int times, char c) {
    mtx.lock();
    for (int i = 0; i < times; i++)
    {
        std::cout << c << " ";
    }
    std::cout << std::endl;
    mtx.unlock();
}

你可能感兴趣的:(C++)