multithreading - c++ multithread -


i use c++ implement thread class. code shows in following. have problem how access thread data. in class thread, create thread use pthread_create() function. calls entrypoint() function start thread created. in run function, want access mask variable, shows segment fault. so, question whether new created thread copy data in original class? how access thread own data?

class thread { public:   int mask;   pthread_t thread;    thread( int );   void start();   static void * entrypoint (void *);   void run(); };  thread::thread( int a) {   mask =a;  }  void thread::run() {    cout<<"thread begin run" <<endl;   cout << mask <<endl;       // show segmentfault here }  void * thread::entrypoint(void * pthis) {   cout << "entry" <<endl;   thread *pt = (thread *) pthis;   pt->run(); }  void thread::start() {    pthread_create(&thread, null, entrypoint, (void *)threadid );   pthread_join(thread, null); }  int main() {   int input_array[8]={3,1,2,5,6,8,7,4};   thread t1(1);   t1.start();   } 

i'm not familiar libraries you're using, how entrypoint know pthis pointer thread? thread (this) not appear passed pthread_create.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -