Problem with pthread_join

D

Damon

Hi,

I have modified a class and tried to compile but I keep getting
errors. Can someone advise me what I'm doing wrong here? Thanks alot.

-----------------error message--------------
g++ -g -Wall -O -finline-functions -I. -I../lib -c -o eve.o eve.cc
In file included from realms/world.h:9,
from eve.cc:8:
.../lib/my_thread.h: In member function `int mylib::my_thread::Stop()':
.../lib/my_thread.h:54: invalid conversion from `pthread_t*' to `long
unsigned
int'
make: *** [eve.o] Error 1


-----------------my_thread.h----------------------
#ifndef MY_THREAD_H
#define MY_THREAD_H

#include "pthread.h"
#include <iostream>
/*
* This code was contributed by Ryan Teixeira ([email protected])
* Thanks for making this code available, dude!
* Some modifications made by me, of course.
*/

namespace mylib {
class my_thread
{
private:
pthread_t ThreadId_;
void * Arg_;

protected:
my_thread() {}

int Run(void * arg) {
Setup();
Execute( arg );
return 0;
}

static void * EntryPoint(void* pthis) {
my_thread * pt = (my_thread*)pthis;
pt->Run( pt->Arg() );
return (void*)NULL;
}

virtual void Setup() {}

virtual void Execute(void* obj) {}

void * Arg() const { return Arg_; }

void Arg(void* a){ Arg_ = a; }

public:
virtual ~my_thread() {}

int Start(void * arg) {
Arg(arg); // store user data
return pthread_create( &ThreadId_,
NULL,
my_thread::EntryPoint,
this );
}

int Stop() {
return pthread_join( &ThreadId_, NULL ); //problem line
here.
}

};

};

#endif
 
R

Rob Williscroft

Damon wrote in
Hi,

I have modified a class and tried to compile but I keep getting
errors. Can someone advise me what I'm doing wrong here? Thanks alot.

pthread is outside the topicality of this newsgroup, if you want
advice on using pthread's you should ask in a threading, unix or
maybe a gcc newsgroup.

[snip]
namespace mylib {
class my_thread
{
private:
pthread_t ThreadId_;
void * Arg_; [snip]

int Stop() {
return pthread_join( &ThreadId_, NULL ); //problem line
here.

This is what I found when I put "pthread_join" into google (1st hit):

int pthread_join(pthread_t thread, void **value_ptr);

This sugests to me that you should be passing ThreadId_ *not*
&ThreadId_.

HTH.

Rob.
 
M

Michael Mellor

Damon said:
Hi,

I have modified a class and tried to compile but I keep getting
errors. Can someone advise me what I'm doing wrong here? Thanks alot.

-----------------error message--------------

May I suggest adding -pedantic to make g++ more standard conforming.
g++ -g -Wall -O -finline-functions -I. -I../lib -c -o eve.o eve.cc
In file included from realms/world.h:9,
from eve.cc:8:
../lib/my_thread.h: In member function `int mylib::my_thread::Stop()':
../lib/my_thread.h:54: invalid conversion from `pthread_t*' to `long
unsigned
int'
make: *** [eve.o] Error 1


-----------------my_thread.h----------------------
#ifndef MY_THREAD_H
#define MY_THREAD_H

#include "pthread.h"

Incase you hadn't noticed said:
#include <iostream>
/*
* This code was contributed by Ryan Teixeira ([email protected])
* Thanks for making this code available, dude!
* Some modifications made by me, of course.
*/

namespace mylib {
class my_thread
{
private:
pthread_t ThreadId_;
void * Arg_;

protected:
my_thread() {}

int Run(void * arg) {
Setup();
Execute( arg );
return 0;
}

static void * EntryPoint(void* pthis) {
my_thread * pt = (my_thread*)pthis;
pt->Run( pt->Arg() );
return (void*)NULL;
}

virtual void Setup() {}

virtual void Execute(void* obj) {}
If this is an abstract base class you could make it a pure virtual method.
virtual void Execute(void* obj) = 0;
void * Arg() const { return Arg_; }

void Arg(void* a){ Arg_ = a; }

public:
virtual ~my_thread() {}

int Start(void * arg) {
Arg(arg); // store user data
return pthread_create( &ThreadId_,
NULL,
my_thread::EntryPoint,
this );
}

int Stop() {
I typed "man pthread_join" and got:
.....
int pthread_join(pthread_t th, void **thread_return);
.....
hence
return pthread_join( ThreadId_, NULL ); //problem line
return pthread_join( &ThreadId_, NULL ); //problem line
here.
}

};

};
No semi-colon here (after namespace) and I wish all compilers would flag
it as an error.

Michael Mellor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top