User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,045 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,267 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 370 | Replies: 4 | Solved
Reply
Join Date: Jul 2007
Posts: 37
Reputation: linux0id is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
linux0id's Avatar
linux0id linux0id is offline Offline
Light Poster

access class members through pthread

  #1  
Jun 3rd, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Posts: 833
Reputation: mitrmkar is a jewel in the rough mitrmkar is a jewel in the rough mitrmkar is a jewel in the rough 
Rep Power: 4
Solved Threads: 170
mitrmkar mitrmkar is offline Offline
Practically a Posting Shark

Re: access class members through pthread

  #2  
Jun 3rd, 2008
See the comments for explanation for the error ...
void *audio::audioThread( void *ptr )
{
     // ptr is NULL here
     audio *play = (audio *)ptr;

     // now you call playAudio() through a NULL pointer 
     play->playAudio();
     delete play;
     return NULL;
}

void audio::playAudio(void)
{
     // here the 'this' pointer is NULL, hence you have no access to
     // member variables, so the seg-fault is coming up ...
     test = 0;
     /* playback code */
}

int audio::play(char *filename)
{
     test = 1;

     // Here it starts to go wrong, because you pass in 
     // NULL as argument to audio::audioThread() ...
     if (pthread_create( &playThread, NULL, &audio::audioThread, NULL) != 0)
     {
             return -1;
      }

     return 0;
}
Reply With Quote  
Join Date: Jul 2007
Posts: 37
Reputation: linux0id is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
linux0id's Avatar
linux0id linux0id is offline Offline
Light Poster

Re: access class members through pthread

  #3  
Jun 3rd, 2008
Well, after some thinking I put the "this" pointer into function argument, so it passes the current class to the thread function, like so -
if (pthread_create( &playThread, NULL, &audio::audioThread, this) != 0)
instead of NULL. Now it works!!!
Thanks a lot for the help and explanation!!!
Reply With Quote  
Join Date: Nov 2007
Posts: 833
Reputation: mitrmkar is a jewel in the rough mitrmkar is a jewel in the rough mitrmkar is a jewel in the rough 
Rep Power: 4
Solved Threads: 170
mitrmkar mitrmkar is offline Offline
Practically a Posting Shark

Re: access class members through pthread

  #4  
Jun 3rd, 2008
Originally Posted by linux0id View Post
Well, after some thinking I put the "this" pointer into function argument, so it passes the current class to the thread function, like so -
if (pthread_create( &playThread, NULL, &audio::audioThread, this) != 0)
instead of NULL. Now it works!!!
Thanks a lot for the help and explanation!!!


Then you might want to remove the line delete play; in audio::audioThread() (if you still have it there). Or at least consider the consiquences of deleting the object inside that function i.e. once you have called audio::play() on an audio object, you are not to do anything else with that particular object instance anymore.
Reply With Quote  
Join Date: Jul 2007
Posts: 37
Reputation: linux0id is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
linux0id's Avatar
linux0id linux0id is offline Offline
Light Poster

Re: access class members through pthread

  #5  
Jun 3rd, 2008
good point...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:19 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC