zoukankan      html  css  js  c++  java
  • C++:创建线程初试

    1.使用CreatThread创建

    #include <iostream>
    #include <Windows.h>
    using namespace std;
    /*
    创建一个线程
    */
    DWORD WINAPI fun(LPVOID ipParamter)  
    {
        while (true)
        {
            cout << "fun1 display!" << endl; Sleep(1000);
        }
    }
    /*
    创建第二个线程
    */
    DWORD WINAPI fun2(LPVOID i)
    {
        while (true)
        {
            cout << "fun2 " << endl; Sleep(1500);
        }
    }
    int main()
    {
        //第二个参数0是初始的字节,第五个参数0是便是立即开始线程
        HANDLE hThread = CreateThread(NULL, 0, fun, NULL, 0, NULL);
        HANDLE hThread2 = CreateThread(NULL, 0, fun2, NULL, 0, NULL);
        CloseHandle(hThread);
        CloseHandle(hThread2);
        
        while (true)
        {
            cout << "main display!" << endl; Sleep(2000);
        }
        
        return 0;
    } 
      
    使用CreatThread创建线程是不安全的,容易造成内存泄漏(CRT相关),所以推荐使用_beginThread或_beginThreadex,它内部也使用了CreatThread,但是在使用前做了很多工作以确保其安全性。
     
    2.使用_beginThread创建
    #include <iostream>
    #include <Windows.h>
    #include <process.h>
    using namespace std;
    void  fun1()
    {
        while (true)
        {
            cout << "fun1 display
    "; Sleep(1000);
        }
    }
    unsigned _stdcall fun2(void * )
    {
        while (true)
        {
            cout << "fun2 display
    "; Sleep(1000);
        }
    }
    int main()
    {
        unsigned int thID1, thID2;
        HANDLE hfun1, hfun2;
        hfun1 = (HANDLE)_beginthread((void(*)(void*))fun1, 0, NULL);
        hfun2 = (HANDLE)_beginthreadex(NULL, 0, fun2, NULL, 0, &thID2);
        WaitForSingleObject(hfun1, INFINITE); //一定要等子线程完毕
        WaitForSingleObject(hfun2, INFINITE);
        CloseHandle(hfun1);
        CloseHandle(hfun2);
        
        
        cout << "end
    ";
        return 0;
    }

    3.std::thread

    C++11以来支持了thread类,方便了多线程的创建管理。

    #include <mutex>
    #include <iostream>
    #include <thread>
    #include <windows.h>
    using namespace std;
    mutex m;
    void f()
    {
        cout << "using..." << endl;
    }
    int a;
    void fun1(int i)
    {
        
        while (true)
        {
            m.lock();
            //a++; cout << "f1"<< endl; Sleep(1000);
            f();
            Sleep(5000);
            m.unlock();
        }
    }
    void fun2()
    {
        
        while (true)
        {
            m.lock();
        //    a--; cout << "f2"<< endl; Sleep(1000);
            f();
            Sleep(1000);
            m.unlock();
        }
    }
    int main()
    {
        a = 1;
        thread th(fun1, 1);
        thread ti(fun2);
        th.join();
        ti.join();
        
        return 0;
    }

    相关博客:C++:线程(std::thread)

      

    ------------ 转载请注明出处 ------------
  • 相关阅读:
    Django 06模板语言的复用
    Django 07模型层—单表操作
    Django 05模板-变量、过滤器、 标签
    Django 04(视图层基础01)
    Django 03 (路由层基础)
    Django--02(项目创建,数据请求迁移,单表orm增删改查)
    Web 文本、reset操作、高级选择器、边界圆角、a的四大伪类、精灵图
    web前端 基础选择器、长度与颜色、display、嵌套、盒模型
    MySQL之数据库的常用语句
    PHP中级篇 Apache配置httpd-vhosts虚拟主机总结及注意事项(转载)
  • 原文地址:https://www.cnblogs.com/whlook/p/6486007.html
Copyright © 2011-2022 走看看