5

I want to run two functions one after the other that accesses a third function with a condition that when the first is using the third, the second function should wait. It should be able to use the third function after first time period of accessing the third function over.

This concept sounds like implementing round robin scheduling with context switching. I know this theoretically, but I want to apply it practically. How can I achieve this and do context switching? Can someone provide me an example to do this? Is there any other way to achieve this as well?

EDIT: Actually i am plotting markers on google maps ,using gmap.net .i have created two thread functions for the two markers.they will be fetching the required data from two separate files.converting it into lat long and plotting on map.with this approach i am creating duplicate functions for both having same functionality.but i guess its not a good way of programming so want to use one common function for plotting and fetching data from file to convert .

so when one thread is accessing the common function another one should wait .once first one release the function or its time period to work on that function is exceeded it should perform context switch and second thread should access the common function. this is what i am trying to achieve.if i should modify my approach please do let me know. enter image description here

pranjal khanduri
  • 351
  • 1
  • 3
  • 16

1 Answers1

9

Sounds like 2 tasks with a lock should do what you want:

class Program
{
    static void Main(string[] args)
    {
        var task1 = Task.Run(() => Func1());
        var task2 = Task.Run(() => Func2());

        Task.WaitAll(task1, task2);
    }

    static object lockObj = new object();

    static void Func1()
    {
        for (int i = 0; i < 10; i++)
        {
            Func3("Func1");
            Thread.Sleep(1);
        }
    }

    static void Func2()
    {
        for (int i = 0; i < 10; i++)
        {
            Func3("Func2");
            Thread.Sleep(1);
        }
    }

    static void Func3(string fromFn)
    {
        lock(lockObj)
        {
            Console.WriteLine("Called from " + fromFn);
        }
    }
}

The lock prevents the enclosed code from running in more than one thread at once. (The Sleep statements are there purely to slow the functions down for demonstration purposes - they will have no place in your final code).

Output is:

Called from Func2
Called from Func1
Called from Func2
Called from Func1
Called from Func2
Called from Func1
Called from Func1
Called from Func2
Called from Func1
Called from Func2
Baldrick
  • 11,712
  • 2
  • 31
  • 35
  • This doesn't necessarily do what the OP wants. There is no guarantee that both functions will alternate evenly. – InBetween Mar 06 '17 at 13:53
  • 1
    @InBetween: Hmm... I didn't see anything in the requirements that specified 'even'. But the question isn't crystal clear in that area. :) – Baldrick Mar 06 '17 at 14:01
  • Its not, but *"I want to run two functions one after the other..."* makes me wonder, hence the comment so its clear that your solution doesn't enforce it. Aside from that, its a good answer. – InBetween Mar 06 '17 at 14:03
  • @InBetween: Fair point. Whether it's appropriate depends on details that are absent from the question. Let's hope it's what is required. – Baldrick Mar 06 '17 at 14:04
  • @Baldrick sir if i include time limitations on func1 and func2 that they can access func 3 only for certain interval of time ,if any one of then exceed the limit context switch should occur so that next time the function resumes from the last position where it stops. – pranjal khanduri Mar 07 '17 at 06:08