0

I am trying to implement a thread interface class

I am having a problem with join() function, it gives me a segmentation fault

the output:

g++ threadInterface.cpp -lpthread
[murtraja@localhost src]$ ./a.out

Name: Thread # 0
Policy: FIFO
Scope: System
State: Detached



Name: my thread!
Policy: Round Robin
Scope: System
State: Joinable

Now running my thread!
Segmentation fault (core dumped)

What is interesting is that when I call gdb and set a break at MyThread::run, the prime nos are printed and I get a message that:

.......
48 is not prime
49 is not prime
0x000000000040141c in main ()
(gdb) s
Single stepping until exit from function main,
which has no line number information.

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bc920a in pthread_join () from /lib64/libpthread.so.0

please refer the code and help me, i am new to threads. BTW, i've removed some code and kept only that is 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<<" is prime"<<endl;
        else
            cout<<i<<" is 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 and everything works flawlessly.

Murtaza Raja
  • 309
  • 4
  • 15

1 Answers1

3

The code looks good correct.

However you should be using the compiler option -pthread but just linking to the PThread library with the linker option -lpthread. (https://stackoverflow.com/a/1665110/694576)

Community
  • 1
  • 1
alk
  • 69,737
  • 10
  • 105
  • 255