I am facing problem void * return type function is not able to access class members if I make that variable as global I am able to access and if it is declared inside class then runtime it is giving me segmentation fault: In The both Condition I am sharing the code below:
Condition 1: in this apple is declared inside class so when i am trying to access apple in therad_alsa_playback function it is giving me segmentation fault
#include <pthread.h>
#include<stdio.h>
#include<unistd.h>
class alsaAudio{
private:
int apple=1202;
int audio_complete=1;
pthread_t playback_thread;
public:
void *thread_alsa_playback(void *InputArgs){
printf("printing_apple%d\n",apple);
}
int alsa_playback(){
printf("here\n");
if(audio_complete==0 ){
pthread_join(playback_thread, NULL);
audio_complete=1;
}
if(audio_complete==1 ){
audio_complete=2;
typedef void * (*THREADFUNCPTR)(void *);
pthread_create(&playback_thread, NULL,(THREADFUNCPTR)&alsaAudio::thread_alsa_playback,NULL);
printf("created\n");
}
return 0;
}
};
int main(int argc, char **argv) {
alsaAudio voice;
voice.alsa_playback();
usleep(20);
return 0;
}
Condition 2: Here apple is global so i am able to access in thread_alsa_playback function without any error.
#include <pthread.h>
#include<stdio.h>
#include<unistd.h>
int apple=1202;
class alsaAudio{
private:
int audio_complete=1;
pthread_t playback_thread;
public:
void *thread_alsa_playback(void *InputArgs){
printf("printing_apple%d\n",apple);
}
int alsa_playback(){
printf("here\n");
if(audio_complete==0 ){
pthread_join(playback_thread, NULL);
audio_complete=1;
}
if(audio_complete==1 ){
audio_complete=2;
typedef void * (*THREADFUNCPTR)(void *);
pthread_create(&playback_thread, NULL,(THREADFUNCPTR)&alsaAudio::thread_alsa_playback,NULL);
printf("created\n");
}
return 0;
}
};
int main(int argc, char **argv) {
alsaAudio voice;
voice.alsa_playback();
usleep(20);
return 0;
}
In condition 2 there is nothing to shocked but why in condition 1 it is crashing.
I tried on internet but i didnt got any answer . If someone can help Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire