胧の宝藏之地
首页项目归档照片墙音乐灵境说说杂谈友链关于
封面

线程控制

写作时间:2026-07-21 01:47:21
// TestThread.cc

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

void routine(int cnt)
{
    while(cnt)
    {
        std::cout<<"new thread: " <<cnt <<std::endl;
        cnt--;
        sleep(1);
    }

}

int main()
{
    std::thread t(routine , 10);

    while(true)
    {
        std::cout<<"main thread" <<std::endl;
        sleep(1);
    }

    t.join();

    return 0;
}







// #include<iostream>
// #include<cstdio>
// #include<string>
// #include<pthread.h>
// #include<unistd.h>
// #include"Task.hpp"
// #include<cstdlib>
// #include<ctime>
// #include<vector>
// #include<sys/syscall.h>

// pid_t GetTid()
// {
//     syscall(SYS_gettid);
// }

// // __thread 线程局部存储,只能存储内置类型,常见为整形
// // 作用让不同的线程用相同的变量名,访问不同的内存块,各自访问各种的存储空间
// __thread pid_t id = 0;
// // pid_t id = 0;

// void *routine(void *args)
// {
//     id = GetTid();
//     std::string name = static_cast<const char*>(args);

//     while(true)
//     {
//         // std::cout<<"new thread id: " <<id <<"\n";
//         // printf("new thread id: %p\n" , &id);
//         printf("new thread lwp: %d\n" , id);
//         id++;
//         sleep(1);
//     }

//     return nullptr;
// }

// int main()
// {
//     id = GetTid();
//     pthread_t tid;
//     pthread_create(&tid , nullptr , routine , (void*)"threan-1");

//     while(true)
//     {
//         // std::cout<<"main thread id: " <<id <<std::endl;
//         // printf("main thread id: %p\n" , &id);
//         printf("main thread lwp: %d\n" , id);
//         sleep(1);
//     }

//     pthread_join(tid , nullptr);

//     return 0;
// }







// class Res
// {
// public:
//     int code;
//     std::string name;
//     std::string info;
// };

// void *Routine(void *args)
// {
//     pthread_detach(pthread_self());
//     int cnt = 5;
//     while(cnt)
//     {
//         std::cout<<"新线程运行中:" <<cnt-- <<std::endl;
//         sleep(1);

//         int a = 10;
//         a /= 0;
//     }

//     return nullptr;
//     // Task *t = static_cast<Task*>(args);
//     // t->Excute();
//     // while(true)
//     // {
//     //     sleep(1);
//     // }
//     // return (void *)t;

//     // std::string name = static_cast<const char*>(args);

//     // while(true)
//     // {
//     //     std::cout<<"new thread " <<name <<std::endl;
//     //     sleep(5);
//     //     break;
//     // }
//     // Res *res = new Res();
//     // res->code = 10;
//     // res->name = name;
//     // res->info = "该线程已结束";

//     // return (void*)res;
// }

// int main()
// {
//     pthread_t tid;
//     Task t(10,20);
//     pthread_create(&tid , nullptr , Routine , (void*)&t);
//     // pthread_create(&tid , nullptr , Routine , (void*)"thread-1");
//     // 1.一般需要等待新线程的退出 , 不等待会产生类似僵尸进程的问题
//     // 2.需要获取子线程的执行结果

//     sleep(1);

//     // 当线程被设置为分离时,主线程不能提前退出
//     // std::cout<<"main thread 分离线程" <<std::endl;
//     // pthread_detach(tid);

//     while(true)
//     {
//         sleep(1);
//     }

//     // std::cout<<"取消目标线程" <<std::endl;
//     // pthread_cancel(tid);

//     // Task *task = nullptr;

//     // int n = pthread_join(tid , (void**)&task);
//     // void *ret = nullptr;
//     // int n = pthread_join(tid , (void**)&ret);
//     // if(n == 0)
//     // {

//     //     std::cout<<"join success " <<(long long)ret <<std::endl;
//     //     // std::cout<<"join success " <<task->Result() <<std::endl;
//     // }
//     // else 
//     // {
//     //     std::cout<<"join error: " <<n <<std::endl;
//     // }


//     // Res *retval = nullptr;
//     // 新线程产生异常,进程会全部退出,join失败,不需要关心异常
//     // int n = pthread_join(tid , (void**)&retval);
//     // pthread_join(pthread_t thread , void **ret); 

//     // if(n == 0)
//     // {
//     //     std::cout<<"join success " << retval->code <<std::endl;
//     //     std::cout<<"join success " << retval->name <<std::endl;
//     //     std::cout<<"join success " << retval->info <<std::endl;
//     // }

//     return 0;
// }

// // // 3. 异常问题: 
// // // a.任意线程产生异常崩溃 , 会导致整个进程中断
// // // b.并发问题


// // // 4. 传参问题

// // pthread_t mainid;

// // int g_val = 100; // 2.3 
// // // int *addr = nullptr; //2.4 证明栈区资源共享

// // // 2.2
// // //  线程资源共享 -- 代码共享
// // void PrintfName(const std::string &name)
// // {
// //     printf("新线程: %s , tid: 0x%lx , pid: %d g_val: %d , &g_val: %p\n" ,\
// //          name.c_str() , pthread_self() , getpid(), g_val , &g_val);
// //     // int pthread_self() 返回线程的 tid

// // }

// // // 多执行流同时进入Routine:函数被重入
// // void *Routine(void *args)
// // {
// //     sleep(3);
// //     Task *t =static_cast<Task *>(args); // 4 
// //     t->Excute();
// //     std::cout<<t->Result() <<std::endl;

// //     // // 子线程无法中断主线程
// //     // int n = pthread_cancel(mainid);
// //     // std::cout<<"n ->" <<n <<std::endl;

// //     while(true)
// //     {
// //         sleep(1);
// //         // pthread_exit(nullptr);
// //         // // void pthread_exit(void *value_ptr);
// //         // // value_ptr: 值为进程的 tid  不能指向局部变量
// //         // // 无返回值

// //         // exit(10); // exit 进程退出 
// //     }

// //     return nullptr;

// //     // std::string name = static_cast<const char*>(args);
// //     // printf("-------------------------\n");
// //     // PrintfName(name);

// //     // while(true)
// //     // {
// //         // PrintfName(name); // 2.2
// //         // std::cout<<"新线程..." <<name <<std::endl;
// //         // sleep(1);
// //         // g_val++; // 2.3
// //         // *addr += 10;

// //         // 3
// //         // if(name == "thread-8")
// //         // {
// //         //     std::cout<<"thread-8 say# 产生异常" <<std::endl;
// //         //     int a = 10;
// //         //     a /= 0;
// //         // }
// //     // }
// // }

// // int main()
// // {
// //     mainid = pthread_self();

// //     // 4
// //     srand(time(nullptr) ^ getpid());

// // //     // 2
// // //     int hello = 1; // 2.4
// // //     addr = &hello;

// //     // 创建多线程
// //     const int num = 10;
// //     std::vector<pthread_t> tids;
// //     for(int i = 1 ; i <= num ; i++)
// //     {
// //         pthread_t tid;

// //         // char threadname[64];
// //         // char *threadname = new cahr[64];
// //         // snprintf(threadname , sizeof threadname , "thread-%d" , i+1);

// //         // 4
// //         int x = rand() % 10 + 1;
// //         usleep(131);
// //         int y = rand() % 7 + 1;
// //         Task *t = new Task(x , y);

// //         int n = pthread_create(&tid  , nullptr , Routine , t);
// //         (void)n;
// //         tids.push_back(tid);
// //         // sleep(1);
// //     }

// //     // 1
// //     // pthread_t tid;
// //     // int n = pthread_create(&tid , nullptr , Routine , (void*)"thread-1");
// //         // 1. 创建线程
// //         // int pthread_create(pthread_t *restrict thread thread id
// //         // , const pthread_attr_t *restrict attr nullptr
// //         // , void *(*start routine)(void *) 函数指针 回调
// //         // , void *restrict args) 参数
// //         // 成功返回 0
// //     // (void)n;

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


// //     for(auto &tid : tids)
// //     {
// //         printf("tid : 0x%lx\n" , tid);
// //     }

// //     while(true)
// //     {
// //         // std::cout<<"主线程..." <<std::endl;
// //         printf("主线程 tid: 0x%lx , pid: %d , g_val: %d , &g_val: %p\n" ,\
// //                 pthread_self() , getpid() , g_val , &g_val);

// //         int pos = rand() % tids.size(); // 获取下标
// //         if(pos == -1) continue;
// //         else 
// //         {
// //             pthread_cancel(tids[pos]); //中断子线程
// //             tids[pos] == -1;
// //         }

// //         printf("main thread alive...\n");
// //         sleep(1);
// //     }

// //     return 0;
// // }

‍

//Task.hpp

#pragma once
#include <string>

class Task
{
public:
    Task(int x, int y):_x(x), _y(y)
    {}
    void Excute()
    {
        _reuslt = _x + _y;
    }
    std::string Result()
    {
        return std::to_string(_x) + "+" + std::to_string(_y) + "=" + std::to_string(_reuslt);
    }
    ~Task(){}
private:
    int _x;
    int _y;
    int _reuslt;
};

// struct Task
// {
//     int x;
//     int y;
//     int (*excute)(int x, int y);
// };

‍

#makefile

test_thread : TestThread.cc
    g++ -o $@ $^ -std=c++11
#   g++ -o $@ $^ -lpthread

.PHONY:clean
clean:
    rm -f test_thread

‍

avatar

胧

RECOMMENDED

线程池——单例模式

2026-07-27 22:36:45

进程控制

2026-07-11 20:27:31

线程封装

2026-07-23 13:49:45

Table of Contents