【问题标题】:Why are methods in thread not running? [closed]为什么线程中的方法没有运行? [关闭]
【发布时间】:2014-10-23 11:12:25
【问题描述】:

这是thread 的后续问题。

我传递了一个结构...

struct threadDirectives_t {
    int block;
    medianfilter mFilter;
};

...我线程的方法computeSignalBlock。但是,它不输出任何文本。既不是cout,也不是printf

我只得到这个控制台输出:

Start computing filtered results.
Started thread 0.
Started thread 1.

这是为什么呢?

void medianfilter::computeFilteredResults(vector<float> _signals, int _nThreads,
        int _windowSize, bool _isParallel) {

    printf("Start computing filtered results.\n");

    // Set instanz variables.
    originalSignalVector = _signals;
    nThreads = _nThreads;
    windowSize = _windowSize;
    isParallel = _isParallel;

    // Create the threads,
    pthread_t threads[nThreads];
    threadDirectives_t threadDirectivesArr[nThreads];
    pthread_attr_t attr;
    // Initialize and set thread joinable
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    int threatResponse;

    for (int i = 0; i < nThreads; i++) {
        threadDirectives_t iTD;
        iTD.block = i;
        iTD.mFilter = *this;
        threadDirectivesArr[i] = iTD;

        threatResponse = pthread_create(&threads[i], NULL, computeSignalBlock,
                (void *) &threadDirectivesArr[i]);
        if (threatResponse) {
            printf("Error: unable to create thread: %d\n", threatResponse);
            exit(-1);
        }
        printf("Started thread %d.\n",  i);
    }

}

void *medianfilter::computeSignalBlock(void *threadDirectives) {
    threadDirectives_t* thisPtr =
            reinterpret_cast<threadDirectives_t*>(threadDirectives);

    cout << "test";
    //TODO I need  access to instance variables!!
    // 1) Get my block to compute.
    printf("This is computation block # %d\n", thisPtr->block);
    // 2) Compute block.
}

项目构建完成并出现警告。

12:55:39 **** Incremental Build of configuration Debug for project SSExercise3 ****
make all 
Building file: ../src/medianfilter.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -pthread -MMD -MP -MF"src/medianfilter.d" -MT"src/medianfilter.d" -o "src/medianfilter.o" "../src/medianfilter.cpp"
../src/medianfilter.cpp: In static member function ‘static void* medianfilter::computeSignalBlock(void*)’:
../src/medianfilter.cpp:79:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
Finished building: ../src/medianfilter.cpp

Building target: SSExercise3
Invoking: GCC C++ Linker
g++ -pthread -o "SSExercise3"  ./src/SSExercise3.o ./src/medianfilter.o ./src/reader.o   
Finished building target: SSExercise3


12:55:40 Build Finished (took 1s.204ms)

我错过了什么?

【问题讨论】:

  • 由于您使用的是 C++11,请考虑使用 std::thread。
  • 警告文本说得很好:medianfilter::computeSignalBlock 中没有声明返回指向 void 的指针的 return 语句。
  • 您可能需要pthread_join,因为其他程序在未完成线程作业的情况下终止。
  • 在@AlexeyKukanov 发表评论后,您可能应该编辑您的代码,然后在重新编译后与我们分享您的结果。
  • @sfrehse 是的,等待失败。请张贴作为答案,我会接受。感谢大家的额外提示!

标签: c++ multithreading


【解决方案1】:

您忘记致电pthread_join。该问题也与pthread - How to start running a new thread without calling join?有关

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多