Threading Help

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

//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
using namespace std;

typedef unsigned int 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
	*/
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Animator.cpp

#include "Animator.h"

Animator::Animator ()
{
	Animator::nFrames = 0;
	Animator::AnimatorThread = 0;
}

Animator::Animator (uInt nFrameLength)
{
	Animator::Frames = new string [nFrameLength];
	Animator::nFrames = nFrameLength;
	Animator::AnimatorThread = 0;
}

Animator::Animator (uInt nFrameLength , string* FrameArray)
{
	Animator::Frames = new string [nFrameLength];
	for (int i = 0 ; i < nFrameLength ; i++)
		{
			Animator::Frames [i] = FrameArray [i]; 
		}
	Animator::AnimatorThread = 0;
}

Animator::~Animator ()
{
  if (Animator::Frames != 0)
		{
			delete [] Animator::Frames;
		}
}

void* Animator::animate (void* Cycles)
{
    int * pCycles = (int*) Cycles;
	int nCycles = *pCycles;
	for (int i = 0 ; i < nCycles ; i++)
		{
			for (int j = 0 ; j < Animator::nFrames ; j++)
				{
					cout << Animator::Frames [j] << endl;
				}
		}
}

int Animator::threadAnimate (int nCycles)
{
	int nReturn = 10;
	nReturn = pthread_create (&(Animator::AnimatorThread) , NULL , Animator::animate , (void*) nCycles);
	pthread_join (Animator::AnimatorThread , NULL);
	return nReturn;
}


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.
Also I am learning from this website. If anyone else wants to learn threading.

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
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.

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
    };

    static void* threadStart(void*);

    int threadAnimate (int);

    int animate (int Cycles); // now just needs normal params

    ...


And

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
...

int Animator::threadAnimate (int nCycles)
{
	int nReturn = 10;
        StartInfo startInfo = {this, nCycles};
	nReturn = pthread_create (&AnimatorThread , NULL, threadstart, (void*)&startInfo);
	pthread_join (AnimatorThread , NULL);
	return nReturn;
}

/*static*/ void* Animator::threadStart(void* arg)
{
    StartInfo* pStartInfo = (StartInfo*)arg;

    Animator* pAnimator = pStartInto->pAnimator;
    int nCycles = pStartInfo->nCycles;

    int nReturn = pAnimator->animate(nCycles);

    return (void*)nReturn;
}

int Animator::animate (int nCycles)
{
    for (int i = 0 ; i < nCycles ; i++)
    {
        for (uInt j = 0 ; j < nFrames ; j++) // j is now uInt to shut warning up!
        {
            cout << Frames [j] << endl;
        }
    }
    return 0; // or a more useful return code
}

...


Andy
Last edited on
That is brilliant!!! Thanks so much. I like the struct idea it seems sooooo much more simpler. Anyway thanks. Problem solved!!! :)
Topic archived. No new replies allowed.