gpt4 book ai didi

c++ - 在 allocate_stack (stack=, pdp=

转载 作者:行者123 更新时间:2023-11-28 05:50:07 28 4
gpt4 key购买 nike

我在执行以下示例程序时遇到了段错误:

#include<iostream>
#include<pthread.h>
#include<string.h>
#include<exception>
#include<stdexcept>
using namespace std;

class ThreadHandler
{
public:
int createThread(void (*function)(void*),void* input);
void setThreadID(int);
int getThreadID();
void runThread();
/* Constructor for ThreadHandler*/
ThreadHandler();
virtual ~ThreadHandler();
protected:
private:
int mThreadID;
pthread_t thread;
};

/* Function: ThreadHandler() */
ThreadHandler::ThreadHandler():mThreadID(0)
{
cout<<"\n ThreadHandling - constructor";
}

/* Function: ~ThreadHandler() */
ThreadHandler::~ThreadHandler()
{
}

/*Function: setThreadID() - set mThreadID value after
thread creation*/
void ThreadHandler::setThreadID(int thread_id)
{
cout<<"\n setThreadID function Entry";
mThreadID=thread_id;
cout<<"\n Thread ID: "<<mThreadID;
cout<<"\n setThreadID function Exit";
}

/*Function: getThreadID() - return a thread ID after
thread creation*/
int ThreadHandler::getThreadID()
{
return mThreadID;
}

/*Function: createThread() - Create Thread
and Assign function dynamically */
int ThreadHandler::createThread(void (*callingFunction)(void*),void* input)
{
cout<<"\n createThread Function Entry";
int thread_id=pthread_create(&thread,(pthread_attr_t*)callingFunction,NULL,input);
if(thread_id)
{
cout<<"\n Failed to create the thread and throw an exception";
throw;
}
setThreadID(thread_id);
cout<<"\n createThread Function Exit";
return thread_id;
}

/* Function: runThread() -- Joinable thread for exection*/
void ThreadHandler::runThread()
{
cout<<"\n Join the thread for runnable"<<endl;
pthread_join(this->thread,NULL);
}

/*Function to execute by the thread*/
void ThreadFunction(void* input)
{
cout<<"\n Thread Execution: "<<input;
}
/*Main part of the Class and Execution*/
int main()
{
char* msgPtr="I am running";
ThreadHandler *objThread = new ThreadHandler();
if(objThread==NULL)
{
cout<<"\n Failed to create a ThreadHandler object";
return -1;
}
/*Create a function pointer to pass*/
//void* (*FunctionPtr)(void*)=NULL;
//FunctionPtr = &ThreadFunction;
try
{
cout<<"\n -- start create a thread --";
objThread->createThread(&ThreadFunction,reinterpret_cast<void*>(msgPtr));
}
catch(exception& e)
{
cout<<"\n Exception while creating the thread";
}
return 0;
}

错误: ThreadHandling - 构造函数 -- 开始创建线程 --

程序收到信号 SIGSEGV,段错误。0x00007ffff76aab8c 在 allocate_stack (stack=, pdp=, attr=0x400ead ) 在 allocatestack.c:415415 allocatestack.c: 没有那个文件或目录。

最佳答案

问题

看起来您以错误的顺序将参数传递给 pthread_create

来自 man-pages :

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);

解决方案

改变这个:

pthread_create(&thread,(pthread_attr_t*)callingFunction,NULL,input);

为此:

pthread_create(&thread, NULL, callingFunction, input);

建议

我建议使用 std::thread因为它使使用线程更容易,尤其是与 std::function 结合使用时.

关于c++ - 在 allocate_stack (stack=<synthetic pointer>, pdp=<synthetic pointer>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35438347/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com