ich habe ein komisches Problem. Ich habe eine kleine Klasse um pthread
gebastelt/geklaut. Nun wollte ich Debug-Ausgabe per printf ausgeben.
Segmentation fault. Nutze ich dagegen std::cout, geht alles.
Nun kann das mir im Prinzip egal sein, aber solche Sachen deuten ja u.U.
auf Fehler hin, die irgendwo anders liegen.
Deshalb meine Frage: Was kann das sein? Woran liegt das? Sieht jemand in
dem Code ein Problem?
Danke!
<UPDATE>
Nachdem ich alles fertig geschrieben hatte, habe ich mal in der main
setvbuf(stdout, NULL, _IONBF, 0);
auskommentiert -> kein Segmentation faul mehr!
Achso. Warum das denn?
</UPDATE>
################# Source #################
// ----- Class : Thread -----
class Thread
{
private:
pthread_t _id;
pthread_attr_t _attr;
// Prevent copying or assignment
Thread(const Thread& arg);
Thread& operator=(const Thread& rhs);
protected:
void *arg;
static void *exec(void *thr);
public:
Thread(bool detach, int size);
virtual ~Thread();
void start(void *arg);
void join();
virtual void run() = 0;
};
##########################
#include "../include/Thread.h"
Thread::Thread(bool detach, int size)
{
int ret;
if ((ret = pthread_attr_init(&_attr)) != 0)
{
cout << strerror(ret) << endl;
throw "Error";
}
if (detach)
{
if ((ret = pthread_attr_setdetachstate(&_attr,
PTHREAD_CREATE_DETACHED)) != 0)
{
cout << strerror(ret) << endl;
throw "Error";
}
}
if (size >= PTHREAD_STACK_MIN)
{
if ((ret = pthread_attr_setstacksize(&_attr, size)) != 0)
{
cout << strerror(ret) << endl;
throw "Error";
}
}
}
Thread::~Thread()
{
int ret;
if ((ret = pthread_attr_destroy(&_attr)) != 0)
{
cout << strerror(ret) << endl;
throw "Error";
}
}
void Thread::start(void *arg)
{
int ret;
this->arg = arg;
if ((ret = pthread_create(&_id, &_attr, &Thread::exec, this)) != 0)
{
cout << strerror(ret) << endl;
throw "Error";
}
}
void Thread::join()
{
pthread_join(_id, NULL);
}
void *Thread::exec(void *thr)
{
reinterpret_cast<Thread *> (thr)->run();
return 0;
}
##########################
Testscenario
class MyThread : public Thread
{
public:
MyThread(bool detach, int size) : Thread(detach, size) {}
void run()
{
printf("\nMythread-test"); // das kracht
cout << endl << "\nMythread-test"; // hiermit kein Problem
}
};
#############
int main(int argc, char* argv[])
{
setvbuf(stdout, NULL, _IONBF, 0); // <-- der Uebeltaeter?!?!
int x = 0;
MyThread thr(true, PTHREAD_STACK_MIN);
thr.start(&x);
getchar();
return 1;
}
--
Bye
In der Tat.
>
> Deshalb meine Frage: Was kann das sein? Woran liegt das? Sieht jemand in
> dem Code ein Problem?
Mit einem Debugger haettest du das Problem in einer Minute leicht klaeren
koennen, man muss sich nur trauen, auch mal auf Assembler-Ebene zu schauen...
> #############
> int main(int argc, char* argv[])
> {
> setvbuf(stdout, NULL, _IONBF, 0); // <-- der Uebeltaeter?!?!
Aber nicht doch.
> int x = 0;
> MyThread thr(true, PTHREAD_STACK_MIN);
^ Der wahre Uebeltaeter.
HTH,
Marcel
>> #############
>> int main(int argc, char* argv[])
>> {
>> setvbuf(stdout, NULL, _IONBF, 0); // <-- der Uebeltaeter?!?!
>
> Aber nicht doch.
>
>> int x = 0;
>> MyThread thr(true, PTHREAD_STACK_MIN);
>
> ^ Der wahre Uebeltaeter.
...ist bei mir 16384. Und wird durch das printf �berschritten. Achso.
Okay.
--
Bye
>>> MyThread thr(true, PTHREAD_STACK_MIN);
Nachtrag:
You can get the absolute minimum limit on stack size by calling the
macro PTHREAD_STACK_MIN (defined in <pthread.h>), which returns the
amount of stack space required for a thread that executes a NULL
procedure. Useful threads need more than this, so be very careful when
reducing the stack size.
NULL procedure. Okay. Alles klar. Ich habe mir gedacht, was die Funktion
macht. Fehler. Nachlesen!
Danke f�r den Tip.
--
Bye