Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a small program that runs 2 threads on the same critical section and uses mutex. The program works fine. But I would like to draw a UML, referably activity or state diagram, illustrating that the 2 threads runs the same critical section, not different parts of the codes. Please help me revise my UML.

Below is the source code:

#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();

Below is my UML. It definitely has some faults enter image description here

like image 593
Minh Pham Avatar asked Oct 04 '20 13:10

Minh Pham


People also ask

How do you represent a thread in UML?

Activity, Sequence, and State Diagrams are all correct ways of showing thread behavior. 1st: (To vs's comments) There are two sets of diagrams or modeling elements in UML, static structure, as you put it, and behavioral.

Which UML diagram is a structural diagram?

Structural Diagrams. Structural diagrams depict a static view or structure of a system. It is widely used in the documentation of software architecture. It embraces class diagrams, composite structure diagrams, component diagrams, deployment diagrams, object diagrams, and package diagrams.

Is flowchart a UML diagram?

An activity diagram is a UML diagram. A flowchart, on the other hand, is a graphical diagram that represents an algorithm.

Which diagram is used to illustrate a single thread?

All diagrams I see are used to illustrate a single thread. Update: The accepted answer was the best example I saw but it does leave a fair bit to be desired. I ended up illustrating the threads in separate sequence diagrams. I'm not sure if the sequence diagram necessarily works for multiple threads. Show activity on this post.

How do I draw a class diagram in UML?

From the libraries you just added, select the shape you want and drag it from the toolbox to the canvas. Model the process flow by drawing lines between shapes while adding text. Dive into this guide on how to draw a class diagram in UML for additional insight. In Lucidchart, it's easy to resize and style any element.

What is the best tool for UML flowchart?

Lucidchart is the ideal tool for creating any kind of UML flowchart, whether it’s an activity diagram, a use case diagram, or a component diagram. Lucidchart offers in-editor collaboration tools and instant web publishing so you can demonstrate the functionality of your system to others. Activity diagram for a banking system

What is LucidChart used for in UML?

It uses different container shapes for activities, decisions, and notes. Lucidchart is the ideal tool for creating any kind of UML flowchart, whether it’s an activity diagram, a use case diagram, or a component diagram.


1 Answers

It definitely has some faults

yes, for instance :

  • both join finish before the execution of hello, this is impossible
  • each join are executed in one the separated threads, this must be done by main
  • there is a fork but no join (the UML one)
  • after the action unlock the flow goes to t2.join which is false and produces a never ending loop

In a sequence diagram it is possible to use a combined fragment critical, but there is no specific notation in an activity to indicate a critical region.

A first possibility is to not try to indicate the critical section, to use call operation actions on the mutex to call lock and unlock, and the reader has to know what that means. You can also add a note to help the reader. For instance (the call to hello is replaced by its body and I replaced the loop by an opaque action to simplify) :

enter image description here

Or with the partition :

enter image description here

A second way is to use a combined fragment critical even this is not specified in the norm, hoping the reader understand ( may be with the help of a note ) :

enter image description here

Of course you can combine using the first way and also drawing the combined fragment critical hoping to help the reader :

enter image description here

A third way if you do not want to have actions corresponding to C++ is to emulate the critical region using accept/send signal action, but to be frank this is not really easy to read/understand :

enter image description here

or with the partition :

enter image description here

Of course it is also possible to do the first send signal action after the fork :

enter image description here

like image 122
bruno Avatar answered Sep 30 '22 07:09

bruno