吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 359|回复: 6
收起左侧

[求助] Linux C,怎么在主线程里把已经分离的子线程关闭掉

[复制链接]
mo1230 发表于 2024-3-22 22:06
本帖最后由 mo1230 于 2024-4-3 11:09 编辑

子线程里在循环执行操作,想要在主线程中关闭这个已经处于分离状态的线程。
因为传入的参数是指针,所以想着通过改变这个指针指向的值,从而去关闭子线程。但是失败了,关闭不了,但是通过pthread_kill函数查看线程的状态,却显示线程已经不存在了。。。

这该怎么操作,才能在主线程里关掉子线程。

#include <iostream>
#include <pthread.h>
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"

struct data
{
    bool* run;
    int* num;
};

void* thread_func(void *arg)
{
    cv::Mat img;
    cv::VideoCapture cap(0);
    struct data* datas = (struct data*)arg;
    int i = *(datas->num);
    for (;;)
    {
        i++;
        cap >> img;
        if (img.empty())
        {
            std::cerr << "img is empty..." << std::endl;
            continue;
        }

        if (*(datas->run))
        {
            std::cout << "thread_func..." << i << img.size() << std::endl;
        }else
        {
            std::cout << "thread_func...end..." << std::endl;
            break;
        }

    }

}

int run()
{
    bool running = true;
    int i = 0;
    pthread_t t_pid;

    struct data data_s;
    data_s.run = &running;
    data_s.num = &i;

    pthread_create(&t_pid, NULL, thread_func, &data_s);
    pthread_detach(t_pid);
    _sleep(2000);
    while (running)
    {
        i++;
        std::cout << "run ..." << i << std::endl;
        _sleep(100);

        if (i > 100)
        {
            running = false;
            pthread_join(t_pid, NULL);
            // std::cout << "arg: " << *arg << std::endl;
            // pthread_cancel(t_pid);
            // pthread_join(t_pid, NULL);
            // _sleep(0);

            int state = pthread_kill(t_pid, 0);
            if (state == ESRCH)
            {
                std::cout << "thread is not exit..." << std::endl;
            }else if (state == EINVAL)
            {
                std::cout << "thread is invalid..." << std::endl;
            }else
            {
                std::cout << "thread is alive..." << std::endl;
            }

            return -1;
        }

    }

}

int main()
{
    run();
    std::cout << "main...end..." << std::endl;
    while (1)
    {
                        // 其他操作
                }

    return 0;    
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Paranoid丶 发表于 2024-3-23 00:01
我常用的做法是,子线程的while判断退出标志,然后主线程需要关子线程的时候修改标志,然后join
 楼主| mo1230 发表于 2024-3-23 01:22
Paranoid丶 发表于 2024-3-23 00:01
我常用的做法是,子线程的while判断退出标志,然后主线程需要关子线程的时候修改标志,然后join

我好像知道我错在哪儿了,不该pthread_detach。。。谢谢啦
ZMWAWOOPP 发表于 2024-3-23 13:28
Paranoid丶 发表于 2024-3-23 00:01
我常用的做法是,子线程的while判断退出标志,然后主线程需要关子线程的时候修改标志,然后join

谢谢楼主分享!!!!!!!!!
testpapa 发表于 2024-3-30 17:59
写法有问题的, pthread_detach 以后就不能在使用 pthread_join 来等待线程结束了。pthread_detach() 就表示这个线程分离了。
 楼主| mo1230 发表于 2024-3-31 10:30
testpapa 发表于 2024-3-30 17:59
写法有问题的, pthread_detach 以后就不能在使用 pthread_join 来等待线程结束了。pthread_detach() 就表 ...

嗯嗯谢谢,是我理解有问题。。。被绕晕了。
 楼主| mo1230 发表于 2024-3-31 10:32
mo1230 发表于 2024-3-31 10:30
嗯嗯谢谢,是我理解有问题。。。被绕晕了。

不过话说,分离后的线程,主线程是不是就没法控制那个子线程结束了。。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-1 15:02

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表