zoukankan      html  css  js  c++  java
  • NEU操作系统实验课4——线程同步

    实验要求:

    创建两个线程按数字顺序打印10以下自然数,其中一个线程打印1-3及8-10;另一个线程打印4-6。要求使用线程同步机制实现上述打印顺序。

    看网上的资料学习了两种写法

    C++11的,使用了std::thread

     1 #include <unistd.h>
     2 #include <thread>
     3 #include <iostream>
     4 #include <mutex>
     5 #include<bits/stdc++.h>
     6 void Fun_1();
     7 void Fun_2();
     8 unsigned int counter=0;
     9 std::mutex mtx;
    10 
    11 int main()
    12 {
    13     std::thread thrd_1(Fun_1);
    14     std::thread thrd_2(Fun_2);
    15     thrd_1.join();
    16     thrd_2.join();
    17     //system("pause");
    18     return 0;
    19 }
    20 
    21 void Fun_1()
    22 {
    23     while(counter<10){
    24         if(counter<3||counter>5)continue;
    25         std::lock_guard<std::mutex> mtx_locker(mtx);
    26         ++counter;
    27         std::cout << "Function 1 counting " << counter << "...
    ";
    28         usleep(1);
    29     }
    30 }
    31 
    32 void Fun_2()
    33 {
    34     while(counter<10){
    35         if(counter>2&&counter<6)continue;
    36         std::lock_guard<std::mutex> mtx_locker(mtx); 
    37         ++counter;
    38         std::cout << "Function 2 counting " << counter << "...
    ";
    39         usleep(1);
    40     }
    41 }
    C++11

    C语言的,使用pthread

     1 #include <stdio.h>
     2 #include <pthread.h>
     3 #include <stdlib.h>
     4 #include <unistd.h>
     5 
     6 pthread_mutex_t mutex;
     7 pthread_cond_t cond1;
     8 pthread_cond_t cond2;
     9 int cnt=0;
    10 
    11 
    12 void hander(void *arg)
    13 { 
    14     free(arg);
    15     (void)pthread_mutex_unlock(&mutex);
    16 }
    17 
    18 void *thread1(void *arg)
    19 {
    20     pthread_cleanup_push(hander,&mutex);
    21     printf("thread1
    1
    2
    3
    ");
    22     pthread_mutex_lock(&mutex);
    23     pthread_cond_wait(&cond1,&mutex);
    24     pthread_mutex_unlock(&mutex);
    25     printf("thread1
    7
    8
    9
    10
    ");
    26     pthread_cleanup_pop(0);
    27 }
    28 
    29 void *thread2(void *arg)
    30 {
    31     pthread_mutex_lock(&mutex);
    32     pthread_cond_wait(&cond2,&mutex);
    33     printf("thread2
    4
    5
    6
    ");
    34     pthread_cond_signal(&cond1);
    35     pthread_mutex_unlock(&mutex);
    36 }
    37 
    38 int main()
    39 {
    40     pthread_t tid1,tid2;
    41     pthread_mutex_init(&mutex,NULL);
    42     pthread_cond_init(&cond1,NULL);
    43     pthread_cond_init(&cond2,NULL);
    44     pthread_create(&tid1,NULL,thread1,NULL);
    45     pthread_create(&tid2,NULL,thread2,NULL);
    46     
    47     sleep(1);
    48     pthread_cond_signal(&cond2);
    49     //sleep(2);
    50     pthread_exit(0);
    51     return 0;
    52 }
    C
  • 相关阅读:
    xdebug安装教程
    如何查看Linux操作系统的位数
    getconf命令【一天一个命令】
    redis 数据类型详解 以及 redis适用场景场合
    Redis和Memcache对比及选择
    无交换机实现集群网络互联
    性能调优攻略
    Chrome 插件集推荐
    在 Linux 下将 PNG 和 JPG 批量互转的四种方法
    Flashback for MySQL 5.7
  • 原文地址:https://www.cnblogs.com/xuwangzihao/p/6674610.html
Copyright © 2011-2022 走看看