Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# and C++ Synchronize between processes

We have 2 applications. One written in C# and the other in C++. We need to maintain a counter (in memory) shared between these processes. Every time one of these applications start, it needs to check for this counter and increase it and every time the application shut-down it needs to decrease the counter. If the application has a crash or shut-down using task manager, we also need the counter to decrease.
We thought of using one of the OS synchronization objects like MUTEX.
My question: What kind of sync object is the best for cross process (when one is C# and the other C++)

Hope my question was clear.
Thank you very much,

Adi Barda

like image 716
Adi Barda Avatar asked Feb 02 '10 09:02

Adi Barda


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

You might get away with named semaphore. Semaphore is basically a count, it is here to allow developers limit the number of thread/processes that are accessing some resource. Usually it works like that

  1. You create a semaphore with maximum count N.
  2. N threads call waiting function on it, WaitForSingleObject or similar and each of them go on without waiting. Each time internal semaphore counter goes down.
  3. N+1 thread also calls waiting function but because internal counter of our semaphore is 0 now, it has to wait.
  4. One of our first N threads releases the semaphore by calling ReleaseSemaphore function. This function increments internal counter of semaphore.
  5. Our waiting thread don't have to wait now, so it resumes but semaphore counter goes back to 0.

I don't think this is how you want to use it though. So, instead, you should:

  1. Create named semaphore with initial counter set to zero.
  2. When application start, immediately release it, increasing the counter. You'll get previous counter value during that call.
  3. When application ends, call WaitForSingleObject(hSemaphore, 0), decreasing the counter. 0 means you don't want to wait.

This is all quite easy.

In C++

//create semaphore
HANDLER hSemaphore = CreateSemaphore(NULL, 0, BIG_NUMBER, "My cool semaphore name");
//increase counter
LONG prev_counter;
ReleaseSemaphore(hSemaphore, 1, &prev_counter);
//decrease counter
WaitForSingleObject(hSemaphore, 0);

In C#

using System.Threading;
//create semaphore
Semaphore sem = new Semaphore(0, BIG_NUMBER, "My cool semaphore name");
//increase counter
int prev_counter = sem.Release(); 
//decrease counter
sem.WaitOne(0);

Names and BIG_NUMBERs should be the same obviously.

If this is not sufficient for your task, you will have to look into shared memory and lock access to it though named mutex, but that is a little bit more complicated.

like image 139
vava Avatar answered Sep 20 '22 05:09

vava