12.线程控制

一.上集回顾

建议先学上篇博客,再向下学习,上篇博客的链接如下:

https://blog.youkuaiyun.com/weixin_60668256/article/details/154530483?fromshare=blogdetail&sharetype=blogdetail&sharerId=154530483&sharerefer=PC&sharesource=weixin_60668256&sharefrom=from_link

二.创建线程

1.线程的创建

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <thread>

void* routine(void* args)
{
    std::string name = static_cast<const char*>(args);
    while(true)
    {
        std::cout << "我们是新线程,我的名字是: " << name << std::endl;
        sleep(1);
    }
    return 0;
}

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

    // std::cout << "new thread id: " << tid << std::endl;
    printf("new thread id: 0x%lx\n",tid);

    while(true)
    {
        std::cout << "我们是main线程..." << std::endl;
        sleep(1);
    }
    return 0;
}

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <thread>

std::string toHex(pthread_t tid)
{
    char buffer[64];
    snprintf(buffer,sizeof(buffer),"0x%lx",tid);
    return buffer;
}

void* routine(void* args)
{
    std::string name = static_cast<const char*>(args);
    while(true)
    {
        std::cout << "我们是新线程,我的名字是: " << name << ", my tid is : " << toHex(pthread_self()) << std::endl;
        sleep(1);
    }
    return 0;
}

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

    // std::cout << "new thread id: " << tid << std::endl;
    printf("new thread id: 0x%lx\n",tid);

    while(true)
    {
        std::cout << "我们是main线程..." << " , my tid is : " << toHex(pthread_self()) << std::endl;
        sleep(1);
    }
    return 0;
}



新线程和main线程谁先运行,不确定(我们要做同步或者互斥操作)

主线程和新线程基本上是瓜分时间片的(10ms  -> 5个线程  ->  每个线程2ms)

2.多线程的创建

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <thread>

std::string toHex(pthread_t tid)
{
    char buffer[64];
    snprintf(buffer,sizeof(buffer),"0x%lx",tid);
    return buffer;
}

void* routine(void* args)
{
    std::string name = static_cast<const char*>(args);
    while(true)
    {
        std::cout << "我们是新线程,我的名字是: " << name << ", my tid is : " << toHex(pthread_self()) << std::endl;
        sleep(1);
    }
    return 0;
}

int main()
{
    pthread_t tid1;
    pthread_create(&tid1,nullptr,routine,(void*)"thread-1");
    pthread_t tid2;
    pthread_create(&tid2,nullptr,routine,(void*)"thread-2");
    pthread_t tid3;
    pthread_create(&tid3,nullptr,routine,(void*)"thread-3");
    pthread_t tid4;
    pthread_create(&tid4,nullptr,routine,(void*)"thread-4");
    
    printf("new thread id: 0x%lx\n",tid1);
    printf("new thread id: 0x%lx\n",tid2);
    printf("new thread id: 0x%lx\n",tid3);
    printf("new thread id: 0x%lx\n",tid4);

    while(true)
    {
        std::cout << "我们是main线程..." << " , my tid is : " << toHex(pthread_self()) << std::endl;
        sleep(1);
    }
    return 0;
}

这里我们的4个线程重入了一个routine函数(这里的cout有问题)

公共资源(显示器)被多个线程访问出现了消息错乱,导致并发问题(所以routine是不可重入函数)

所有的线程都能看到我们任意的一个方法(函数),全部的线程数据共享,代码也是共享的

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <thread>


//5.全局变量在线程内部是共享的
int gval = 100;

std::string toHex(pthread_t tid)
{
    //4.进程内的函数,线程共享
    char buffer[64];
    snprintf(buffer,sizeof(buffer),"0x%lx",tid);
    return buffer;
}

void* routine1(void* args)
{
    std::string name = static_cast<const char*>(args);
    while(true)
    {
        //3.不加保护的情况下,显示器文件就是共享资源
        std::cout << "我们是新线程,我的名字是: " << name << 
        ", my tid is : " << toHex(pthread_self()) 
        << ", 全局变量(修改): " << gval<< std::endl;
        gval++;
        sleep(1);
    }
    return 0;
}

void* routine2(void* args)
{
    std::string name = static_cast<const char*>(args);
    while(true)
    {
        //3.不加保护的情况下,显示器文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值