鱼C论坛

 找回密码
 立即注册
查看: 405|回复: 3

[已解决]互斥锁相关编程

[复制链接]
发表于 2023-8-21 17:54:25 | 显示全部楼层 |阅读模式
5鱼币
利用互斥锁pthread_mutex_t编写互斥访问临界资源的多线程程序。程序完成的功能要求:主函数初始化共享内存变量mv(初值为10),创建互斥锁,创建两个线程并等待两个线程执行完毕,之后输出mv的值并释放互斥锁;两个线程分别实现通过获取互斥锁的方式完成对内存变量mv自加10次和自减5次的功能。
最佳答案
2023-8-21 17:54:26
下面是一个使用互斥锁pthread_mutex_t编写的多线程程序,实现了对临界资源的互斥访问:

  1. #include <stdio.h>
  2. #include <pthread.h>

  3. int mv = 10;  // 共享内存变量
  4. pthread_mutex_t lock;  // 互斥锁

  5. void* increment(void* arg) {
  6.     for (int i = 0; i < 10; i++) {
  7.         pthread_mutex_lock(&lock);  // 获取互斥锁
  8.         mv += 1;  // 自加
  9.         pthread_mutex_unlock(&lock);  // 释放互斥锁
  10.     }
  11.     return NULL;
  12. }

  13. void* decrement(void* arg) {
  14.     for (int i = 0; i < 5; i++) {
  15.         pthread_mutex_lock(&lock);  // 获取互斥锁
  16.         mv -= 1;  // 自减
  17.         pthread_mutex_unlock(&lock);  // 释放互斥锁
  18.     }
  19.     return NULL;
  20. }

  21. int main() {
  22.     pthread_mutex_init(&lock, NULL);  // 初始化互斥锁

  23.     pthread_t thread1, thread2;
  24.     pthread_create(&thread1, NULL, increment, NULL);  // 创建线程1
  25.     pthread_create(&thread2, NULL, decrement, NULL);  // 创建线程2

  26.     pthread_join(thread1, NULL);  // 等待线程1执行完毕
  27.     pthread_join(thread2, NULL);  // 等待线程2执行完毕

  28.     printf("mv的值为:%d\n", mv);  // 输出mv的值

  29.     pthread_mutex_destroy(&lock);  // 销毁互斥锁

  30.     return 0;
  31. }
复制代码


在主函数中,我们首先初始化共享内存变量mv为10,并创建互斥锁lock。然后,我们创建两个线程,一个线程执行increment函数,另一个线程执行decrement函数。在这两个函数中,我们使用pthread_mutex_lock函数获取互斥锁,然后对共享内存变量mv进行自加或自减操作,最后使用pthread_mutex_unlock函数释放互斥锁。主函数通过调用pthread_join函数等待两个线程执行完毕,然后输出mv的值,并使用pthread_mutex_destroy函数销毁互斥锁。

这样,我们就实现了对临界资源mv的互斥访问,保证了线程之间的正确执行顺序和数据的一致性。

最佳答案

查看完整内容

下面是一个使用互斥锁pthread_mutex_t编写的多线程程序,实现了对临界资源的互斥访问: 在主函数中,我们首先初始化共享内存变量mv为10,并创建互斥锁lock。然后,我们创建两个线程,一个线程执行increment函数,另一个线程执行decrement函数。在这两个函数中,我们使用pthread_mutex_lock函数获取互斥锁,然后对共享内存变量mv进行自加或自减操作,最后使用pthread_mutex_unlock函数释放互斥锁。主函数通过调用pthread_joi ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-21 17:54:26 | 显示全部楼层    本楼为最佳答案   
下面是一个使用互斥锁pthread_mutex_t编写的多线程程序,实现了对临界资源的互斥访问:

  1. #include <stdio.h>
  2. #include <pthread.h>

  3. int mv = 10;  // 共享内存变量
  4. pthread_mutex_t lock;  // 互斥锁

  5. void* increment(void* arg) {
  6.     for (int i = 0; i < 10; i++) {
  7.         pthread_mutex_lock(&lock);  // 获取互斥锁
  8.         mv += 1;  // 自加
  9.         pthread_mutex_unlock(&lock);  // 释放互斥锁
  10.     }
  11.     return NULL;
  12. }

  13. void* decrement(void* arg) {
  14.     for (int i = 0; i < 5; i++) {
  15.         pthread_mutex_lock(&lock);  // 获取互斥锁
  16.         mv -= 1;  // 自减
  17.         pthread_mutex_unlock(&lock);  // 释放互斥锁
  18.     }
  19.     return NULL;
  20. }

  21. int main() {
  22.     pthread_mutex_init(&lock, NULL);  // 初始化互斥锁

  23.     pthread_t thread1, thread2;
  24.     pthread_create(&thread1, NULL, increment, NULL);  // 创建线程1
  25.     pthread_create(&thread2, NULL, decrement, NULL);  // 创建线程2

  26.     pthread_join(thread1, NULL);  // 等待线程1执行完毕
  27.     pthread_join(thread2, NULL);  // 等待线程2执行完毕

  28.     printf("mv的值为:%d\n", mv);  // 输出mv的值

  29.     pthread_mutex_destroy(&lock);  // 销毁互斥锁

  30.     return 0;
  31. }
复制代码


在主函数中,我们首先初始化共享内存变量mv为10,并创建互斥锁lock。然后,我们创建两个线程,一个线程执行increment函数,另一个线程执行decrement函数。在这两个函数中,我们使用pthread_mutex_lock函数获取互斥锁,然后对共享内存变量mv进行自加或自减操作,最后使用pthread_mutex_unlock函数释放互斥锁。主函数通过调用pthread_join函数等待两个线程执行完毕,然后输出mv的值,并使用pthread_mutex_destroy函数销毁互斥锁。

这样,我们就实现了对临界资源mv的互斥访问,保证了线程之间的正确执行顺序和数据的一致性。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-21 17:55:13 | 显示全部楼层
下面是一个基于Linux系统的C程序,利用互斥锁pthread_mutex_t实现对共享内存变量的互斥访问。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. int mv = 10;                    // 共享内存变量
  5. pthread_mutex_t mutex;          // 互斥锁
  6. void* thread_increment(void* arg) {
  7.     int i;
  8.     for (i = 0; i < 10; i++) {
  9.         pthread_mutex_lock(&mutex);            // 加锁
  10.         mv += 10;                              // 自加操作
  11.         pthread_mutex_unlock(&mutex);          // 解锁
  12.     }
  13.     pthread_exit(NULL);
  14. }
  15. void* thread_decrement(void* arg) {
  16.     int i;
  17.     for (i = 0; i < 5; i++) {
  18.         pthread_mutex_lock(&mutex);            // 加锁
  19.         mv -= 5;                               // 自减操作
  20.         pthread_mutex_unlock(&mutex);          // 解锁
  21.     }
  22.     pthread_exit(NULL);
  23. }
  24. int main() {
  25.     pthread_t tid_increment, tid_decrement;
  26.     pthread_mutex_init(&mutex, NULL);           // 初始化互斥锁
  27.     pthread_create(&tid_increment, NULL, thread_increment, NULL);    // 创建自增线程
  28.     pthread_create(&tid_decrement, NULL, thread_decrement, NULL);    // 创建自减线程
  29.     pthread_join(tid_increment, NULL);          // 等待自增线程执行完毕
  30.     pthread_join(tid_decrement, NULL);          // 等待自减线程执行完毕
  31.     pthread_mutex_destroy(&mutex);               // 销毁互斥锁
  32.     printf("mv = %d\n", mv);                      // 输出mv的值
  33.     return 0;
  34. }
复制代码

以上代码首先定义了一个全局变量mv作为共享内存变量,初始值为10。然后定义了一个互斥锁mutex。接着,实现了两个线程函数thread_increment()thread_decrement(),分别完成对共享内存变量mv的自加和自减操作。在这两个线程函数中,使用pthread_mutex_lock()函数获取互斥锁,执行临界区代码,然后使用pthread_mutex_unlock()函数释放互斥锁。

在主函数中,首先初始化互斥锁mutex,然后使用pthread_create()函数创建两个线程,分别执行自增和自减操作。然后使用pthread_join()函数等待两个线程执行完毕。最后,使用pthread_mutex_destroy()函数销毁互斥锁并输出共享内存变量mv的值。

希望以上代码能够满足您的需求。如果有任何疑问,请随时提出。

如果回答对你有帮助,请给我一个最佳答案!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-21 17:55:30 | 显示全部楼层
以下是使用互斥锁 pthread_mutex_t 编写的多线程程序,以实现对共享内存变量 mv 的互斥访问:

  1. #include <stdio.h>
  2. #include <pthread.h>

  3. int mv = 10;
  4. pthread_mutex_t mutex;

  5. void* increment(void* arg) {
  6.     int i;
  7.     for (i = 0; i < 10; ++i) {
  8.         pthread_mutex_lock(&mutex); // 获取互斥锁
  9.         mv += 1;
  10.         pthread_mutex_unlock(&mutex); // 释放互斥锁
  11.     }
  12.     pthread_exit(NULL);
  13. }

  14. void* decrement(void* arg) {
  15.     int i;
  16.     for (i = 0; i < 5; ++i) {
  17.         pthread_mutex_lock(&mutex); // 获取互斥锁
  18.         mv -= 1;
  19.         pthread_mutex_unlock(&mutex); // 释放互斥锁
  20.     }
  21.     pthread_exit(NULL);
  22. }

  23. int main() {
  24.     pthread_t thread1, thread2;

  25.     pthread_mutex_init(&mutex, NULL); // 初始化互斥锁

  26.     // 创建线程
  27.     if (pthread_create(&thread1, NULL, increment, NULL) != 0) {
  28.         perror("pthread_create failed");
  29.         return 1;
  30.     }
  31.     if (pthread_create(&thread2, NULL, decrement, NULL) != 0) {
  32.         perror("pthread_create failed");
  33.         return 1;
  34.     }

  35.     // 等待线程执行完毕
  36.     if (pthread_join(thread1, NULL) != 0) {
  37.         perror("pthread_join failed");
  38.         return 1;
  39.     }
  40.     if (pthread_join(thread2, NULL) != 0) {
  41.         perror("pthread_join failed");
  42.         return 1;
  43.     }

  44.     printf("mv = %d\n", mv);

  45.     pthread_mutex_destroy(&mutex); // 销毁互斥锁

  46.     return 0;
  47. }
复制代码


主函数初始化了共享内存变量 mv 的初值为10,然后创建了互斥锁 mutex。接着,创建了两个线程 thread1thread2,分别调用 incrementdecrement 函数来实现对 mv 的自加和自减操作,每个线程执行10次和5次。在每个操作之前,线程会使用 pthread_mutex_lock 函数来获取互斥锁,以确保只有一个线程可以访问临界资源 mv。每个操作完成后,线程会使用 pthread_mutex_unlock 函数释放互斥锁。

主函数等待两个线程执行完毕,并输出最终的 mv 值。最后,主函数使用 pthread_mutex_destroy 函数销毁互斥锁。

这样,通过互斥锁的机制,可以确保在任意时刻只有一个线程可以访问临界资源 mv,从而实现了对共享内存的互斥访问。

希望以上代码能满足您的需求!如有任何问题,请随时提问。
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 13:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表