Subj : Re: How to start programming with pthreads? To : comp.programming.threads From : tat Date : Sun Mar 27 2005 08:37 am Thanks Ed for your suggestion. I have the following codes which do not compile. I wonder if anyone can help me to figure out what when wrong. TIA. // maincode.cpp #include #include "tat_thread.h" #include "hello.h" using namespace std; int main() { TAT_Thread t_en; TAT_Thread t_fr; TAT_Thread t_de; TAT_Thread t_it; TAT_Thread t_vn; string hello = "Hello"; string bonjour = "Bonjour"; string gutentag = "Guten Tag"; string chao = "Chao"; string ciao = "Ciao"; t_en.Start((void*)&hello); t_fr.Start((void*)&bonjour); t_de.Start((void*)&gutentag); t_vn.Start((void*)&chao); t_it.Start((void*)&ciao); t_en.Wait(); t_fr.Wait(); t_de.Wait(); t_it.Wait(); t_vn.Wait(); return (0); } // tat_thread.h #ifndef TAT_THREAD_H #define TAT_THREAD_H #include #define FALSE 0 #define TRUE 1 static void* ThreadFunction(void*); class TAT_Thread { friend void* ThreadFunction(void*); public: TAT_Thread(void); virtual ~TAT_Thread(void); int Start(void* = NULL); void Detach(void); void* Wait(void); void Stop(void); unsigned int GetThreadID(void); static unsigned int GetCurrentThreadID(void); static void Sleep(int); protected: virtual void* Run(void*); private: pthread_t m_ThreadHandle; unsigned int m_ThreadID; int m_Started; int m_Detached; }; // tat_thread.cpp #include #include #include "tat_thread.h" #include using namespace std; /*! Default constructor */ TAT_Thread::TAT_Thread(void) { m_Started = FALSE; m_Detached = FALSE; } /*! Destroys the object */ TAT_Thread::~TAT_Thread(void) { Stop(); } int TAT_Thread::Start(void* param) { if(!m_Started) { pthread_attr_t attributes; pthread_attr_init (&attributes); if(m_Detached) { pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); } m_Param = param; m_ThreadID = 0; if(pthread_create(&m_ThreadHandle,&attributes, ThreadFunction,this) == 0) { m_Started = TRUE; } pthread_attr_destroy(&attributes); } return m_Started; } void* TAT_Thread::Run(void* param) { return NULL; } void TAT_Thread::Detach(void) { if(m_Started && !m_Detached) { pthread_detach(m_ThreadHandle); } m_Detached = TRUE; } void* TAT_Thread::Wait(void) { void* status = NULL; if(m_Started && !m_Detached) { pthread_join(m_ThreadHandle,&status); m_Detached = TRUE; } return status; } void TAT_Thread::Stop(void) { if(m_Started && !m_Detached) { pthread_cancel(m_ThreadHandle); pthread_detach(m_ThreadHandle); m_Detached = TRUE; } } unsigned int TAT_Thread::GetThreadID(void) { return m_ThreadID; } unsigned int TAT_Thread::GetCurrentThreadID(void) { return 0; } void TAT_Thread::Sleep(int delay) { timeval timeout = {(delay/1000,((delay*1000)%1000000))}; select(0,(fd_set*)NULL,(fd_set*)NULL,(fd_set*)NULL,&timeout); } static void* ThreadFunction(void* object) { TAT_Thread* thread = (TAT_Thread*) object; return thread->Run(thread->m_Param); } // hello.h #ifndef HELLO_HEADER_H #define HELLO_HEADER_H #include "tat_thread.h" class Hello: public TAT_Thread { //public: //Hello(); //~Hello(); protected: virtual void* Run(void*); }; #endif // end of HELLO_HEADER_H //////// // hello.cpp ///////// #include #include "hello.h" using namespace std; void* Hello::Run(void* param) { char* message = (char*) param; for(int i=0; i < 3; i++) { cout << message << endl; Sleep(100); } return NULL; } //// // Makefile ////// CC = g++ CFLAGS = -g3 -pg -c -O3 -pthread EXE_SRCS = maincode.cpp tat_thread.cpp hello.cpp EXE_NAME = maincode OBJS = maincode.o tat_thread.o hello.o %.o:%.C @echo $(FOO) @echo \* @echo \* Making $@ @echo \* $(CC) -c $(CFLAGS) $< -o $@ $(EXE_NAME): $(OBJS) @echo $(FOO) @echo \* @echo \* Linking $@ @echo \* $(CC) -o $@ $(OBJS) $(CLIBS) clean: @echo $(FOO) @echo \* @echo \* Clean up $@ @echo \* rm -rf *.o core $(EXE_NAME) .