热门标签 | 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)。

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


推荐阅读
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文介绍如何使用 Python 将一个字符串按照指定的行和元素分隔符进行两次拆分,最终将字符串转换为矩阵形式。通过两种不同的方法实现这一功能:一种是使用循环与 split() 方法,另一种是利用列表推导式。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • Java 中 Writer flush()方法,示例 ... [详细]
  • python的交互模式怎么输出名文汉字[python常见问题]
    在命令行模式下敲命令python,就看到类似如下的一堆文本输出,然后就进入到Python交互模式,它的提示符是>>>,此时我们可以使用print() ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • This document outlines the recommended naming conventions for HTML attributes in Fast Components, focusing on readability and consistency with existing standards. ... [详细]
  • Splay Tree 区间操作优化
    本文详细介绍了使用Splay Tree进行区间操作的实现方法,包括插入、删除、修改、翻转和求和等操作。通过这些操作,可以高效地处理动态序列问题,并且代码实现具有一定的挑战性,有助于编程能力的提升。 ... [详细]
  • 本文讨论了如何根据特定条件动态显示或隐藏文件上传控件中的默认文本(如“未选择文件”)。通过结合CSS和JavaScript,可以实现更灵活的用户界面。 ... [详细]
  • 本文详细介绍了IBM DB2数据库在大型应用系统中的应用,强调其卓越的可扩展性和多环境支持能力。文章深入分析了DB2在数据利用性、完整性、安全性和恢复性方面的优势,并提供了优化建议以提升其在不同规模应用程序中的表现。 ... [详细]
  • Docker的安全基准
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • QUIC协议:快速UDP互联网连接
    QUIC(Quick UDP Internet Connections)是谷歌开发的一种旨在提高网络性能和安全性的传输层协议。它基于UDP,并结合了TLS级别的安全性,提供了更高效、更可靠的互联网通信方式。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
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社区 版权所有