美文网首页
2020-08-30 class mutex 互斥锁 (C++1

2020-08-30 class mutex 互斥锁 (C++1

作者: Wonton_skin | 来源:发表于2020-08-30 16:44 被阅读0次

// mutex::lock/unlock
#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex

std::mutex mtx;  // mutex for critical section

void  print_thread_id (int id)  {
    //critical section (exclusive access to std::cout signaled by locking mtx):
    mtx.lock();
    std::cout << "thread #" << id << '\n';
    mtx.unlock();
}

int main () {
    std::thread threads[10];   // spawn 10 threads:
    for (int i=0; i<10; ++i)
        threads[i] = std::thread(print_thread_id, i+1);

    for (auto& th : threads) th.join();
    return  0;
}


#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex

std::mutex mtx; // mutex for critical section
void print_block (int n,char c)
{
    // critical section (exclusive access to std::cout signaled by locking mtx):
    mtx.lock();
    for (int i = 0; i < n; ++i)  {  std::cout << c;  } 
    std::cout << '\n';
    mtx.unlock();
}

int main ()
{
     std::thread th1 (print_block,50,'*');
     std::thread th2 (print_block,50,'$');
     
     th1.join();
     th2.join();
     
    return 0;


#include <iostream> // std::cout
#include <thread>        // std::thread
#include <mutex>          // std::mutex, std::lock_guard
#include <stdexcept>      // std::logic_error

std::mutex mtx;

void print_even(int x)
{
    if (x % 2 == 0)
        std::cout << x << " is even\n";
    else
        throw (std::logic_error("not even"));
}

void print_thread_id(int id)
 {
    try {
        // using a local lock_guard to lock mtx guarantees unlocking on destruction / exception:
        std::lock_guard<std::mutex> lck(mtx);
        print_even(id);
    } catch (std::logic_error&) {
        std::cout << "[exception caught]\n";
    }
}

int main()
{
    std::thread threads[10];
    for (int i = 0; i < 10; ++i)  threads[i] = std::thread(print_thread_id, i + 1);

    for (auto& th : threads)   th.join();

return 0;
}

相关文章

  • 2020-08-30 class mutex 互斥锁 (C++1

    // mutex::lock/unlock#include // std::cout#includ...

  • 自旋锁 和 互斥锁

    自旋锁 和 互斥锁 Pthreads提供了多种锁机制: (1) Mutex(互斥量):pthread_mutex_...

  • 系统编程-------线程编程----线程互斥锁

    线程互斥锁 互斥锁pthread_mutex_t 参数: 返回值: pthread_mutex_inti;初始化互...

  • 进程间同步

    互斥锁 进程间也可以使用互斥锁同步, 但必须在pthread_mutex_init(&mutex, attr)之前...

  • Golang 锁的相关知识

    Golang锁分类:互斥锁(Mutex)、读写锁(RWMutex)。 互斥锁 在编写代码中引入了对象互斥锁的概念,...

  • 线程 -- 互斥锁

    互斥锁的使用步骤: # 创建锁mutex = threading.Lock()# 上锁mutex.acquire(...

  • 第二十六天--[互斥与同步]

    学习内容:互斥与同步收获: 了解了互斥与同步的概念; 了解了互斥锁(mutex)的使用:pthread_mutex...

  • golang rwmutex的实现原理

    1. 前言 前面我们聊了互斥锁Mutex,所谓读写锁RWMutex,完整的表述应该是读写互斥锁,可以说是Mutex...

  • C++锁

    锁的种类 互斥锁、条件锁、自旋锁、读写锁、递归锁。 互斥锁 头文件: 类型:pthread_mutex_t, 函数...

  • Go 语言的锁

    Go 语言提供两类锁: 互斥锁(Mutex)和读写锁(RWMutex)。其中读写锁(RWMutex)是基于互斥锁(...

网友评论

      本文标题:2020-08-30 class mutex 互斥锁 (C++1

      本文链接:https://www.haomeiwen.com/subject/uteesktx.html