ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

线程学习二:创建与销毁线程

2022-03-03 14:02:33  阅读:393  来源: 互联网

标签:std 销毁 1.1 创建 void 线程 include


目录

1. 线程创建

1.1 std::thread创建线程

    1. 用普通函数创建线程
    1. 用成员函数创建线程
    1. 用类对象创建线程
    1. 用Lambda表达式创建线程

1.1.1 用普通函数创建线程

#include <iostream>
#include <thread>
 
void foo(int a)
{
    std::cout << a << std::endl;
}
 
int main()
{
    // Create and execute the thread
    std::thread thread(foo, 10); // foo is the function to execute, 10 is the
                                 // argument to pass to it
 
    // Keep going; the thread is executed separately
 
    // Wait for the thread to finish; we stay here until it is done
    thread.join();
 
    return 0;
}
// 编译方法: g++ -g test.cpp -o test -std=c++11 -lpthread

1.1.2 用成员函数创建线程

#include <iostream>
#include <thread>
 
class Bar
{
public:
    void foo(int a)
    {
        std::cout << a << std::endl;
    }
};
 
int main()
{
    Bar bar;
    
    // Create and execute the thread
    std::thread thread(&Bar::foo, &bar, 10); // Pass 10 to member function
 
    // The member function will be executed in a separate thread
 
    // Wait for the thread to finish, this is a blocking operation
    thread.join();
 
    return 0;
}

// 编译方法: g++ -g test.cpp -o test -std=c++11 -lpthread

1.1.3 用类对象创建线程

#include <iostream>
#include <thread>

// 类要变成可调用对象需要重载操作符()
class Bar
{
public:
    void operator()(int a)
    {
        std::cout << a << std::endl;
    }
};
 
int main()
{
    Bar bar;
    
    // Create and execute the thread
    std::thread thread(bar, 10); // Pass 10 to functor object
 
    // The functor object will be executed in a separate thread
 
    // Wait for the thread to finish, this is a blocking operation
    thread.join();
 
    return 0;
}

// 编译方法: g++ -g test.cpp -o test -std=c++11 -lpthread

1.1.4 用Lambda表达式创建线程

#include <iostream>
#include <thread>

class Test
{
public:
    Test(int m) : m_iCount(m) {}
    ~Test() {}

    void init() {
        // 用Lambda表达式创建线程。this是该匿名函数捕获的外部参数。()为空,表示该匿名函数没有参数
        std::thread([this]() {
            std::cout << "m_iCount = " << m_iCount << std::endl;
        }).join();
    }
private:
    int m_iCount;
};
 
int main()
{
    // 方法1:
    auto lambda = [](int a) { std::cout << a << std::endl; };

    // Create and execute the thread
    std::thread thread(lambda, 10); // Pass 10 to the lambda expression
 
    // The lambda expression will be executed in a separate thread
 
    // Wait for the thread to finish, this is a blocking operation
    thread.join();

    // 方法2:在一个类中用匿名函数创建线程
    Test test(5);
    test.init();
 
    return 0;
}

// 编译方法: g++ -g test.cpp -o test -std=c++11 -lpthread

1.2 pthread创建线程

int pthread_create (pthread_t *__restrict __newthread,
			   const pthread_attr_t *__restrict __attr,
			   void *(*__start_routine) (void *),
			   void *__restrict __arg)
// 第一个参数为指向线程标识符的指针。
// 第二个参数用来设置线程属性。
// 第三个参数是线程运行函数的起始地址。
// 第四个参数是运行函数的参数。
// pthread_create() 在调用成功完成之后返回零。其他任何返回值都表示出现了错误。

实例:

#include <iostream>
#include <pthread.h>

void* test(void* context)
{
    std::cout << "hello, world" << std::endl;
}

int main()
{

    pthread_t tid;
    int ret = pthread_create(&tid, NULL, test, NULL);

    pthread_join(tid, NULL);

    return 0;

}

// 编译方法: g++ -g test.cpp -o test -lpthread

来自于
https://zhuanlan.zhihu.com/p/358428937

标签:std,销毁,1.1,创建,void,线程,include
来源: https://www.cnblogs.com/vivian187/p/15959504.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有