diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2b33f04..bcec49c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,9 @@ add_executable(proxy-network - ./proxy-network.cpp + ./mt_web_server.cpp +) + +add_executable(tempmulti + ./multi_threads.cpp ) target_include_directories(proxy-network PUBLIC ${CMAKE_CURRENT_LIST_DIR}) diff --git a/src/multi_threads.cpp b/src/multi_threads.cpp index b3b7190..e64fd4d 100644 --- a/src/multi_threads.cpp +++ b/src/multi_threads.cpp @@ -17,36 +17,51 @@ * 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 50 // Number of child thread's repeats +#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 *dummy); // The child thread #1 -void ChildThread2(void *dummy); // The child thread #2 +void ChildThread1(void); // The child thread #1 +void ChildThread2(void); // The child thread #2 /* The MAIN --------------------------------------------------------------- */ -void main (void) +int main (void) { /* Set the child thread status (TRUE = RUNNING) --- */ - nChild1_status = TRUE; - nChild2_status = TRUE; + nChild1_status = true; + nChild2_status = true; /* Create and start the two threads --- */ - _beginthread (ChildThread1, 0, NULL ); // Start child process #1 - _beginthread (ChildThread2, 0, NULL ); // Start child process #2 + 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)) + while ((nChild1_status == true)||(nChild2_status == true)) { ; } /* Two threads are now finished --- */ @@ -55,7 +70,7 @@ void main (void) } // The Child-Thread #1 /////////////////////////////////////////////////////// -void ChildThread1(void *dummy) +void ChildThread1(void) { /* Child #1 local variable(s) --- */ int i; // Loop counter @@ -64,24 +79,24 @@ void ChildThread1(void *dummy) printf("Child Thread #1 has started ... \n"); /* wait for 10 seconds w/ count down --- */ - for (i = 0; i < REPEATS ; i++) + for (i = 0; i < REPEATS; i++) { /* Wait for 10 seconds --- */ - Sleep((clock_t)INTERVAL * CLOCKS_PER_SEC); + 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; + nChild1_status = false; /* Terminate this thread --- */ - _endthread(); + //_endthread(); } // The Child-Thread #2 /////////////////////////////////////////////////////// -void ChildThread2(void *dummy) +void ChildThread2(void) { /* Child #2 local variable(s) --- */ int i; // Loop counter @@ -90,20 +105,20 @@ void ChildThread2(void *dummy) printf("Child Thread #2 has started ... \n"); /* wait for 10 seconds w/ count down --- */ - for (i = 0; i < REPEATS ; i++) + for (i = 0; i < REPEATS; i++) { /* Wait for 10 seconds --- */ - Sleep((clock_t)INTERVAL * CLOCKS_PER_SEC); + 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; + nChild2_status = false; /* Terminate this thread --- */ - _endthread(); + //_endthread(); } -// THE END OF LINES ////////////////////////////////////////////////////////// \ No newline at end of file +// THE END OF LINES //////////////////////////////////////////////////////////