"C + + Programming" C + + implements multithreaded programming

Source: Internet
Author: User

In the C + + multithreaded programming implementation in two ways, one is the CreateProcess in the Windows header file, the other is the _beginthread in the process.h, I use the latter kind of, And it encapsulates multithreaded operations into Java-like thread classes.

The thread class contains four operations (several states of the corresponding thread): Ready (Start), suspend (suspend), resume (resume), and terminate (terminate), plus an operation that can set the line blocks until those.

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

#ifndef Thread_h#define THREAD_H#include <windows.h>#include <process.h>typedefvoid*handle; class Thread{ Public: Thread ();//Builder    voidStart ();//thread start functionVirtualvoidRun ();//thread run function    intSuspend ();//thread hangs, returns the number of previous hangs    intResume ();//thread recovery, return the previous suspend Count    voidTerminate ();//forcibly terminate the process    voidSetTimeOut (DWORD time);//Set timeout time, default is infinite   DWORDGetTimeout ();//Return time-outPrivate:HANDLEM_h;//When the handle of the front thread   DWORDM_timeout;//Timeout period    Static voidInitvoid*_H);//Run 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 default timeout for Infinity}void Thread:: Start() {M_h=(HANDLE) _beginthread (INIT,0, (void *) this);//Create thread}void Thread:: Run(){//Thread Run method}void Thread:: Init(void*_h) {Thread *T=(Thread*) _h; T -Run (); DWORD DW=WaitForSingleObject (t -M_h, T -M_timeout);//wait for thread, end of 500ms after timeout, infinite means no timeoutSwitch (DW) { CaseWait_object_0:break; CaseWait_timeout:cout<< " Time Out" <<Endl Break CaseWait_failed:break; }}//thread hangs, returns the number of previous hangsIntThread:: Suspend(){returnSuspendThread (This -M_h);}//thread recovery, return the previous suspend CountIntThread:: Resume() {DWORD re=ResumeThread (This -M_H);returnRe;}//forcibly terminate the processvoid Thread:: Terminate() {TerminateThread (this -M_h, exit_success);}//Set timeout time, default is infinitevoid Thread:: SetTimeOut(DWORD time) {This -M_timeout=Time;}//Return time-outDWORDThread:: GetTimeout(){returnThis -M_timeout;}

Test file (TestThread.cpp):

 //Test files#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;classTestthread: Publicthread{ Public: Testthread (string_s) {s = _s; }stringSvoidRun ();};voidTestthread::run () { for(inti =0; I <Ten; i++) {cout<<s+"\ n"; }}intMainintargcChar*argv[]) {Testthread *dt[Ten]; dt[0] =NewTestthread ("A");//New Thread Adt[1] =NewTestthread ("B");//new thread Bdt[0]->start ();//Turn on thread a    cout<<dt[0]->suspend () <<endl;//Suspend thread adt[1]->settimeout ( -); dt[1]->start ();//Turn on thread BSleep ( -);//Pause 2s, at which time thread A is still in the suspended state, so do not executedt[0]->resume ();//Recover thread A, at which time thread A is in the recovery state and continues executionGetChar ();}

Test results

"C + + Programming" C + + implements multithreaded programming

Related Article

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.