0

我正在尝试实现一个线程接口类

我遇到了 join() 函数的问题,它给了我一个分段错误

输出:

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)

有趣的是,当我调用 gdb 并在 MyThread::run 设置中断时,会打印素数并且我收到一条消息:

.......
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

请参考代码并帮助我,我是线程新手。顺便说一句,我已经删除了一些代码,只保留了必要的代码

#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;
}

更新:

使用pthread_create(&id, NULL, function, NULL);, 不会出现段错误,并且一切正常。

4

1 回答 1

3

代码看起来正确。

但是,您应该使用编译器选项-pthread,而只是使用链接器选项链接到 PThread 库-lpthread。(https://stackoverflow.com/a/1665110/694576

于 2015-03-28T16:33:20.630 回答