Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Multicore Example.c
- Version 0.94 for use with SimpleIDE 9.40 and its Simple Libraries
- Launch a function into another cog (processor) and display what it does
- to the global n variable over time.
- http://learn.parallax.com/propeller-c-functions/multicore-example
- */
- #include "simpletools.h" // Include simpletools
- void adder(void *par); // Forward declaration
- void subtracter(void *par);
- static volatile int t, n, m; // Global vars for cogs to share
- unsigned int stack[40 + 25]; // Stack vars for other cog
- unsigned int unstack[40 + 25];
- int main() // main function
- {
- t = 50; // Set values of t & n
- n = 5000;
- m = 5000;
- // Launch adder function into another cog (processor).
- int cog = cogstart(&adder, NULL, stack, sizeof(stack));
- int cogtwo = cogstart(&subtracter, NULL, unstack, sizeof(unstack));
- print("cog1 = %d and cogtwo = %d\n", cog, cogtwo);
- // Watch what the other cog is doing to the value of n.
- while(1)
- {
- print("n = %d and m = %d\n", n, m); // Display result
- pause(100); // Wait 1/10 of a second
- }
- }
- // Function that can continue on its
- // own if launched into another cog.
- void adder(void *par) // Adder keeps going on its own
- {
- while(1) // Endless loop
- {
- n = n + 1; // n + 1 each time loop repeats
- pause(t); // Wait for t ms between updates
- }
- }
- void subtracter(void *par)
- {
- while(1)
- {
- m = m - 1;
- pause(t);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement