Hello all I am having a bit of trouble learning pthreads. I normally do not post unless I have no other choice but I can not for the life of me see what is wrong with my code. I am creating a ASCII animator class and for the animation I am using pthreads and I am new to pthreads so I know I must be making an elementary mistake (sorry). Anyway here is my code sorry for the sloppiness.
//Animator.h
//Basic ASCII Animator Class
//Written in C++ By Felix Sanchez
//E-Mail thisismyemail2address@gmail.com
//Date 11/8/11
//
#include <iostream> //Console I/O
#include <string> //String class
#include <pthread.h> //Multi threading
//Using the standard namespace
usingnamespace std;
typedefunsignedint uInt;
class Animator
{
pthread_t AnimatorThread; //Thread containing the Animate function
string* Frames; //String Array
uInt nFrames; //Frames in the array
void* animate (void*); //Animator thread for threads
public:
Animator (); //Default Animator constructor
Animator (uInt); //Takes in the amount of frames
Animator (uInt , string*); //Takes in the amount of frames and the frames
~Animator (); //Deletes the frame array
int threadAnimate (int); //Takes in the amount of times a frame will repeat then makes a thread for animating then returns it's return code
/*
uInt getFrames (); //Returns the amount of frames
uInt getCurrentFrame (); //Returns the position of the current frame
void popBack (); //Deletes the last frame
void clearScreen (); //Clears the screen
void animate (uInt); //Takes in the amount of times the frames will repeat
void clearFrames (); //Clears all the frames
void popFrame (uInt); //Takes in a frame number and deletes that frame
void getFrame (uInt); //Takes in a frame number and returns that frame you can also use the [] operator
void pushBack (string*); //Takes a new frame and adds it to the back
void insertFrame (uInt , string*); //Takes in a frame number and a new frame then adds to the array
void replaceFrame (uInt , string*); //Takes in a frame number and a new frame then replaces the old frame with the new one
Animator& operator= (Animator&); //Animator assignement operator
string operator[] (uInt); //Takes in a frame position then returns the frame
*/
};
Ps I am still getting used to Emacs so sorry again. Also here are the errors I get
Animator.cpp: In constructor ‘Animator::Animator(uInt, std::string*)’:
Animator.cpp:19:23: warning: comparison between signed and unsigned integer expressions
Animator.cpp: In member function ‘void* Animator::animate(void*)’:
Animator.cpp:40:35: warning: comparison between signed and unsigned integer expressions
Animator.cpp:45:1: warning: no return statement in function returning non-void
Animator.cpp: In member function ‘int Animator::threadAnimate(int)’:
Animator.cpp:50:100: error: argument of type ‘void* (Animator::)(void*)’ does not match ‘void* (*)(void*)’
My compiler is g++ and my OS is Ubuntu 11.04 if that helps.
To start with, inside class members you don't need the Animate::
pthread_create() can't handle a normal class member function as the thread start routine, as they must be called through a this pointer. You need to use a static class member or a regular function instead.
class Animator
{
...
// You could avoid using this struct, and just pass this to
// threadStart if you make nCycles a member, like Frames and
// nFrames
struct StartInfo
{
Animator* pAnimator;
int nCycles;
};
staticvoid* threadStart(void*);
int threadAnimate (int);
int animate (int Cycles); // now just needs normal params
...