zoukankan      html  css  js  c++  java
  • C++多线程二

    SuspendThread()暂停一个线程,ResumeThread()重启一个线程。参数均为线程的句柄。

    #include <iostream>
    #include <windows.h>
    using namespace std;
    DWORD WINAPI mythread(void *p)
    {
        for(int i=0;i<100;i++)
        {
            cout<<"hello,everybody!"<<endl;
            ::Sleep(100);
        }
        return 0;
    }
    
    int main()
    {
        HANDLE handle;
        DWORD dw;
        handle=::CreateThread(NULL,0,mythread,NULL,0,&dw);
        ::Sleep(100);
        for(int i=0;i<10;i++)
        {
            cout<<"---now,suspended!----"<<endl;
            ::SuspendThread(handle); //暂停
            for(int j=0;j<3;j++)
            {
                cout<<"good,thank you!"<<endl;
                ::Sleep(100);
            }
            cout<<"----now,resumed!----"<<endl;
            ::ResumeThread(handle); //重启
            ::Sleep(300);
        }
        ::CloseHandle(handle);
        return 0;
    }

    TerminateThread()终止一个线程,两个参数(线程的句柄,线程的退出码)。此函数比较危险,被终止的线程函数立马停止执行,无法释放相关资源。

    DWORD WINAPI mythread(void *lp)
    {
        for(int i=0;i<20;i++)
        {
            cout<<"hello"<<endl;
            ::Sleep(100);
        }
        return 0;
    }
    
    int main()
    {
        HANDLE handle;
        DWORD dw;
        handle=::CreateThread(NULL,0,mythread,NULL,0,&dw);
        if(handle==NULL)
        {
            cout<<"thread failed!"<<endl;
            return  -1;
        }
        ::Sleep(1000);
        cout<<"thread terminated!"<<endl; 
        ::TerminateThread(handle,2);//终止线程的执行 
        ::Sleep(1000);
        ::CloseHandle(handle);
        return 0;
    }

    GetExitCodeThread()用来查询某个线程的退出码。如果线程还在运行,返回STILL_ACTIVE;如果已经结束,则返回由TerminateThread()或ExitThread()设置的退出码。

    DWORD WINAPI mythread(void *p)
    {
        cout<<"mythread id is:"<<::GetCurrentThreadId()<<endl;
        return 8;
    }
    //分别输出线程ID和线程退出码
    int main()
    {
        HANDLE handle;
        DWORD dw;
        handle=::CreateThread(NULL,0,mythread,NULL,0,&dw);
        ::Sleep(1000);
        ::GetExitCodeThread(handle,&dw);
        cout<<"thread exitCode is:"<<dw<<endl;
        return 0;
    }

    ExitThread()退出线程,并释放相关的资源。参数为线程的退出码。

    DWORD WINAPI mythread(void *p)
    {
        cout<<"mythread id is :"<<::GetCurrentThreadId()<<endl;
        cout<<"thread exit!"<<endl;
        ::ExitThread(5);//参数即为线程的退出码
        cout<<"不能执行到这里"<<endl;
        return 5;
    }
    int main()
    {
        HANDLE handle;
        DWORD dw;
        handle=::CreateThread(NULL,0,mythread,NULL,0,&dw);
        ::Sleep(50);
        ::GetExitCodeThread(handle,&dw);
        cout<<"thread id is :"<<dw<<endl;
        ::CloseHandle(handle);
        return 0;
    }

    BOOL WINAPI SetThreadPriority(HANDLE hThread,int nPriority)设置线程的优先级。

    //(线程句柄,要设置的优先级)

    Int WINAPI GetThreadPriority(HANDLE hThread);获得线程的优先级。

  • 相关阅读:
    SQL DATE_SUB 函数用法
    MySQL中concat函数(连接字符串)
    Mysql命令
    PHP 数字转汉字函数
    php 递归的生成目录函数
    我的SublimeText配置
    常见HTTP状态200,304,403,404,503
    鞋子特大号歌词
    去掉iframe白色背景方法
    php 空格无法替换,utf-8空格惹的祸
  • 原文地址:https://www.cnblogs.com/hometown/p/3394256.html
Copyright © 2011-2022 走看看