I am trying syncronisation between two threads in a cpp script on CLING interpreter.
But, I see that, when I use mutex synchronisation using mtx.lock() and mtx.unlock() in the thread function and execute the scripts, the script terminates abruptly without getting completed.
Here is the script for reference that I am executing on CLING:
// C++ program to illustrate the race conditions
//#include <iostream>
#include <thread>
#include <mutex>
//#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
//using namespace std;
// Shared resource
int number = 0;
// Create object for mutex
mutex mtx;
// function to increment the number
void increment(){
// Lock the thread using lock
mtx.lock();
// increment number by 1 for 1000000 times
for(int i=0; i<1000000; i++){
number++;
}
// Release the lock using unlock()
mtx.unlock();
}
int main()
{
// Create thread t1 to perform increment()
FILE* fptr= fopen("C:\\Release_NEW\\ThreadOutput_50\\sum.txt", "a");
thread t1(increment);
// Create thread t2 to perform increment()
thread t2(increment);
// Start both threads simultaneously
t1.join();
t2.join();
// Print the number after the execution of both threads
//cout << "Number after execution of t1 and t2 is " << number;
fprintf(fptr, "%d", number);
fputs("\n", fptr);
fclose(fptr);
return 0;
}
I cannot reproduce your issue with ROOT 6.30.06.
This is a formatted and simplified version of the code you nicely provided [1].
To help @vvassilev and @bellenot , perhaps could you be more explicit about the ROOT (or cling) version you are using? I assume you are on windows.
Cheers,
Danilo
[1]
int number = 0;
// Create object for mutex
mutex mtx;
// function to increment the number
void increment()
{
// Lock the thread using lock
mtx.lock();
// increment number by 1 for 1000000 times
for (int i = 0; i < 1000000; i++) {
number++;
}
// Release the lock using unlock()
mtx.unlock();
}
int a()
{
// Create thread t1 to perform increment()
FILE *fptr = fopen("sum.txt", "a");
thread t1(increment);
// Create thread t2 to perform increment()
thread t2(increment);
// Start both threads simultaneously
t1.join();
t2.join();
// Print the number after the execution of both threads
// cout << "Number after execution of t1 and t2 is " << number;
fprintf(fptr, "%d", number);
fputs("\n", fptr);
fclose(fptr);
return 0;
}
No, no solution foreseen for now. It looks like an issue between the interpreter and the runtime libraries. Need some time to figure out what the real issue is.
#include <iostream>
#include <thread>
#include <mutex>
int number = 0;
// Create object for mutex
std::mutex mtx;
// function to increment the number
void increment()
{
// Lock the thread using lock
mtx.lock();
// increment number by 1 for 1000000 times
for (int i = 0; i < 1000000; i++) {
number++;
}
// Release the lock using unlock()
mtx.unlock();
}
int b()
{
// Create thread t1 to perform increment()
std::thread t1(increment);
// Create thread t2 to perform increment()
std::thread t2(increment);
// Start both threads simultaneously
t1.join();
t2.join();
// Print the number after the execution of both threads
std::cout << "Number after execution of t1 and t2 is " << number << std::endl;
return 0;
}
works just fine with cling, llvm16, x64:
C:\root-dev\rootdev\forum>..\..\install\x64\cling\bin\cling
****************** CLING ******************
* Type C++ code and press enter to run it *
* Type .q to exit *
*******************************************
[cling]$ .x b.C
Number after execution of t1 and t2 is 2000000
(int) 0
[cling]$
#include <iostream>
#include <thread>
#include <mutex>
#include <fstream>
int number = 0;
// Create object for mutex
std::mutex mtx;
// function to increment the number
void increment()
{
// Lock the thread using lock
mtx.lock();
// increment number by 1 for 1000000 times
for (int i = 0; i < 1000000; i++) {
number++;
}
// Release the lock using unlock()
mtx.unlock();
}
int a()
{
// Create thread t1 to perform increment()
std::thread t1(increment);
// Create thread t2 to perform increment()
std::thread t2(increment);
// Start both threads simultaneously
t1.join();
t2.join();
// Print the number after the execution of both threads
std::cout << "Number after execution of t1 and t2 is " << number << std::endl;
std::ofstream out;
out.open("sum.txt", std::ios_base::app);
out << number << std::endl;
out.close();
return 0;
}
It gives, after several executions:
C:\root-dev\rootdev\forum>..\..\install\x64\cling\bin\cling
****************** CLING ******************
* Type C++ code and press enter to run it *
* Type .q to exit *
*******************************************
[cling]$ .x a.C
Number after execution of t1 and t2 is 2000000
(int) 0
[cling]$ .!more sum.txt
2000000
2000000
2000000
2000000
[cling]$
Hi @bellenot ,
What version of cling version are you using here?
And, does it work fine on 32-bit CLING exe(x86) of the same version as well? Because, I need to use 32-bit CLING exe as all my DLL files are of 32 bit(x86 type)
C:\root-dev\rootdev\forum>..\..\install\x86\cling\bin\cling.exe
****************** CLING ******************
* Type C++ code and press enter to run it *
* Type .q to exit *
*******************************************
[cling]$ .x a.C
Number after execution of t1 and t2 is 2000000
(int) 0
[cling]$ .! more sum.txt
2000000
2000000
2000000
2000000
2000000
2000000
[cling]$
Thankyou for the response.
You are saying, you have built this working version of CLING from github and I have also built the CLING in a similar fashion by following the instructions at the below root forum page:
After building as per the above instructions, my CLING exe version is as below: cling.exe --version 1.0~dev
Can you please let me know if the build instructions which I have followed is correct?
If not, please let me know the instructions, from where I can build the latest CLING version( which can support mutex locking and unlocking and all the other advanced features of C++) for Windows?
Hi @bellenot ,
I followed the instruction like you specified( I used Visual Studio 16 2019 instead of Visual Studio 17 2022;and -A Win32 instead of -A x64). Please find the below accurate list of instructions that I followed:
Created a directory named CLING_Latest in C drive
C:\CLING_Latest
I tried with VS2022, its the same behvaiour of abrupt CLING ending.
The thing is, if I load mutex.cpp file directly without loading my project DLLs, then the mutex lock and unlock works fine.
If I load the .cpp file after my project DLLs, then I see the abrupt ending in CLING