GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Apr-2008, 06:31
at8 at8 is offline
New Member
 
Join Date: Apr 2008
Posts: 3
at8 is on a distinguished road

pthread trick works with gcc 3.x.x, but not with 4.x.x


At may 2000 issue of the drJobbs journal was a cool trick to do posix threading with cpp.
Here is the original paper:
HTML Code:
http://www.lotn.org/~calkinsc/papers/cujthread.pdf
I used the trick at small simulation SW and it works just fine.
Recently I tried to use it again, but it does not compile any more. After few tries I found out that it still builds with gcc 3.x.x.

Apparently gcc 4.x.x. is more strict. What is the problem and how to solve it??


Comple attached example by: g++ -lpthread main.cpp
(@ pdf document above are the original examples)

CPP / C++ / C Code:
#include <pthread.h>
#include <iostream>
using namespace std;

class Thread {
protected:
   pthread_t _threadptr;
   void Run() {cout << "thread hello" << endl;}
public:
   friend void *thread_func(void *);
   void Create() { pthread_create(&_threadptr, NULL, thread_func, this); }
   ~Thread() { pthread_join(_threadptr, NULL); }
};

void *thread_func(void *p) {
   Thread *t=reinterpret_cast<Thread *>(p);
   if (t)
     t->Run();
   return 0;
}

int main() {
   Thread t;
   t.Create();
   cout << "main hello" << endl;
   return 0;
}
  #2  
Old 21-Apr-2008, 08:03
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: pthread trick works with gcc 3.x.x, but not with 4.x.x


I don't have the environment available right now. Could you please post the error message(s)?

Thanks,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 21-Apr-2008, 08:14
at8 at8 is offline
New Member
 
Join Date: Apr 2008
Posts: 3
at8 is on a distinguished road

Re: pthread trick works with gcc 3.x.x, but not with 4.x.x


oops.

$ g++ -lpthread main1.cpp
main1.cpp: In member function ‘void Thread::Create()’:
main1.cpp:12: error: ‘thread_func’ was not declared in this scope
$

-antti
  #4  
Old 21-Apr-2008, 08:25
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: pthread trick works with gcc 3.x.x, but not with 4.x.x


You probably just need to let the compiler know of the signature of the function before implementing the method in the class.
Try to either declare the prototype of thread_func prior to the Class definition, or to simply move the entire function implementation before the class.

A code similar to the one you posted compiles fine in Visual Studio, and I don't have the Standard handy, nor the time to search in the Behemoth.
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #5  
Old 21-Apr-2008, 16:58
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: pthread trick works with gcc 3.x.x, but not with 4.x.x


Quote:
Originally Posted by LuciWiz
...declare the prototype of thread_func prior to the Class definition
That works for me.

Quote:
Originally Posted by LuciWiz
... move the entire function implementation before the class.
That doesn't, since the function does its thing with the cast to pointer to Thread...


With g++ version 4.1.2 on my Centos 5.1 Linux platform, the following works:


CPP / C++ / C Code:
#include <pthread.h>
#include <iostream>
using namespace std;

void *thread_func(void *p);

class Thread {

.
.
.
 //Everything else as in the original post
.
.
.

Output:
Code:
$ g++ -Wall -W test_pthread.cpp -lpthread -o test_pthread $ ./test_pthread main hello thread hello

Regards,

Dave
  #6  
Old 22-Apr-2008, 08:02
at8 at8 is offline
New Member
 
Join Date: Apr 2008
Posts: 3
at8 is on a distinguished road

Re: pthread trick works with gcc 3.x.x, but not with 4.x.x


Great, thanks to all.
That was a a really trivial error that I made.
-a
  #7  
Old 23-Apr-2008, 07:57
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: pthread trick works with gcc 3.x.x, but not with 4.x.x


Quote:
Originally Posted by davekw7x
That doesn't, since the function does its thing with the cast to pointer to Thread...

You are right. I wrote the suggestion without actually trying it out - sorry.
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:39.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.