【Linux】线程的控制

内核中没有很明确线程的概念,但是存在轻量级进程的概念。所以操作系统不会直接提供线程的系统调用,只会提供轻量级进程的系统调用。

        用户使用线程的接口,只能通过应用层调用pthread线程库,这个库对轻量级进程接口进行封装,为用户直接创建或者控制线程的接口,几乎所有的Linux平台都是默认自带这个线程库的。在Linux中编写多线程代码需要使用第三方库pthread库。

线程的接口

POSIX线程库

  • 与线程有关的函数构成了一个完整的系列,绝大多数函数的名字都是以“pthread_t”打头的
  • 要使用这些函数库,要通过引入头文件<pthread.h>
  • 链接这些线程函数库时要使用编译器命令的“-lpthread”选项

创建新线程pthread_create

  • 第一个参数pthread_t *thread是一个输出型参数,代表线程的thread id
  • 第二个参数const pthread_attr_t *attr 代表线程的属性,一般设置成nullptr。
  • 第三个参数void *(*start_routine) (void *)是一个函数指针,返回值为void*,参数是(void*),线程启动后要执行的函数
  • 第四个参数void *arg是输入型参数,创建线程成功,新线程回调线程函数的时候需要参数,这个参数是传给线程启动函数的参数。

如果创建线程成功,返回值是0;如果创建线程失败,会返回值错误码非0。

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

using namespace std;

// new thread
void* threadRoutine(void* args)
{
    while(true)
    {
        cout << "new thread, pid: " << getpid() << endl;
        sleep(2);
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, nullptr);

    while(true)
    {
        cout << "main thread, pid: " << getpid() << endl;
        sleep(1);
    }
    return 0;
}
mythread:mythread.cc
	g++ -o $@ $^ -lpthread -std=c++11
.PHONY:clean
clean:
	rm -f mythread

在查看进程时只能查看到一个进程。

指令:ps -aL

【功能】-L选项查看当前用户所启用的轻量级进程

LWP(light weight process):代表轻量级进程的pid

【注意】主线程的PID与LWP相同,当使用信号kill杀掉线程时,不管选择主线程还是其他线程都会杀死进程,即任何一个进程被杀死,所有的线程都会被杀死。

现象:同一个函数可以被多个执行流执行

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

using namespace std;

// 可以被多个执行流执行
void show(const string& name)
{
    cout << name << "say#" << "hello thread" << endl;
}

// new thread
void* threadRoutine(void* args)
{
    while(true)
    {
        show("[new thread]");
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, nullptr);

    while(true)
    {
        show("[main thread]");
        sleep(1);
    }
    return 0;
}

测试主线程与其他线程的全局变量的变化:

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

using namespace std;

int g_val = 100;

// new thread
void* threadRoutine(void* args)
{
    while(true)
    {
        printf("new thread pid: %d, g_val: %d, &g_val: 0x%p\n", getpid(), g_val, &g_val);
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, nullptr);

    while(true)
    {
        printf("main thread pid: %d, g_val: %d, &g_val: 0x%p\n", getpid(), g_val, &g_val);
        sleep(1);
        g_val++;
    }
    return 0;
}

线程之间通信是非常容易的。

【注意】当一个线程出现异常(例如除零异常),整个进程中的所有线程都会崩掉。

实验:传递pthread_create的第四个参数

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

using namespace std;

int g_val = 100;

void* threadRoutine(void* args)
{
    const char *name = (const char*)args;
    while(true)
    {
        printf("%s, pid: %d, g_val: %d, &g_val: 0x%p\n", name, getpid(), g_val, &g_val);
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void*)"Thread 1");

    while(true)
    {
        printf("main thread pid: %d, g_val: %d, &g_val: 0x%p\n", getpid(), g_val, &g_val);
        sleep(1);
        g_val++;
    }
    return 0;
}

线程的等待pthread_join

  • 新线程创建处理后退出,其空间是没有被释放的,仍然在进程的地址空间内,造成内存泄漏
  • 如果需要,可以获取到线程的退出结果

功能:等待线程结束

原型:int pthread_join(pthread_t thread, void **value_ptr);

参数:

  • thread:线程ID
  • value_ptr:二级指针,指向一个指针,后者指向线程的返回值

返回值:成功返回0;失败返回错误码

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

using namespace std;

int g_val = 100;

// new thread
void* threadRoutine(void* args)
{
    const char *name = (const char*)args;
    int cnt = 5;
    while(true)
    {
        printf("%s, pid: %d, g_val: %d, &g_val: 0x%p\n", name, getpid(), g_val, &g_val);
        sleep(1);
        cnt--;
        if(cnt == 0) break;
    }

    // 到这里默认线程退出
    return nullptr;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void*)"Thread 1");

    // wait thread
    pthread_join(tid, nullptr);

    cout << "main thread quit..." << endl;

    return 0;
}

【注意】主线程等待的时候,默认是阻塞等待的。

一个线程退出,其退出结果是由其返回值来反映的。

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

using namespace std;

int g_val = 100;

// new thread
void* threadRoutine(void* args)
{
    const char *name = (const char*)args;
    int cnt = 5;
    while(true)
    {
        printf("%s, pid: %d, g_val: %d, &g_val: 0x%p\n", name, getpid(), g_val, &g_val);
        // cout << "new thread, pid: " << getpid()  << endl;
        // show("[new thread]");
        sleep(1);
        cnt--;
        if(cnt == 0) break;
    }

    // 到这里默认线程退出
    return (void*)1;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void*)"Thread 1");

    // wait thread
    void *ret;
    pthread_join(tid, &ret);

    cout << "main thread quit..." << (long long int)ret << endl;

    return 0;
}

线程结束后,为什么不需要考虑异常情况?是因为线程做不到,线程一旦出异常,进程整体结,线程只需要考虑正常情况即可。

终止线程pthread_exit

【注意】在线程中调用exit,不仅仅会结束该线程,还会将整个进程都给结束。exit是用来终止进程的,不能用来终止线程。

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

using namespace std;

int g_val = 100;

// new thread
void* threadRoutine(void* args)
{
    const char *name = (const char*)args;
    int cnt = 5;
    while(true)
    {
        printf("%s, pid: %d, g_val: %d, &g_val: 0x%p\n", name, getpid(), g_val, &g_val);
        // cout << "new thread, pid: " << getpid()  << endl;
        // show("[new thread]");
        sleep(1);
        cnt--;
        if(cnt == 0) break;
    }

    // 到这里默认线程退出
    // return (void*)1;
    pthread_exit((void*)100);
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void*)"Thread 1");

    // wait thread
    void *ret;
    pthread_join(tid, &ret);

    cout << "main thread quit..." << (long long int)ret << endl;

    return 0;
}

当线程退出时,可以使用return或者pthread_exit可以仅仅退出当前线程。

线程的取消pthread_cancel

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

using namespace std;

int g_val = 100;

// 可以被多个执行流执行
void show(const string& name)
{
    cout << name << "say#" << "hello thread" << endl;
}

// new thread
void* threadRoutine(void* args)
{
    const char *name = (const char*)args;
    int cnt = 5;
    while(true)
    {
        printf("%s, pid: %d, g_val: %d, &g_val: 0x%p\n", name, getpid(), g_val, &g_val);
        // cout << "new thread, pid: " << getpid()  << endl;
        // show("[new thread]");
        sleep(1);
        cnt--;
        if(cnt == 0) break;
    }

    // 到这里默认线程退出
    // return (void*)1;
    pthread_exit((void*)100);
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void*)"Thread 1");

    sleep(1);// 确保新线程启动
    pthread_cancel(tid); //在主线程取消threadRoutine线程
    // wait thread
    void *ret;
    pthread_join(tid, &ret);

    cout << "main thread quit..." << (long long int)ret << endl;

    return 0;
}

使用pthread_cancel退出时,退出结果是PTHREAD_CANCELED(-1).

  1. 如果thread线程通过return返回,value_ptr所指向的单元里存放的是thread线程函数的返回值。
  2. 如果thread线程被别的线程调用pthread_cancel异常终止,value_ptr所指向的单元里存放的是常数PTHREAD_CANCELED
  3. 如果thread线程是自己调用pthread_exit终止的,value_ptr所指向的单元存放的是传给pthread_exit的参数。
  4. 如果对thread线程的终止状态不感兴趣,可以传NULL给value_ptr参数。

使用线程处理数字加和

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

using namespace std;

class Request
{
public:
    Request(int start, int end, const string& threadname)
    :_start(start), _end(end), _threadname(threadname)
    {}
public:
    int _start;
    int _end;
    string _threadname;
};

class Response
{
public:
    Response(int result, int exitcode)
    :_result(result), _exitcode(exitcode)
    {}
public:
    int _result; // 计算结果
    int _exitcode; // 计算结果是否有效
};

void* sumCount(void* args)
{
    Request *rq = static_cast<Request*>(args); // (Request*)(args)
    Response *rsp = new Response(0, 0);
    for(int i = rq->_start; i <= rq->_end; ++i)
    {
        rsp->_result += i;
    }
    delete rq;
    return rsp;
}

int main()
{
    pthread_t tid;
    Request *rq = new Request(1, 100, "thread 1");
    pthread_create(&tid, nullptr, sumCount, rq);

    void* ret;
    pthread_join(tid, &ret);
    Response *rsp = static_cast<Response*>(ret);

    cout << "rsp->_result: " << rsp->_result << ", exitcode: " << rsp->_exitcode << endl;
    delete rsp;

    return 0;
}

线程的参数与返回值不知可以传递一般参数,也可以传递对象。

C++11中线程的接口

上述所使用的都是原生线程(pthread线程),即原生线程库。在C++11中也支持多线程。

#include <iostream>
#include <thread>
#include <string>
#include <unistd.h>

using namespace std;

void threadrun()
{
    while(true)
    {
        cout << " I am a new thread for c++" << endl;
        sleep(1);
    }
}

int main()
{
    // 创建线程
    thread t1(threadrun);
    // 等待线程
    t1.join();

    return 0;
}
mythread:mythread.cc
	g++ -o $@ $^ -std=c++11 -lpthread
.PHONY:clean
clean:
	rm -f mythread

查看自身线程pthread_self

#include <iostream>
#include <pthread.h>
#include <cstdlib>
#include <string>
#include <unistd.h>

using namespace std;

string tohex(pthread_t tid)
{
    char hex[64];
    snprintf(hex, sizeof(hex), "%p", tid);
    return hex;
}

void* threadRoutine(void* args)
{
    while(true)
    {
        sleep(1);
        cout << "thread id: " << tohex(pthread_self()) << endl;
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void*)"thread 1");

    
    cout << "main thread create thread done, new thread id: " << tohex(tid) << endl;

    pthread_join(tid, nullptr);
    return 0;
}

创建轻量级进程clone

clone是用来创建轻量级进程的函数,第一个参数被线程库封装。也就是说线程的概念是由线程库来维护的,当使用原生线程库时,该线程库也是需要加载到内存里的。

  • 线程库是要维护线程概念——不用维护线程的执行流
  • 线程库注定需要维护多个线程属性集合,线程库管理这些线程(先描述后组织)

【注意】每一个线程的库级别的tcb的起始地址叫做线程的tid。

每一个线程都有独立的调用链,也就说明了每一个线程都有独立的调用链的栈结构。主线程(main函数)使用主线程栈,所有其他的非主线程栈都会被维护在库的结构中,即共享区维护,也就是在pthread库中,tid指向的用户tcb中。

线程ID及进程地址空间布局

  • pthread_create函数会产生一个线程ID,存放在第一个参数指向的地址中。该线程ID和前面说的线程ID不是一回事。

之前所谈的线程ID属于进程调度的范畴。因为线程是轻量级进程,是操作系统调度器的最小单位,所有需要一个数值来唯一标识该线程。

  • pthread_create函数第一个参数指向一个虚拟内存单元,该内存单元的地址即为新创建线程的线程ID,属于NPTL线程库(原生线程库)的范畴。线程库的后续操作就是根据该线程ID来操作线程的。
  • 线程库NPTL提供了pthread_self函数,可以获得线程自身的ID。

示例:创建多个线程

#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <vector>
#include <string>

using namespace std;

#define NUM 10

struct threadDate
{
    string tid;
    string threadname;
};

void* threadPoutine(void* args)
{
    threadDate* td = static_cast<threadDate*>(args);
    int i = 0;
    while(i < 10)
    {
        cout << "pid : " << getpid() << ", tid : " << td->tid << ", threadname: " << td->threadname << endl;
        sleep(1);
        i++;
    }
    delete td;
    return nullptr;
}

void InitThreaddate(threadDate* td, int number, pthread_t tid)
{
    td->threadname = "thread-" + to_string(number);
    char buffer[128];
    snprintf(buffer, sizeof(buffer), "0x%x", tid);
    td->tid = buffer;
}

int main()
{
    vector<pthread_t> tids;
    // 创建多个线程
    for(int i = 0; i < NUM; ++i)
    {
        pthread_t tid;
        threadDate *td = new threadDate;
        InitThreaddate(td, i, tid);
        pthread_create(&tid, nullptr, threadPoutine, td);
        tids.push_back(tid);
        sleep(1);
    }

    // 等待线程
    for(int i = 0; i < tids.size(); ++i)
    {
        pthread_join(tids[i], nullptr);
    }

    return 0;
}

每一个线程都具备独立的栈结构,且线程与线程之间几乎是没有密码的,线程的栈上的数据也是可以被其他线程所访问的。

【问题】如果需要设置一个私有的仅仅线程可见的全局变量

__thread p_val = 100;

这种技术称为线程的局部存储,且__thread 只能用来局部存储内置类型,自定义不能用__修饰。

分离线程pthread_detach

  • 默认情况下,新创建的线程是joinable的,线程退出后,需要对其进行pthread_join操作,否则无法释放资源,从而造成系统泄漏。
  • 如果不关心线程的返回值,join是一种负担,这个时候可以通知线程,当线程退出时,自动释放线程资源。

pthread_detach(tid);

可以是线程组内其他线程对目标线程进行分离,也可以是线程自己分离。

pthread_detach(pthread_self());

【注意】joinable和分离是冲突的,一个线程不能既是joinable又是分离的。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值