C++ std::thread 和 std::mutex 应用程序崩溃(程序异常终止)

问题描述 投票:0回答:0

我有一个 C++Builder VCL DLL 项目,我的窗体上有一个计时器。计时器调用一个函数,该函数创建一个新的

std::thread
(分离),而
thread_function
调用其他函数并使用全局变量做一些事情。

问题是程序崩溃,报错“程序异常终止”。在我使用

std::thread
(我是直接调用函数)之前,它运行良好,但 GUI 挂起。现在,我正在使用
std::thread
,但程序崩溃了。我不确定,但也许是全局变量导致了这个问题?

基本上,我的代码是这样的:

Form1.h

typedef struct{
    int x=0;
    int y=0;
    int z=0;
    int k=0;
    int j=0;
}nodeData;

std::vector<nodeData> nodeDataList;
//defined here because im using this on Form2 too
//pushing elements in Form1_Create event

Form1.cpp

int a=0;
int b=0;
AnsiString c;

bool ProcessNodeX(){
    c="processing x..";
    for (int i=0; i < Form1->ListBox1->Items->Count; i++) {
        if (a<12){b++;}
        if (Form1->nodeDataList.at(i).x>24){b++;}
        Form1->nodeDataList.at(0).k=15
    }
    Form1->Timer1->Interval=1000;
    return true;
}

bool ProcessNodeY(){
    c="processing y..";
    for (int i=0; i < Form1->ListBox1->Items->Count; i++) {
        if (a<6){b++; Form1->Timer1->Interval=1500;}
        if (Form1->nodeDataList.at(i).y>12){b++;}
        Form1->nodeDataList.at(0).k=20
    }
    return true;
}

bool ProcessNodeZ(){
    c="processing z..";
    for (int i=0; i < Form1->ListBox1->Items->Count; i++) {
        if (a<18){b++; Form1->Timer1->Interval=3000;}
        if (Form1->nodeDataList.at(i).z>16){b++;}
        Form1->nodeDataList.at(0).k=30
    }
    return true;
}

void RelocateNodes(){
    int _local_val=0;
    if (ProcessNodeX()){_local_val++}
    if (ProcessNodeY()){_local_val++}
    if (ProcessNodeZ()){_local_val++}

    a++;
    if(b<a){
        Form1->nodeDataList.at(0).k=15;
        Form1->Timer1->Enabled=true;
        return;
    }
    else{
        Form1->nodeDataList.at(0).k=30;
        Form1->Timer1->Enabled=true;
        return;
    }

    if(Form1->nodeDataList.at(0).k<20){
        Form1->nodeDataList.at(0).l=50;
        Form1->Timer1->Enabled=true;
        return;
    }

    Form1->nodeDataList.at(0).l=99;
    Form1->Timer1->Enabled=true;
    return;
}

void LaunchRelocate(){
    std::thread rlc(RelocateNodes);
    rlc.detach();
}

Timer1_Event(TObject *Sender){
    Form1->Timer1->Enabled=false;
    LaunchRelocate();
}

然后我尝试使用互斥锁/锁,像这样:

Form1.h

typedef struct{
    int x=0;
    int y=0;
    int z=0;
    int k=0;
    int j=0;
}nodeData;

std::vector<nodeData> nodeDataList;

Form1.cpp

int a=0;
int b=0;
AnsiString c;
std::mutex psrMutex;

bool ProcessNodeX(){
    std::lock_guard<std::mutex> lock(psrMutex);
    c="processing x..";
    for (int i=0; i < Form1->ListBox1->Items->Count; i++) {
        if (a<12){b++;}
        if (Form1->nodeDataList.at(i).x>24){b++;}
        Form1->nodeDataList.at(0).k=15
    }
    Form1->Timer1->Interval=1000;
    return true;
}

bool ProcessNodeY(){
    std::lock_guard<std::mutex> lock(psrMutex);
    c="processing y..";
    for (int i=0; i < Form1->ListBox1->Items->Count; i++) {
        if (a<6){b++; Form1->Timer1->Interval=1500;}
        if (Form1->nodeDataList.at(i).y>12){b++;}
        Form1->nodeDataList.at(0).k=20
    }
    return true;
}

bool ProcessNodeZ(){
    std::lock_guard<std::mutex> lock(psrMutex);
    c="processing z..";
    for (int i=0; i < Form1->ListBox1->Items->Count; i++) {
        if (a<18){b++; Form1->Timer1->Interval=3000;}
        if (Form1->nodeDataList.at(i).z>16){b++;}
        Form1->nodeDataList.at(0).k=30
    }
    return true;
}

void RelocateNodes(){
    int _local_val=0;
    if (ProcessNodeX()){_local_val++}
    if (ProcessNodeY()){_local_val++}
    if (ProcessNodeZ()){_local_val++}

    psrMutex.lock();
    a++;
    if(b<a){
        Form1->nodeDataList.at(0).k=15;
        Form1->Timer1->Enabled=true;
        psrMutex.unlock();
        return;
    }
    else{
        Form1->nodeDataList.at(0).k=30;
        Form1->Timer1->Enabled=true;
        psrMutex.unlock();
        return;
    }

    if(Form1->nodeDataList.at(0).k<20){
        Form1->nodeDataList.at(0).l=50;
        Form1->Timer1->Enabled=true;
        psrMutex.unlock();
        return;
    }

    Form1->nodeDataList.at(0).l=99;
    Form1->Timer1->Enabled=true;
    psrMutex.unlock();
    return;
}

void LaunchRelocate(){
    std::thread rlc(RelocateNodes);
    rlc.detach();
}

Timer1_Event(TObject *Sender){
    Form1->Timer1->Enabled=false;
    LaunchRelocate();
}

我不确定,我是否以正确的方式使用了

mutex
?它仍然随机崩溃(有时当我启动计时器时它会立即崩溃,有时它会工作 5-10 分钟)。默认定时器间隔为 2000 毫秒。

multithreading crash mutex c++builder vcl
© www.soinside.com 2019 - 2024. All rights reserved.