C# and C++ Synchronize between processes -
We have 2 apps written in C and another C ++ is shared between these processes in a counter ( In Memory) we need to maintain each time one of these applications starts, it should check and extend this counter and every time the application is shut down it needs to reduce the counter. If the app has a crash or shutdown using the Task Manager, then we also need a counter to reduce it.
We think about using OS synchronization objects such as MUTEX.
My question: What type of sync object is best for cross process (when there is a C # and other C ++)
Hope that my question was clear. Thank you very much,
etc. Varda
with the nominated Sikram It may be far away Semofor is basically a count, it is to allow the developer to limit the number of threads / processes that reach some resources. Usually this works that
- you make a semaphore with the maximum count N.
- N Thread Wait for this function call,
WaitForSingleObject
or similar and every one of them is waited every time the internal semaphore counters down. - The N + 1 thread also calls the waiting function, but because our semaphore has an internal counter 0, it will have to wait.
- Our first N threads release the simofor by calling the
release sensor
function. This function increases the inner counter of the sequence. - Our waiting thread is no longer waiting, so it starts again, but the semaphore goes back to Counter 0.
I don 'do not think that you want to use it like this though. Therefore, instead, you should:
- Create the named counter with the initial counter set to zero.
- When the application begins, immediately issuing it, increasing the counter will get you the last counter value during that call.
- When the app is finished, call
WaitForSingleObject (hSemaphore, 0) , decreasing the counter 0 means that you do not want to wait.
It's all very easy.
In C ++
// semaphore handler h equore = spreadsheet (faucet, 0, bíg_ NUMBER, "my calm semaphore name"); // increase counter long pre_counter; Release Sander (Hesfor, 1, and prequocer); // Low Counter WaitingCurrent Object (HSAMFor, 0);
in C #
using the system. threading; // Secure vs Semaphore Semi = New Semaphore (0, BIIG NUMBER, "My Silent Sayyandra Name"); // increase counter int prev_counter = sem.Release (); // Less Counter sem.WaitOne (0);
The name and BIG_NUMBER should be evenly the same.
If this is not enough for your work, you will have to see shared memory and lock access for it, named Mute X, but it is a bit more complicated.
Comments
Post a Comment