D
DanielEKFA
Hey there
Just joined the group, looking for a resolution to a problem I and my
three groups members are having (we're students, making an FTP-like
server/client as an exam project).
We're getting segmentation faults when threading with the following
code (this code is not the actual code as it's going to look, but a
shortened version for debugging purposes, yet it segfaults the same way
as the "real" code):
-------------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
#include <vector>
#include "executer.h"
#include "taskmanager.h"
using namespace std;
typedef void ReporterFunctionType(const string&);
typedef bool QueueGetFunctionType(SymbolSequence&,
PerformerFunctionType*&);
struct ThreadParameters
{
string host, username, password;
int port;
ReporterFunctionType* reporter;
QueueGetFunctionType* retriever;
};
void reporter(const string& message) {
cout << message << endl;
}
void *worker(void *structPtr)
{/*
// TODO: Get this out somewhere global, it's in TaskManager, too:
struct ThreadParameters
{
string host, username, password;
int port;
ReporterFunctionType* reporter;
QueueGetFunctionType* retriever;
};
// We need the parameters readable:
ThreadParameters *MyParams = (ThreadParameters*) structPtr;
(*(MyParams->reporter))("inside the thread!");
// Done, so we deallocate the memory occupied by our parameter struct:
delete MyParams;*/
}
int main(int argc, char *argv[])
{
// TEST START:
pthread_t handler;
pthread_attr_t attr;
// Create a parameter struct for the thread:
ThreadParameters* Parameters;
Parameters = new ThreadParameters(); // NOTE: The thread must remember
to deallocate memory when it's done.
// Fill in common parameter values:
Parameters->host = "";
Parameters->username = "";
Parameters->password = "";
Parameters->port = 21;
Parameters->reporter = &reporter;
Parameters->retriever = NULL;
pthread_create(&handler, &attr, worker, (void *) Parameters);
// TEST STOP.
return EXIT_SUCCESS;
}
-------------------------------------------------------------------------------
The project is being written in kdevelop, so we were suspecting that
perhaps kdevelop was "forgetting" the "-lpthread" make parameter
(earlier on we were getting segfaults when threading because we had
forgotten this parameter in our custom made makefile), but making our
own makefile with the -lpthread parameter in place yielded the same
result. Compiling works, execution doesn't.
As you'll notice, the actual code in the thread function is commented
out, simply to try to determine if the segfault was happening inside
the thread, before the thread, or in the actual pthread_create call. It
is the latter that is the case, as was confirmed by using the debug
tool ("gdb", is it?).
We're thinking it has to be something with our parameters, but we're
not sure what... Help will be very very much appreciated, as we're
really scratching our dumb heads here
Thanks in advance,
Daniel plus crew
Just joined the group, looking for a resolution to a problem I and my
three groups members are having (we're students, making an FTP-like
server/client as an exam project).
We're getting segmentation faults when threading with the following
code (this code is not the actual code as it's going to look, but a
shortened version for debugging purposes, yet it segfaults the same way
as the "real" code):
-------------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
#include <vector>
#include "executer.h"
#include "taskmanager.h"
using namespace std;
typedef void ReporterFunctionType(const string&);
typedef bool QueueGetFunctionType(SymbolSequence&,
PerformerFunctionType*&);
struct ThreadParameters
{
string host, username, password;
int port;
ReporterFunctionType* reporter;
QueueGetFunctionType* retriever;
};
void reporter(const string& message) {
cout << message << endl;
}
void *worker(void *structPtr)
{/*
// TODO: Get this out somewhere global, it's in TaskManager, too:
struct ThreadParameters
{
string host, username, password;
int port;
ReporterFunctionType* reporter;
QueueGetFunctionType* retriever;
};
// We need the parameters readable:
ThreadParameters *MyParams = (ThreadParameters*) structPtr;
(*(MyParams->reporter))("inside the thread!");
// Done, so we deallocate the memory occupied by our parameter struct:
delete MyParams;*/
}
int main(int argc, char *argv[])
{
// TEST START:
pthread_t handler;
pthread_attr_t attr;
// Create a parameter struct for the thread:
ThreadParameters* Parameters;
Parameters = new ThreadParameters(); // NOTE: The thread must remember
to deallocate memory when it's done.
// Fill in common parameter values:
Parameters->host = "";
Parameters->username = "";
Parameters->password = "";
Parameters->port = 21;
Parameters->reporter = &reporter;
Parameters->retriever = NULL;
pthread_create(&handler, &attr, worker, (void *) Parameters);
// TEST STOP.
return EXIT_SUCCESS;
}
-------------------------------------------------------------------------------
The project is being written in kdevelop, so we were suspecting that
perhaps kdevelop was "forgetting" the "-lpthread" make parameter
(earlier on we were getting segfaults when threading because we had
forgotten this parameter in our custom made makefile), but making our
own makefile with the -lpthread parameter in place yielded the same
result. Compiling works, execution doesn't.
As you'll notice, the actual code in the thread function is commented
out, simply to try to determine if the segfault was happening inside
the thread, before the thread, or in the actual pthread_create call. It
is the latter that is the case, as was confirmed by using the debug
tool ("gdb", is it?).
We're thinking it has to be something with our parameters, but we're
not sure what... Help will be very very much appreciated, as we're
really scratching our dumb heads here
Thanks in advance,
Daniel plus crew