I have a multi-threaded(3 threads) C++ application to create three files in a single location. The application gets a start and stop command as input(say a user input at runtime). For each start-stop pair, all three threads should create files in a same folder.
I am using a single shell script to create file paths for each file generated by each of the three threads. There is a shell script which is getting called from within each thread. The shell script reads a number maintained in a common file called "count" and creates a string(filepath) using this count, with the count number appended to the directory to signify a new set. The number inside the "count" file should be updated only by the first thread that receives the update flag or "start_flag" from user input
The shell script is run inside the C++ file as follows:
Global variable to capture the start file update:
bool start_flag = false;
Inside each of the threads, there is something like:
if(true == start_flag) /*one of the threads that first receives the start signal*/
system("./folderpath.sh"); /*This will create a new string by incrementing count and update the file path in a file which will be later read*/
else
system("./folderpath.sh"); /*This will create a string using teh same count previously updated by another thread */
Inside the shell script, I am creating a text string and writing it into a file called Filepath.txt. I read this file to finally fopen a file in the generated path
I want to ensure that correct strings are updated in each of the Filepath.txt and read the content updated in the Filepath.txt by each of the threads without any issues
I observe that I am not getting what is expected.
How do I fix this?