Search code examples
c++pthreadspthread-join

How to initialized pthread_t variable in class constructor


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)

Solution

  • 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;
    }