[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

Communication With External Function

The next example is comprised of 3 files. The main program spawns a new thread with an external function (from a loaded module) and uses the same channel mechanism for communication between the threads.

Program Listing 3-10 is the main program.

Program Listing 3-10 chanx2a.b

  1. implement Command;
  2. include "sys.m";
  3. include "draw.m";
  4. include "chanx.m";
  5. sys: Sys;
  6. print: import sys;
  7. cx: ChanX;
  8. sc: chan of int;
  9. rc: chan of int;
  10. Command: module {
  11. init: fn(ctxt: ref Draw->Context, argv: list of string);
  12. };
  13. init(ctxt: ref Draw->Context, argv: list of string) {
  14. sys = load Sys Sys->PATH;
  15. cx = load ChanX "./chanx2b.dis";
  16. sc = chan of int;
  17. rc = chan of int;
  18. for (i:=1; i<=5; i++) {
  19. spawn cx->func(sc, rc);
  20. print("%2d\n", i);
  21. sc<- = i;
  22. func(<-rc);
  23. }
  24. }
  25. func(i: int) {
  26. print("%6d\n", i);
  27. }

Program Listing 3-11, chanx2b.m, is the module interface file for Program Listing 3-12, chanx2b.b.

Program Listing 3-11 chanx2b.m

  1. ChanX: module {
  2. func: fn(sc: chan of int, rc: chan of int);
  3. };


Program Listing 3-12	 chanx2b.b

  1. implement ChanX;
  2. include "sys.m";
  3. include "draw.m";
  4. include "chanx.m";
  5. sys: Sys;
  6. print: import sys;
  7. func(sc: chan of int, rc: chan of int) {
  8. sys = load Sys Sys->PATH;
  9. i: int;
  10. i = <-sc;
  11. print("%4d\n", i);
  12. rc<- = i;
  13. }



[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

Copyright © 1998, Lucent Technologies, Inc. All rights reserved.