# Can mutex and its locking and unlocking features be used in CLING interpreter?

**URL:** <https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274>\
**Category:** Cling\
**Created:** [May 9, 2024, 11:10am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274 "2024-05-09T11:10:09Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![prabhu634](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@prabhu634](https://root-forum.cern.ch/u/prabhu634)\
**Post date:** [May 9, 2024, 11:10am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/1 "2024-05-09T11:10:09Z")

</div>

Hi @bellenot and @vvassilev ,

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:

```auto
// 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; 
}

```

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [May 9, 2024, 12:04pm UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/2 "2024-05-09T12:04:33Z")

</div>

Hi,

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]

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![prabhu634](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@prabhu634](https://root-forum.cern.ch/u/prabhu634)\
**Post date:** [May 10, 2024, 4:49am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/3 "2024-05-10T04:49:34Z")

</div>

Hi @Danilo ,  
Yes I am on windows,  
the cling version is as below;

> cling.exe --version  
> 1.0~dev

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 13, 2024, 9:22am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/4 "2024-05-13T09:22:26Z")

</div>

I can reproduce the issue on Windows. Investigating

---

<div class="post-metadata">

**Author:** ![prabhu634](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@prabhu634](https://root-forum.cern.ch/u/prabhu634)\
**Post date:** [May 15, 2024, 10:45am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/5 "2024-05-15T10:45:29Z")

</div>

Hi @bellenot ,

Any update related to this? Any possible solution here?

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 15, 2024, 10:47am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/6 "2024-05-15T10:47:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 15, 2024, 2:29pm UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/7 "2024-05-15T14:29:06Z")

</div>

So this code:

```auto
#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:

```cmd
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]$

```

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 15, 2024, 2:45pm UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/8 "2024-05-15T14:45:52Z")

</div>

And this works too:

```auto
#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:

```cmd
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]$

```

---

<div class="post-metadata">

**Author:** ![prabhu634](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@prabhu634](https://root-forum.cern.ch/u/prabhu634)\
**Post date:** [May 16, 2024, 6:28am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/9 "2024-05-16T06:28:37Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 16, 2024, 6:31am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/10 "2024-05-16T06:31:06Z")

</div>

I use the master branch of GitHub. And I didn’t try with 32 bit, I’ll try and let you know.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 16, 2024, 8:29am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/11 "2024-05-16T08:29:48Z")

</div>

> [@prabhu634](#):
>
> And, does it work fine on 32-bit CLING exe(x86) of the same version as well?

So yes, it works the same:

```cmd
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]$

```

---

<div class="post-metadata">

**Author:** ![prabhu634](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@prabhu634](https://root-forum.cern.ch/u/prabhu634)\
**Post date:** [May 17, 2024, 4:38am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/12 "2024-05-17T04:38:30Z")

</div>

Hi @bellenot ,

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:

> [@Is there a Windows Cling binary or how to compile it for Windows?](https://root-forum.cern.ch/t/is-there-a-windows-cling-binary-or-how-to-compile-it-for-windows/32993):
>
> Is there a precompiled Cling binary for Windows or how to compile it? Is it possible to compile Cling with VS2017? Can anyone provide detailed instructions? Why there is a problem with Cling in Windows, but not with Clang?

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?

---

<div class="post-metadata">

**Author:** ![prabhu634](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@prabhu634](https://root-forum.cern.ch/u/prabhu634)\
**Post date:** [May 20, 2024, 8:44am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/13 "2024-05-20T08:44:50Z")

</div>

Hi @bellenot ,

Any update for the above query related to build instructions?

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 21, 2024, 6:55am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/14 "2024-05-21T06:55:11Z")

</div>

See [Cling Build Instructions - ROOT](https://root.cern/cling/cling_build_instructions/)  
And adapt the following part of the recipe for Visual Studio:

```auto
mkdir build
cd build
cmake -G"Visual Studio 17 2022" -A x64 -Thost=x64 -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_TOOLS=Off -DLLVM_EXTERNAL_PROJECTS=cling -DLLVM_EXTERNAL_CLING_SOURCE_DIR=../cling -DLLVM_ENABLE_PROJECTS=clang -DLLVM_TARGETS_TO_BUILD="host;NVPTX" src/llvm
cmake --build . --config Release

```

---

<div class="post-metadata">

**Author:** ![prabhu634](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@prabhu634](https://root-forum.cern.ch/u/prabhu634)\
**Post date:** [May 23, 2024, 6:50am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/15 "2024-05-23T06:50:41Z")

</div>

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:

1. Created a directory named CLING\_Latest in C drive  
C:\CLING\_Latest

2. Go inside CLING\_Latest directory using cd command

3. git clone -b cling-llvm16 `https://github.com/root-project/llvm-project.git` src

4. git clone `https://github.com/root-project/cling.git`

5. mkdir build

6. cd build

7. C:\CMake\bin\cmake -G"Visual Studio 16 2019" -A Win32 -Thost=x64 -DCMAKE\_BUILD\_TYPE=Release -DLLVM\_BUILD\_TOOLS=Off -DLLVM\_EXTERNAL\_PROJECTS=cling -DLLVM\_EXTERNAL\_CLING\_SOURCE\_DIR=…/cling -DLLVM\_ENABLE\_PROJECTS=clang -DLLVM\_TARGETS\_TO\_BUILD=“host;NVPTX” …/src/llvm

8. C:\CMake\bin\cmake --build . --config Release

CLING utility was built and the version is as below:  
C:\CLING\_Latest\build\Release\bin\>cling.exe --version  
**1.1~dev**

Even, with this version of CLING, I am finding the same issue of abrupt CLING exe closure when mutex lock and unlock is used.

Please confirm for the build instructions

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 23, 2024, 6:59am UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/16 "2024-05-23T06:59:41Z")

</div>

> [@prabhu634](#):
>
> Please confirm for the build instructions

It looks fine

> [@prabhu634](#):
>
> Even, with this version of CLING, I am finding the same issue of abrupt CLING exe closure when mutex lock and unlock is used.

With my examples? Then it’s maybe a problem with VS 2019… I can make some tests later if you confirm you have the same error with my examples

---

<div class="post-metadata">

**Author:** ![prabhu634](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@prabhu634](https://root-forum.cern.ch/u/prabhu634)\
**Post date:** [May 29, 2024, 1:02pm UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/17 "2024-05-29T13:02:08Z")

</div>

Hi @bellenot ,

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

Any idea as to what might be issue here?

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 29, 2024, 1:05pm UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/18 "2024-05-29T13:05:09Z")

</div>

> [@prabhu634](#):
>
> If I load the .cpp file after my project DLLs, then I see the abrupt ending in CLING
> 
> Any idea as to what might be issue here?

I would say that it depends on what you do in your project’s DLLs…

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [June 12, 2024, 1:05pm UTC](https://root-forum.cern.ch/t/can-mutex-and-its-locking-and-unlocking-features-be-used-in-cling-interpreter/59274/19 "2024-06-12T13:05:10Z")

</div>

Closed on June 12, 2024, 1:05pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
