c++ - segmentation fault on joining pthread -


i trying implement thread interface class

i having problem join() function, gives me segmentation fault

the output:

g++ threadinterface.cpp -lpthread [murtraja@localhost src]$ ./a.out  name: thread # 0 policy: fifo scope: system state: detached    name: thread! policy: round robin scope: system state: joinable  running thread! segmentation fault (core dumped) 

what interesting when call gdb , set break @ mythread::run, prime nos printed , message that:

....... 48 not prime 49 not prime 0x000000000040141c in main () (gdb) s single stepping until exit function main, has no line number information.  program received signal sigsegv, segmentation fault. 0x00007ffff7bc920a in pthread_join () /lib64/libpthread.so.0 

please refer code , me, new threads. btw, i've removed code , kept necessary

#include <iostream> #include<pthread.h> #include<stdio.h> #include<cstring>  using namespace std; class mythread {     int policy, state, scope;     char name[35];     char policyname[30];     char statename[30];     char scopename[30];     pthread_attr_t attrib;     pthread_t id;     static int count;     void setprintables(); public:     mythread(void);     mythread(char*);     void setpolicy(int);     void setstate(int);     void setscope(int);     void setname(char*);     void printdetails();     void loaddefaults();     void run(void *(*function)(void*));     void join();  }; int mythread::count = 0; mythread::mythread() {     loaddefaults();     count++; } mythread::mythread(char* name) {     loaddefaults();     strcpy(this->name, name);     count++; } void mythread::loaddefaults() {     pthread_attr_init(&attrib);     pthread_attr_setinheritsched(&attrib, pthread_explicit_sched);     state = pthread_create_joinable;     scope = pthread_scope_process;     policy = sched_other;     sprintf(name, "thread # %d", count); }  void mythread::join() {     if(state == pthread_create_joinable)         pthread_join(id, null); } void mythread::run(void *(*function)(void*)) {     cout<<"now running "<<name<<endl;     pthread_create(&id, &attrib, function, null);     //function(this); } void *printfactorials(void*) {     for(int i=1; i<50; i++)     {         long fact = 1;         for(int j=i; j>=1; j--)         {             fact*=(long)j;         }         cout<<i<<"! = "<<fact<<endl;     } } void *printprimes(void*) {     for(int i=1; i<50; i++)     {         int c = 0;         for(int j=1; j<=i; j++)         {             if(!(i%j))                 c++;         }         if(c==2)             cout<<i<<" prime"<<endl;         else             cout<<i<<" not prime"<<endl;     } } int main() {     char name[] = "my thread!";     mythread t1, t2(name);     t1.setpolicy('f');     t1.setstate('d');     t1.setscope('p');      t2.setpolicy('r');     t2.setstate('j');     t2.setscope('p');      t1.printdetails();     cout<<endl;     t2.printdetails();     //t1.run(printfactorials);     //void* (*f)(void*) = printprimes;     t2.run(printprimes);     //t1.join();     t2.join();     return 0; } 

update:

using pthread_create(&id, null, function, null);, gives no seg fault , works flawlessly.

the code looks good correct.

however should using compiler option -pthread linking pthread library linker option -lpthread. (https://stackoverflow.com/a/1665110/694576)


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -