/////////////////////////////////// // // thread_ex project // thread_1.cpp // // created 7/13/21 by tj // rev 0 // /////////////////////////////////// // // Example for thread usage // // **** DONT FORGET THE WHILE() IN THE THREAD **** // **** IF YOU WANT TO KEEP IT RUNNING **** // // printing various characters based on threads // /////////////////////////////////// #include "mbed.h" #include // function prototypes (for thread callback) void Plus(void); void Minus(void); void Star(void); // create seperate thread for each character Thread T_plus; Thread T_minus; Thread T_star; // globals to manipulate in the threads static int p = 0; static int m = 0; static int s = 0; int main(void){ setbuf(stdout, NULL); // splash printf("\n\nthread_1 - example for EE2905\n"); printf("Using Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); // start the threads T_plus.start(callback(Plus)); T_minus.start(callback(Minus)); T_star.start(callback(Star)); // main loop prints CR - every 5 seconds while(1){ printf("\n"); printf("%i %i %i\n", p, m, s); wait_us(4800000); }// end while return 0; }// end main void Plus(void){ while(1){ printf("+"); p++; ThisThread::sleep_for(200ms); }// end while return; }// end plus void Minus(void){ int i; for(i = 0; i < 50; i++){ printf("-"); m++; ThisThread::sleep_for(600ms); }// end for return; }// end minus void Star(void){ while(1){ printf("*"); s++; ThisThread::sleep_for(1200ms); }// end while return; }// end minus