/* ************************************************************************* * * * * Multi.cpp: * * This is a sample program for multi-threaded applications. * * * * As soon as this program starts, the main thread generates two child * * threads. The two child threads wait for 10 seconds and terminate. * * * * While the two child threads are running, the main thread waits. The * * main thread waits until both threads finish. * * * * Compile: * * In Project->Setting->C/C++->CodeGenartion(in Category) * * ->Select Multi-threaded for runtime library * * * * Coded by: H. Fujinoki * * September 12, 11:00 AM at Edwardsville, IL * * * * ************************************************************************* */ #include #define UNIX // WIN for Windows environment, UNIX for BSD or LINUX env. /* FOR WIN ------------------------------------------------------------- */ #ifdef WIN #include // for thread system calls (_beginthread, etc.) #include // for TRUE, FALSE labels #endif /* --------------------------------------------------------------------- */ /* FOR UNIX/LINUX ------------------------------------------------------ */ #ifdef UNIX #endif /* --------------------------------------------------------------------- */ #include #include #include // for printf #include // for clock() and CLK_TCK /* Global label defenition ------------------------------------------------ */ #define INTERVAL 1 // Transmission interval in seconds #define REPEATS 10 // Number of child thread's repeats /* Global Variables ------------------------------------------------------- */ int nChild1_status; // Child thread #1 status int nChild2_status; // Child thread #2 status /* Prototypes ------------------------------------------------------------- */ void ChildThread1(void); // The child thread #1 void ChildThread2(void); // The child thread #2 /* The MAIN --------------------------------------------------------------- */ int main (void) { /* Set the child thread status (TRUE = RUNNING) --- */ nChild1_status = true; nChild2_status = true; /* Create and start the two threads --- */ std::thread nChild1(ChildThread1); // Start child process #1 std::thread nChild2(ChildThread2); // Start child process #2 nChild1.join(); nChild2.join(); /* Spin-loop until both threads finish --- */ while ((nChild1_status == true)||(nChild2_status == true)) { ; } /* Two threads are now finished --- */ printf("Both child threads are finished ... \n"); printf("The main thread is finishing ... \n"); } // The Child-Thread #1 /////////////////////////////////////////////////////// void ChildThread1(void) { /* Child #1 local variable(s) --- */ int i; // Loop counter /* This thread is started --- */ printf("Child Thread #1 has started ... \n"); /* wait for 10 seconds w/ count down --- */ for (i = 0; i < REPEATS; i++) { /* Wait for 10 seconds --- */ std::this_thread::sleep_for(std::chrono::duration(1000)); /* Display count down --- */ printf("Child #1: %d more second(s) to finish ...\n", REPEATS -i); } /* Reset the status flag --- */ nChild1_status = false; /* Terminate this thread --- */ //_endthread(); } // The Child-Thread #2 /////////////////////////////////////////////////////// void ChildThread2(void) { /* Child #2 local variable(s) --- */ int i; // Loop counter /* This thread is started --- */ printf("Child Thread #2 has started ... \n"); /* wait for 10 seconds w/ count down --- */ for (i = 0; i < REPEATS; i++) { /* Wait for 10 seconds --- */ std::this_thread::sleep_for(std::chrono::duration(1000)); /* Display count down --- */ printf("Child #2: %d more second(s) to finish ...\n", REPEATS -i); } /* Reset the status flag --- */ nChild2_status = false; /* Terminate this thread --- */ //_endthread(); } // THE END OF LINES //////////////////////////////////////////////////////////