0

How to initialized pthread_t variable in class constructor.

C++ Static Analysis(coverity) Error: Non-static class member threadId is not initialized in this constructor nor in any functions that it calls.

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>

#include <unistd.h>

void * 
threadFunc(void * arg)
{
    std::cout << "Thread Function :: Start" << std::endl;
    // Sleep for 2 seconds
    sleep(2);
    std::cout << "Thread Function :: End" << std::endl;
    return NULL;
}

class threads
{
    private:
        pthread_t threadId;
        int err;
    public:
        threads():err(0){};
        ~threads(){};
        void create_thread();
        void join_thread();
};

void
threads::create_thread()
{
  // Create a thread that will function threadFunc()
    err = pthread_create(&threadId, NULL, &threadFunc, NULL);
    // Check if thread is created sucessfuly
    if (err)
    {
        std::cout << "Thread creation failed : " << strerror(err);
    }
    else
    {
        std::cout << "Thread Created with ID : " << threadId << std::endl;
    }

}

void
threads::join_thread()
{
   err = pthread_join(threadId, NULL);
    // check if joining is sucessful
    if (err)
    {
        std::cout << "Failed to join Thread : " << strerror(err) << std::endl;
    }
}


int main()
{
    threads T;
    T.create_thread();
    T.join_thread();    
    std::cout << "Exiting Main" << std::endl;
    return 0;
}

Note:

  • I have checked all existing questions and answer in Stackoverflow. But none of them have clear answer.
  • The above c++ code is a sample code(copied from Internet and updated to show my actual problem)
  • 1
    First, If you have c++11 or later, the short answer is.. you don't. You use `std::thread`. Belaying that, add `threadId()` to your member-initializer list (first, maintaining matching order with your member decl order), and let the compiler sort it out from there. – WhozCraig Nov 25 '19 at 05:56
  • I have old c++ compiler and can't use c++11 now. – Chittaranjan Sethi Nov 25 '19 at 06:00
  • @WhozCraig I concur with advice of using std::thread, but there are some occasions where the raw, old skool C API of phtreads has features not reachable from std::thread. Some features can be reached by using a native handle, but not all. And of course, if not using std::thread, portability may be an issue. See e.g. https://stackoverflow.com/questions/24645880/set-cpu-affinity-when-create-a-thread – Erik Alapää Nov 25 '19 at 10:49

1 Answers1

0

I have tried this and its working (Posting the answer, to help others)

#include <iostream>
#include <string.h>
#include <pthread.h>

#include <unistd.h>

void * 
threadFunc(void * arg)
{
    std::cout << "Thread Function :: Start" << std::endl;
    // Sleep for 2 seconds
    sleep(2);
    std::cout << "Thread Function :: End" << std::endl;
    return NULL;
}

class threads
{
    private:
        pthread_t threadId;
        int err;
    public:
        threads():err(0){ threadId = pthread_t(); std::cout <<threadId<<std::endl;};
        ~threads(){};
        void create_thread();
        void join_thread();
};

void
threads::create_thread()
{
  // Create a thread that will function threadFunc()
    err = pthread_create(&threadId, NULL, &threadFunc, NULL);
    // Check if thread is created sucessfuly
    if (err)
    {
        std::cout << "Thread creation failed : " << strerror(err);
    }
    else
    {
        std::cout << "Thread Created with ID : " << threadId << std::endl;
    }

}

void
threads::join_thread()
{
   err = pthread_join(threadId, NULL);
    // check if joining is sucessful
    if (err)
    {
        std::cout << "Failed to join Thread : " << strerror(err) << std::endl;
    }
}


int main()
{
    threads T;
    T.create_thread();
    T.join_thread();    
    std::cout << "Exiting Main" << std::endl;
    return 0;
}