Advertisement
Guest User

Untitled

a guest
Sep 28th, 2013
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. /*
  2. Multicore Example.c
  3.  
  4. Version 0.94 for use with SimpleIDE 9.40 and its Simple Libraries
  5.  
  6. Launch a function into another cog (processor) and display what it does
  7. to the global n variable over time.
  8.  
  9. http://learn.parallax.com/propeller-c-functions/multicore-example
  10. */
  11.  
  12. #include "simpletools.h" // Include simpletools
  13.  
  14. void adder(void *par); // Forward declaration
  15. void subtracter(void *par);
  16.  
  17. static volatile int t, n, m; // Global vars for cogs to share
  18. unsigned int stack[40 + 25]; // Stack vars for other cog
  19. unsigned int unstack[40 + 25];
  20.  
  21. int main() // main function
  22. {
  23. t = 50; // Set values of t & n
  24. n = 5000;
  25. m = 5000;
  26.  
  27. // Launch adder function into another cog (processor).
  28. int cog = cogstart(&adder, NULL, stack, sizeof(stack));
  29. int cogtwo = cogstart(&subtracter, NULL, unstack, sizeof(unstack));
  30. print("cog1 = %d and cogtwo = %d\n", cog, cogtwo);
  31.  
  32. // Watch what the other cog is doing to the value of n.
  33. while(1)
  34. {
  35. print("n = %d and m = %d\n", n, m); // Display result
  36. pause(100); // Wait 1/10 of a second
  37. }
  38. }
  39.  
  40. // Function that can continue on its
  41. // own if launched into another cog.
  42. void adder(void *par) // Adder keeps going on its own
  43. {
  44. while(1) // Endless loop
  45. {
  46. n = n + 1; // n + 1 each time loop repeats
  47. pause(t); // Wait for t ms between updates
  48. }
  49. }
  50.  
  51. void subtracter(void *par)
  52. {
  53. while(1)
  54. {
  55. m = m - 1;
  56. pause(t);
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement