Hello everyone!
I am using pthreads on Linux with C++ which spawns one thread and (tries to) play music. However, as soon as the program gets to the point where any function tries to use any member in the class, program seg-faults.
Here -
class audio
{
public:
int play(char *filename);
int test;
private:
static void *audioThread( void *ptr );
void playAudio(void);
pthread_t playThread;
}
void *audio::audioThread( void *ptr )
{
audio *play = (audio *)ptr;
play->playAudio();
delete play;
return NULL;
}
void audio::playAudio(void)
{
test = 0;
/* playback code */
}
int audio::play(char *filename)
{
....
....
test = 1;
if (pthread_create( &playThread, NULL, &audio::audioThread, NULL) != 0)
{
return -1;
}
return 0;
}
The aboce program would crash when the thread function playAudio would try to write 0 to test. What's wrong? Thank you.