Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronized methods in C#

Part of porting a Java application to C# is to implement a synchronized message buffer in C#. By synchronized I mean that it should be safe for threads to write and read messages to and from it.

In Java this can be solved using synchronized methods and wait() and notifyAll().

Example:

public class MessageBuffer {
    // Shared resources up here

    public MessageBuffer() {
        // Initiating the shared resources
    }

    public synchronized void post(Object obj) {
        // Do stuff
        wait();
        // Do more stuff
        notifyAll();
        // Do even more stuff
    }

    public synchronized Object fetch() {
        // Do stuff
        wait();
        // Do more stuff
        notifyAll();
        // Do even more stuff and return the object
    }
}

How can I achieve something similar in C#?

like image 405
Dimme Avatar asked Feb 22 '13 15:02

Dimme


People also ask

What are synchronized methods?

Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods.

What is synchronization C?

Process Synchronization in C/C++ CC++Server Side ProgrammingProgramming. Process synchronization is the technique to overcome the problem of concurrent access to shared data which can result in data inconsistency.

What is synchronized method in C#?

Synchronization in C# is a mechanism that makes sure only one process or thread accesses the critical section of the program. All the other threads have to wait until the critical section is free before they can enter it.


2 Answers

In .NET you can use the lock-statement like in

object oLock = new object();
lock(oLock){
  //do your stuff here
}

What you are looking for are mutexes or events. You can use the ManualResetEvent-class and make a thread wait via

ManualResetEvent mre = new ManualResetEvent(false);
...
mre.WaitOne();

The other thread eventually calls

mre.Set();

to signal the other thread that it can continue.

Look here.

like image 135
bash.d Avatar answered Nov 03 '22 01:11

bash.d


Try this:

using System.Runtime.CompilerServices;
using System.Threading;

public class MessageBuffer
{
    // Shared resources up here

    public MessageBuffer()
    {
        // Initiating the shared resources
    }

    [MethodImpl(MethodImplOptions.Synchronized)]
    public virtual void post(object obj)
    {
        // Do stuff
        Monitor.Wait(this);
        // Do more stuff
        Monitor.PulseAll(this);
        // Do even more stuff
    }

    [MethodImpl(MethodImplOptions.Synchronized)]
    public virtual object fetch()
    {
        // Do stuff
        Monitor.Wait(this);
        // Do more stuff
        Monitor.PulseAll(this);
        // Do even more stuff and return the object
    }
}
like image 39
Dave Doknjas Avatar answered Nov 03 '22 00:11

Dave Doknjas