Problem in Threads in C++.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Problem in Threads in C++.

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Posts
    5

    Problem in Threads in C++.

    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

    Error:
    cDqu.h:36: error: argument of type ‘void (cDequeuevoid*)’ 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:fstream file("output.txt", std::ios_base:ut);
    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. #2
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Problem in Threads in C++.

    please make sure that the thread function you pass to pthread is a static not a class member, that is why it is not working.

    pthread_create is expecting function pointer ::

    void ( *function ) (void *)

    you are passing in class

    void (myclass::*function)(void *)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center