1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Problem in Threads in C++.

Discussion in 'C++' started by hrdksanghavi, Jul 13, 2011.

  1. hrdksanghavi

    hrdksanghavi New Member

    Joined:
    Jun 6, 2011
    Messages:
    6
    Likes Received:
    2
    Trophy Points:
    0
    Hello, I m facing problem while adding thread in my class. But if I add thread to my main function than it's working fine.

    Code that is working fine is
    Code:
    #include <cstdlib>
    #include <stdio.h>
    #include "cDqu.h"
    #include "cEnq.h"
    #include <pthread.h>
    
    void *Fun4Dequeu(void *threadid);
    
    main()
    {
        pthread_t thdMain;
        int t=1;
        pthread_create( &thdMain, NULL, Fun4Dequeu, (void *)t);
        pthread_join( thdMain, NULL);
        exit(0);
    }
    void *Fun4Dequeu(void *threadid)
    {
        for(;;)
        {
            //Some code;
        }
    }
    
    
    But when I am trying to add a thread in my class then it gives me error

    Code:
    cDqu.h:36: error: argument of type ‘void (cDequeue::)(void*)’ does not match ‘void* (*)(void*)’
    cDqu.h: At global scope:
    cDqu.h:40: error: cannot declare pointer to ‘void’ member
    
    the code for the class is
    Code:
    #include <cstdlib>
    #include <fstream>
    #include <time.h>
    #include <ctime>
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #include "cEnq.h"
    using namespace std;
    #ifndef CDQU_H
    #define    CDQU_H
    
    std::ofstream file("output.txt", std::ios_base::out);
    class cDequeue : public cEnqueue
    {
    public:
        pthread_t thdDqueue;
        
        int iThdRet;
        cDequeue();
        void StartThread2();
        void FunGetToken();
        void funDequeue(void *threadid);
       
    };
    void cDequeue::StartThread2()
    {
        int t=1;
        pthread_create(&thdDqueue,NULL,funDequeue,(void *)t);
        /*above line giving problem*/
        pthread_join(thdDqueue,NULL);
             
    }
        /*below line giving problem*/
    void cDequeue::*funDequeue(void *threadid)
    {
        cEnqueue objEnq;
        int Token=0;
        if(objEnq.qToken.empty() == false)
        {
            Token = objEnq.qToken.front();
            objEnq.qToken.pop();
        }
            
    }
    void cDequeue::FunGetToken()
    {
            int Token=0; 
            Token = FunDequeue();
            if(Token !=0)
                    file<<"\tToken:"<<Token<<endl;
            else
            {
                    file<<"\tQueue Empty!!!"<<endl;
            }
    }
    #endif    /* CDQU_H */
    
    Please sugesst me for the same.

    Regards,
    Hardik
     
  2. priyatendulkar

    priyatendulkar New Member

    Joined:
    Jun 20, 2011
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    Hi,

    U cannot pass a non static member function to pthread_create as non static member function includes an implicit "this" pointer.
    U can consider following two solutions depending on your requirements.
    1. Declare funDequeue() as static function.(No this pointer in case of static functions)
    2. Write a helper function(non member ) which takes object of class cDequeue as parameter and then calls function funDequeue().
    ie
    void * helper(void * Obj)
    {
    cDequeue * ptr = (cDequeue *) Obj;
    ptr -> funDequeue();

    }
     

Share This Page