0

This is a subsequent question to this thread.

I pass a struct ...

struct threadDirectives_t {
    int block;
    medianfilter mFilter;
};

... to my thread's method computeSignalBlock. However, it does not output any text. Neither with cout, nor with printf.

I only get this console output:

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

Why is that?

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.
}

Building the project completes with warnings.

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)

What am I missing?

Community
  • 1
  • 1
feder
  • 1,849
  • 2
  • 25
  • 43
  • 4
    Since you are using C++11 please consider to use std::thread. – sfrehse Oct 23 '14 at 11:17
  • 2
    The warning text says it well: you did not have a return statement in `medianfilter::computeSignalBlock` which is declared to return a pointer-to-void. – Alexey Kukanov Oct 23 '14 at 11:20
  • 3
    You probably required `pthread_join` since other the program terminates without completing the thread job. – sfrehse Oct 23 '14 at 11:23
  • 1
    After @AlexeyKukanov comment you should probably edit your code and then share with us your result after recompiling. –  Oct 23 '14 at 11:32
  • @sfrehse True, Failed to wait. Please post as an answer and I'll accept. Thanks to all for the extra hints! – feder Oct 23 '14 at 11:39

1 Answers1

1

You have forgot to call pthread_join. The question is also related to pthread - How to start running a new thread without calling join?

Community
  • 1
  • 1
sfrehse
  • 1,062
  • 9
  • 22