Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Classes and Threads

This thread is locked; no one can reply to it. rss feed Print
Classes and Threads
alex Ioan
Member #12,015
June 2010

Well I need to have an instruction running in the same time as the main thread,
example: instruction would be play an audio then destroy the sample. Normally I need to call that function that plays the audio then destroys it and wait until it does so. What I want is to call that class function in a separate thread and start it so that the game can continue without waiting for the sound to be played and then destroyed.

ALLEGRO_THREAD *t = al_create_thread(playdead,&sound);
al_start_thread(t);

void *playdead(ALLEGRO_THREAD *me,void *arg)
{
sound.playkill();
return NULL;
}

this gives an error at sound.playkill() : IntelliSense: a nonstatic member reference must be relative to a specific object

Am I doing it wrong ?

kazzmir
Member #1,786
December 2001
avatar

void * playdead(ALLEGRO_THREAD * me, void * arg){
  Sound * sound = (Sound*) arg;
  sound->playKill();
  return NULL;
}

Thats what the arg argument is for.

alex Ioan
Member #12,015
June 2010

Got it thanks ! hope it works :D

EDIT: it works though when I play another sample after it crashes ....

strange that I got it to play 2 samples in the same time from main thread and the thread I created... but the third sound just makes the program crash

"Unhandled exception at 0x5bd1d190 in game.exe: 0xC0000005: Access violation writing location 0x5c09d000."

code:

game:

ALLEGRO_THREAD *t = al_create_thread(playdead,&sound);
al_start_thread(t);

//stuff
//press 1 return to main menu:
al_destroy_thread(t);
sound.stopfightmusic(1); 
sound.startbackmusic(); //this line triggers the error 
return 0;

sound class

#SelectExpand
1 ALLEGRO_SAMPLE_ID bkgid; 2 3void sound::startbackmusic() 4 {if(musicon==1) 5 { al_play_sample(bkg,0.55,0,1.0,ALLEGRO_PLAYMODE_LOOP,&bkgid);}}//this line triggers the error 6 7void sound::stopfightmusic(int a) 8 {if(musicon==1) 9 {al_stop_sample(&bkgfid); 10 if (a==1) 11 {al_destroy_sample(bkg_fight);} 12 } 13 } 14 15void sound::playkill() 16 {if (soundon==1) 17 {al_play_sample(kill,1.0,0,1.0,ALLEGRO_PLAYMODE_ONCE,NULL);}}

kazzmir
Member #1,786
December 2001
avatar

You can't pass stack allocated variables to a thread and expect the thread to work if the function that created the thread returns. You should allocate your sounds dynamically (malloc or new) in that case.

// bad
void dostuff(){
  Sound sound;
  play_in_thread(&sound);
}

// good
void dostuff(){
  Sound * sound = new Sound();
  play_in_thread(sound);
}

alex Ioan
Member #12,015
June 2010

hmm will see if it works

bamccaig
Member #7,536
July 2006
avatar

That code is HORRIBLY formatted. You will find it difficult to get people to help you with code formatted like that. I suggest you read online about how to properly style C or C++ code (there are different ways that are purely subjective, but your way is just unacceptable).

You seem to be lacking some of the core knowledge of C++ classes and objects and parameter passing in C or C++. Do you understand what kazzmir is talking about?

alex Ioan
Member #12,015
June 2010

yeap I understand it and yea I know my code style is horrible but I get it way better my way :( will try and change it in time

Go to: