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
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.
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.
An activity diagram is a UML diagram. A flowchart, on the other hand, is a graphical diagram that represents an algorithm.
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.
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.
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
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.
It definitely has some faults
yes, for instance :
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) :
Or with the partition :
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 ) :
Of course you can combine using the first way and also drawing the combined fragment critical hoping to help the reader :
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 :
or with the partition :
Of course it is also possible to do the first send signal action after the fork :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With