Is it possible to interleave execution of multiple delegates on a single thread?

Chin

Is it possible to create a function that looks like this:

void ExecuteInterleave(Action a, Action b) {
    ...
}

that will automatically execute a and b interleavingly on the current thread? For example, a would run for a few hundred milliseconds, then b would run, repeat. It would block the current thread until both a and b have returned.

This question is purely from a theoretical standpoint, as I'm curious if it's possible.

Alexei Levenkov

Automatically - no since there is no way for current thread to magically stop executing a method.

If you can get delegates to cooperate in multitasking - doable. Cool looking approach would be to use yield return in each delegate to and driver method would iterate both "sequences" in order it wants.

IEnumerable<int> A()
{
   // cooperative step 1    
   yield return 0;
   // cooperative step 3    
   yield return 0;
}

IEnumerable<int> B()
{
   // cooperative step 2
   yield return 0;
   // cooperative step 4    
   yield return 0;
}

void ExecuteInterleave(Func<IEnumerable<int>> a, Func<IEnumerable<int>> b) {
 var i1 = a().GetEnumerator();
 var i2 = b().GetEnumerator();

 i1.MoveNext();
 i2.MoveNext();
 i1.MoveNext();
 i2.MoveNext();
}
...
ExecuteInterleave(A, B);    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is it possible to create a single socket, single thread, TCP/IP packet reader for multiple clients?

From Dev

How to manage single thread execution properly?

From Dev

Java single thread of execution and cpu cache

From Dev

Multiple transaction scopes in single thread?

From Dev

Synchronization of multiple tasks on single thread

From Dev

Queuing Actions/Delegates for Asyncronous Execution

From Dev

Queuing Actions/Delegates for Asyncronous Execution

From Dev

Run multiple IQueryable's at single point of execution

From Dev

Execution of multiple php pages on a single click

From Dev

Multiple path execution through single curl request

From Dev

Multiple delegates/datasources in UITableView

From Dev

Multiple delegates for UITableView in swift

From Dev

Multiple delegates/datasources in UITableView

From Dev

Delegates with multiple methods

From Dev

Delphi - terminate thread after single successful execution or by user cancellation

From Dev

Is it possible to stop single threaded execution of code from UI?

From Dev

Can a server handle multiple sockets in a single thread?

From Dev

Send Data from multiple threads to a single thread

From Dev

Sequence of execution in multicast delegates in c#

From Dev

How is it possible that go routines interleave perfectly?

From Dev

All possible ways to interleave two strings

From Dev

Multiple web container thread pools in WebSphere - is it possible?

From Dev

Multiple Thread Writer with Single Thread Reader using PipedOutputStream and PipedInputStream

From Dev

Is it possible to have multiple controllers for a single entity type?

From Dev

Is it possible to `git squash` multiple branches into a single branch?

From Dev

A single exception handler for multiple possible exceptions

From Dev

Is it possible to use multiple macros on a single line?

From Dev

Is it possible to set multiple decorators for a single node?

From Dev

Multiple indices under a single name in kibana is possible?

Related Related

  1. 1

    Is it possible to create a single socket, single thread, TCP/IP packet reader for multiple clients?

  2. 2

    How to manage single thread execution properly?

  3. 3

    Java single thread of execution and cpu cache

  4. 4

    Multiple transaction scopes in single thread?

  5. 5

    Synchronization of multiple tasks on single thread

  6. 6

    Queuing Actions/Delegates for Asyncronous Execution

  7. 7

    Queuing Actions/Delegates for Asyncronous Execution

  8. 8

    Run multiple IQueryable's at single point of execution

  9. 9

    Execution of multiple php pages on a single click

  10. 10

    Multiple path execution through single curl request

  11. 11

    Multiple delegates/datasources in UITableView

  12. 12

    Multiple delegates for UITableView in swift

  13. 13

    Multiple delegates/datasources in UITableView

  14. 14

    Delegates with multiple methods

  15. 15

    Delphi - terminate thread after single successful execution or by user cancellation

  16. 16

    Is it possible to stop single threaded execution of code from UI?

  17. 17

    Can a server handle multiple sockets in a single thread?

  18. 18

    Send Data from multiple threads to a single thread

  19. 19

    Sequence of execution in multicast delegates in c#

  20. 20

    How is it possible that go routines interleave perfectly?

  21. 21

    All possible ways to interleave two strings

  22. 22

    Multiple web container thread pools in WebSphere - is it possible?

  23. 23

    Multiple Thread Writer with Single Thread Reader using PipedOutputStream and PipedInputStream

  24. 24

    Is it possible to have multiple controllers for a single entity type?

  25. 25

    Is it possible to `git squash` multiple branches into a single branch?

  26. 26

    A single exception handler for multiple possible exceptions

  27. 27

    Is it possible to use multiple macros on a single line?

  28. 28

    Is it possible to set multiple decorators for a single node?

  29. 29

    Multiple indices under a single name in kibana is possible?

HotTag

Archive