当启动boost :: thread时,.exe会崩溃

时间:2015-07-17 00:36:04

标签: boost-bind

这是我的功能:

void cmdChangeSett(cmdbuf* cmd_buffer, CTimeTag tagger, uint8_t chNum, int mask) {

double* oldChannelvoltage = new double[chNum];
double* newChannelvoltage = new double[chNum];
bool* oldEdge = new bool[chNum];
bool* newEdge = new bool[chNum];
int newmask;
double chDiff;
int edgeDiff;
int i;

while (runAcquisition) {

    for (i = 0; i < chNum; i++) {
        cmd_getThresh_getEdge(cmd_buffer, i, oldChannelvoltage, oldEdge);

    }

    Sleep(500);

    newmask = 0;
    for (i = 0; i < chNum; i++) {
        cmd_getThresh_getEdge(cmd_buffer, i, newChannelvoltage, newEdge);
        chDiff = oldChannelvoltage[i] - newChannelvoltage[i];
        edgeDiff = oldEdge[i] - newEdge[i];
        //printf("\nOld: %.2f, New: %.2f -> DIFF = %.2f", oldChannelvoltage[i], newChannelvoltage[i], diff);

        if (chDiff != 0) {

            WARN(newChannelvoltage[i] > 1.5, newChannelvoltage[i] = 1.5f, "Threshold of %.2fV exceeds channel %i's max. Rounding to %.2fV.", newChannelvoltage[i], i + 1, 1.5);
            WARN(newChannelvoltage[i] < -1.5, newChannelvoltage[i] = -1.5f, "Threshold of %.2fV exceeds channel %i's max. Rounding to %.2fV.", newChannelvoltage[i], i + 1, -1.5);
            tagger.SetInputThreshold(i + 1, newChannelvoltage[i]);

        }

        if (edgeDiff) {
            if (!newEdge[i]) newmask += 1 << i;
        }
    }

    if (newmask != mask) {
        tagger.SetInversionMask(newmask);
        mask = newmask;
    }

}

delete[] oldChannelvoltage;
delete[] newChannelvoltage;
delete[] oldEdge;
delete[] newEdge;
}

当我从main()启动线程时崩溃:

    int main(int argc, char** argv) {
      int mask = 0;             
      cmdbuf* cmd_buffer;
      CTimeTag tagger;

      //some code ....
      //......

      boost::function<void()> cmdChangeSettThread = boost::bind(&cmdChangeSett,cmd_buffer, tagger, 16, mask);
      boost::thread th(cmdChangeSettThread);
      //some other code ...
      return 0;
      }

任何想法? 我认为问题是由我在函数中使用的数组引起的,但我无法弄清楚如何解决问题。 非常感谢你!

2 个答案:

答案 0 :(得分:2)

你需要等待线程在main中完成。

如果调用线程析构函数并且线程仍在运行,则调用terminate()。

  th.join();  // should stop the application crashing.
  return 0;
  }

PS。这些都不好:

double* oldChannelvoltage = new double[chNum];
double* newChannelvoltage = new double[chNum];
bool* oldEdge = new bool[chNum];
bool* newEdge = new bool[chNum];

使用矢量(或数组)。

获取代码审核:http://codereview.stackexchange.com

答案 1 :(得分:0)

谢谢大家!我发现了问题!!我愚蠢地通过值愚蠢地传递了对象CTimeTag tagger,如果我在提出问题时不是很清楚,我很抱歉! 所以现在函数定义是:

void cmdChangeSett(cmdbuf* cmd_buffer, CTimeTag *tagger, tt_buf* buffer, uint8_t chNum, int mask) 

当我用boost :: bind调用它时,我有:

boost::function<void()> cmdChangeSettThread = boost::bind(&cmdChangeSett,cmd_buffer, &tagger, buffer, 16, mask);

再次感谢你!