[C ++ programming] C ++ implements multi-threaded programming and multi-threaded programming

Source: Internet
Author: User

[C ++ programming] C ++ implements multi-threaded programming and multi-threaded programming

In the multi-threaded programming implementation of C ++, there are two methods: CreateProcess in the Windows header file and process. the _ beginthread in h is used here, and the multi-Thread operation is encapsulated into a Thread class similar to that in Java.

The Thread class contains four operations (corresponding to several States of the Thread): start, suspend, resume, and terminate. In addition, it also contains an operation that can set thread timeout.

The Thread class code is as follows (Thread. h ):

# Ifndef THREAD_H # define THREAD_H # include <windows. h> # include <process. h> typedef void * HANDLE; class Thread {public: Thread (); // constructor void start (); // Thread start function virtual void run (); // thread-running function int suspend (); // thread suspension, returns the number of previous suspensions int resume (); // thread recovery, returns the number of previous suspensions void terminate (); // force terminate the process void setTimeOut (DWORD time); // set the timeout time. The default value is infinite dword getTimeOut (); // return the timeout time private: HANDLE m_h; // The current thread handle DWORD m_timeout; // time-out static void init (void * _ h); // run the current thread}; # endif

Implementation file (Thread. cpp ):

# Include <iostream> using std: cin; using std: cout; using std: endl; # include "Thread. h "Thread: Thread () {m_timeout = INFINITE; // set the default timeout value to infinity} void Thread: start () {m_h = (HANDLE) _ beginthread (init, 0, (void *) this); // create Thread} void Thread: run () {// Thread running method} void Thread :: init (void * _ h) {Thread * t = (Thread *) _ h; t-> run (); DWORD dw = WaitForSingleObject (t-> m_h, t-> m_timeout); // wait for the thread to end with a timeout of Ms. INFINITE indicates no timeout switch (dw) {case WAIT_OBJECT_0: break; case WAIT_TIMEOUT: cout <"Time Out" <endl; break; case WAIT_FAILED: break;} // indicates the number of times the Thread was suspended. int Thread: suspend () is returned () {return SuspendThread (this-> m_h);} // Thread recovery, returns the number of previous hangs int Thread: resume () {DWORD re = ResumeThread (this-> m_h ); return re;} // forcibly terminate the process void Thread: terminate () {TerminateThread (this-> m_h, EXIT_SUCCESS);} // set the timeout time. The default value is INFINITEvoid Thread :: setTimeOut (DWORD time) {this-> m_timeout = time;} // return timeout DWORD Thread: getTimeOut () {return this-> m_timeout ;}

Test file (TestThread. cpp ):

// Test file # include "Thread. h "# include <windows. h> # include <process. h ># include <iostream> using std: cin; using std: cout; using std: endl; # include <string> using std: string; class TestThread: public Thread {public: TestThread (string _ s) {s = _ s;} string s; void run () ;}; void TestThread: run () {for (int I = 0; I <10; I ++) {cout <s + "\ n" ;}} int main (int argc, char * argv []) {TestThread * dt [10]; dt [0] = new TestThread (""); // create thread A dt [1] = new TestThread ("B"); // create thread B dt [0]-> start (); // enable thread A cout <dt [0]-> suspend () <endl; // suspend thread A dt [1]-> setTimeOut (500 ); dt [1]-> start (); // enable thread B Sleep (2000); // pause for 2 s. Thread A is still suspended, therefore, do not execute dt [0]-> resume (); // restore thread A. At this time, thread A is in the recovery state, and continue to execute getchar ();}

Test Results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.