如何绘制 UML 来说明在关键部分工作的 2 个线程

How to draw UML to illustrate 2 threads working on a critical section

我有一个小程序在同一临界区运行 2 个线程并使用互斥。该程序运行良好。但我想画一个 UML,参考 activity 或状态图,说明 2 个线程运行相同的关键部分,而不是代码的不同部分。请帮我修改一下我的UML

下面是源代码:

#include <iostream>
#include <thread>
#include <mutex>
#include <stdio.h>
/* Global variables where both threads have access to*/
std::mutex myMutex;
int globalVariable = 1;
/* CRITICAL SECTION */
void hello()
{
    myMutex.lock();
    for (int counter =0 ; counter< 100; counter++){
        //std::lock_guard<std::mutex> lock(myMutex);
        printf("%d ",std::this_thread::get_id() );      //print the id of the thread that executes the for loop
        globalVariable++;
    }
    printf("Thread with id = %d runs. Counter is %d \n", std::this_thread::get_id(), msg) ; //print the result counter value and thread id that finishes the for loop
    myMutex.unlock();
}
int main()
{
    std::thread t1(hello);
    std::thread t2(hello);
    t1.join();
    t2.join();

下面是我的UML。它肯定有一些缺点

It definitely has some faults

是的,例如:

  • 两个join完成before执行hello,这是不可能的
  • 每个 join 都在一个单独的线程中执行,这必须由 main
  • 完成
  • 有一个 fork 但没有 join(UML 一个)
  • 在操作 解锁 之后,流程转到 t2.join,这是错误的并产生一个永无止境的循环

在序列图中可以使用组合片段临界区,但activity中没有特定的符号来指示临界区。

第一种可能性是不尝试指示临界区,使用 call operation actions 在互斥锁上调用 lockunlock,reader必须知道那是什么意思。您还可以添加注释以帮助reader。例如(对 hello 的调用被它的主体替换,我用一个不透明的操作替换了循环以简化):

或分区:

第二种方法是使用组合片段关键即使这在规范中没有指定,希望reader理解(可能需要a的帮助注意 ) :

当然可以用第一种方式组合,也可以画出组合片段critical希望对reader:

有帮助

第三种方法,如果你不想有对应于 C++ 的动作,是使用 accept/send 信号动作来模拟临界区,但坦率地说,这并不容易 read/understand :

或分区:

当然也可以在fork之后做第一个send signal动作: