热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

发现多线程间内存冲突

发现多线程间内存冲突原文:https://www.geeks

发现多线程间内存冲突

原文:https://www . geeksforgeeks . org/find-memory-multi-threads 冲突/

考虑一个按块组织的内存。系统上运行着多个进程。每个应用程序都会获得以下信息。

(线程 T,内存块 M,时间 T,读/写),它本质上告诉线程 T 在时间 T 正在使用内存块 M,操作可以是读或写。

内存冲突定义为–
–同一位置的多个读取操作不会导致冲突。
–在 x+5 到 x-5 到位置 M 之间的一次写操作,将是线程在时间 x 访问位置 M 的冲突原因,其中 x 是标准时间测量单位中的某个时间。
–示例–如果线程 T1 在时间 x+1 访问了内存位置 M,并且如果线程 T2 在时间 x+6 之前访问了位置 M,那么 T1 和 T2 是冲突的候选对象,因为其中一个线程执行了写操作。

给你一个访问内存位置的线程列表,你必须找到所有的冲突。

例子

Input:
(1, 512, 1, R)
(2, 432, 2, W)
(3, 512, 3, R)
(4, 932, 4, R)
(5, 512, 5, W)
(6, 932, 6, R)
(7, 835, 7, R)
(8, 432, 8, R)
Output:
Thread 1 & 3 conflict with thread 5
All other operations are safe.

我们强烈建议你尽量减少浏览器,先自己试试这个。
这个想法是按内存块对所有线程进行排序,如果内存块相同,则按时间排序。一旦我们对所有线程进行了排序,我们就可以逐个遍历所有线程。对于每个被遍历的线程,我们只需要检查同一个块的先前相邻线程,因为线程是按时间排序的。

下面是这个想法的 C++实现。

// C++ program to find memory conflicts among threads
#include<bits/stdc++.h>
using namespace std;
/* Structure for details of thread */
struct Thread
{
    int id, memblck, time;
    char access;
};
/* Compare function needed for sorting threads
   We first sort threads on the basis of memory block,
   If they are same, then we sort on the basis of time */
bool compare(const Thread& x, const Thread& y)
{
    if (x.memblck == y.memblck)
        return x.time < y.time;
    else return x.memblck < y.memblck;
}
// Function to print all conflicts among threads
void printConflicts(Thread arr[], int n)
{
    /* Sort the threads first by memblock, then by
       time */
    sort(arr, arr+n, compare);
    /*start from second thread till last thread*/
    for (int i = 1; i < n; i++)
    {
        /* As threads are sorted, We check further
           for conflict possibility only if there
           memblock is same*/
        if(arr[i].memblck == arr[i-1].memblck)
        {
            /* As threads with same block are sorted in increasing order
               of access time. So we check possibility conflict from last
               thread to all previous threads which access the same block
               of memory such that the current thread access under the
               arr[i-1].time + 5.. and if any of them does read operation
               than conflict occurs. at any point memblock becomes different
               or current thread moves out of vulnerable time of latest
               previous processed thread, the loop breaks.
            */
            if (arr[i].time <= arr[i-1].time+5)
            {
                int j = i-1; /* start with previous thread */
                // Find all previous conflicting threads
                while (arr[i].memblck == arr[j].memblck &&
                        arr[i].time <= arr[j].time+5 &&
                        j >= 0)
                {
                    if (arr[i].access == 'W' || arr[j].access == 'W')
                    {
                        cout << "threads " << arr[j].id << " and "
                             << arr[i].id << " conflict.\n";
                    }
                    j--;
                }
            }
        }
    }
    cout << "All other operations are same\n";
}
// Driver program to test above function
int main()
{
    Thread arr[] = { {1, 512, 1, 'R'}, {2, 432, 2, 'W'},
                     {3, 512, 3, 'R'}, {4, 932, 4, 'R'},
                     {5, 512, 5, 'W'}, {6, 932, 6, 'R'},
                     {7, 835, 7, 'R'}, {8, 432, 8, 'R'}
                   };
    int n = sizeof(arr)/sizeof(arr[0]);
    printConflicts(arr, n);
    return 0;
}

输出:

threads 3 and 5 conflict.
threads 1 and 5 conflict.
All other operations are same

时间复杂度:上面的解决方案使用排序对线程进行排序。排序可以在 O(nLogn)时间内完成。然后打印所有冲突。打印所有冲突需要 O(n + m)个时间,其中 m 是冲突数。所以整体时间复杂度为 O(nLogn + m)。

本文由高拉夫·阿赫瓦尔供稿。如果你发现任何不正确的地方,或者你想分享更多关于上面讨论的话题的信息,请写评论。


推荐阅读
  • Java 中 Writer flush()方法,示例 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 深入探讨CPU虚拟化与KVM内存管理
    本文详细介绍了现代服务器架构中的CPU虚拟化技术,包括SMP、NUMA和MPP三种多处理器结构,并深入探讨了KVM的内存虚拟化机制。通过对比不同架构的特点和应用场景,帮助读者理解如何选择最适合的架构以优化性能。 ... [详细]
  • 在多线程编程环境中,线程之间共享全局变量可能导致数据竞争和不一致性。为了解决这一问题,Linux提供了线程局部存储(TLS),使每个线程可以拥有独立的变量副本,确保线程间的数据隔离与安全。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文介绍如何使用 Python 将一个字符串按照指定的行和元素分隔符进行两次拆分,最终将字符串转换为矩阵形式。通过两种不同的方法实现这一功能:一种是使用循环与 split() 方法,另一种是利用列表推导式。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • 本文探讨了在Java多线程环境下,如何确保具有相同key值的线程能够互斥执行并按顺序输出结果。通过优化代码结构和使用线程安全的数据结构,我们解决了线程同步问题,并实现了预期的并发行为。 ... [详细]
  • 本文总结了Java程序设计第一周的学习内容,涵盖语言基础、编译解释过程及基本数据类型等核心知识点。 ... [详细]
  • 作者:守望者1028链接:https:www.nowcoder.comdiscuss55353来源:牛客网面试高频题:校招过程中参考过牛客诸位大佬的面经,但是具体哪一块是参考谁的我 ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有