Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #8227 > unrolled thread
| Started by | cerr <ron.eggler@gmail.com> |
|---|---|
| First post | 2011-07-25 21:15 -0700 |
| Last post | 2011-07-26 17:35 +0200 |
| Articles | 7 — 3 participants |
Back to article view | Back to comp.lang.c++
Re: simple pthread cerr <ron.eggler@gmail.com> - 2011-07-25 21:15 -0700
Re: simple pthread Ian Collins <ian-news@hotmail.com> - 2011-07-26 16:45 +1200
Re: simple pthread cerr <ron.eggler@gmail.com> - 2011-07-25 22:54 -0700
Re: simple pthread Paul Brettschneider <paul.brettschneider@yahoo.fr> - 2011-07-26 08:09 +0200
Re: simple pthread cerr <ron.eggler@gmail.com> - 2011-07-25 23:15 -0700
Re: simple pthread Ian Collins <ian-news@hotmail.com> - 2011-07-26 19:41 +1200
Re: simple pthread Paul Brettschneider <paul.brettschneider@yahoo.fr> - 2011-07-26 17:35 +0200
| From | cerr <ron.eggler@gmail.com> |
|---|---|
| Date | 2011-07-25 21:15 -0700 |
| Subject | Re: simple pthread |
| Message-ID | <34eba984-d360-46c0-a5ec-71abf8c1ee00@glegroupsg2000goo.googlegroups.com> |
> This looks like a mixed up function declaration!
Yep, exactly right... :o
>
>
> int something = pthread_create( );
>
> A decent compiler should tell you that MyThread::PrintMsg is the wrong
> function type for the function pointer parameter of pthread_create.
I'm using g++ and io got this now:
int MyThread::StartMe(void)
{
pthread_create(&ThreadA, NULL, &MyThread::PrintMsg, void);
}
//-------------------------------------------------------------
and the compiler tells me:
$ g++ -o example example.cpp
example.cpp: In member function ‘int MyThread::StartMe()’:
example.cpp:42:55: error: expected primary-expression before ‘void’
what is a primary expression in this case? :o
Thanks,
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-07-26 16:45 +1200 |
| Message-ID | <996v46FkjtU2@mid.individual.net> |
| In reply to | #8227 |
On 07/26/11 04:15 PM, cerr wrote:
>> This looks like a mixed up function declaration!
>
> Yep, exactly right... :o
>>
>>
>> int something = pthread_create( );
>>
>> A decent compiler should tell you that MyThread::PrintMsg is the wrong
>> function type for the function pointer parameter of pthread_create.
>
> I'm using g++ and io got this now:
> int MyThread::StartMe(void)
> {
> pthread_create(&ThreadA, NULL,&MyThread::PrintMsg, void);
> }
> //-------------------------------------------------------------
> and the compiler tells me:
> $ g++ -o example example.cpp
> example.cpp: In member function ‘int MyThread::StartMe()’:
> example.cpp:42:55: error: expected primary-expression before ‘void’
>
> what is a primary expression in this case? :o
> Thanks,
Firstly, please don't use the sociopathic "new" google interface, it
ruins threading!.
It looks like you missed my other comments, go be back and check, 'void'
isn't a valid parameter value and a member function isn't a valid thread
function.
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | cerr <ron.eggler@gmail.com> |
|---|---|
| Date | 2011-07-25 22:54 -0700 |
| Message-ID | <1f8bd996-7f31-4e5b-85c6-28325d00a7b6@f17g2000prf.googlegroups.com> |
| In reply to | #8228 |
On Jul 25, 9:45 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 07/26/11 04:15 PM, cerr wrote:
>
>
>
>
>
>
>
>
>
> >> This looks like a mixed up function declaration!
>
> > Yep, exactly right... :o
>
> >> int something = pthread_create( );
>
> >> A decent compiler should tell you that MyThread::PrintMsg is the wrong
> >> function type for the function pointer parameter of pthread_create.
>
> > I'm using g++ and io got this now:
> > int MyThread::StartMe(void)
> > {
> > pthread_create(&ThreadA, NULL,&MyThread::PrintMsg, void);
> > }
> > //-------------------------------------------------------------
> > and the compiler tells me:
> > $ g++ -o example example.cpp
> > example.cpp: In member function ‘int MyThread::StartMe()’:
> > example.cpp:42:55: error: expected primary-expression before ‘void’
>
> > what is a primary expression in this case? :o
> > Thanks,
>
> Firstly, please don't use the sociopathic "new" google interface, it
> ruins threading!.
Alright, switched back.. hopefully they'll fix that soon, I like the
new interface...
hoooooooooowever...
>
> It looks like you missed my other comments, go be back and check, 'void'
> isn't a valid parameter value and a member function isn't a valid thread
> function.
Alright, so how can I pass no parameter then, NULL or (void) NULL
don't work either...
So I cannot have a thread in a class? Alright then...would a different
class by okay or does it need to be global?
Thanks!
[toc] | [prev] | [next] | [standalone]
| From | Paul Brettschneider <paul.brettschneider@yahoo.fr> |
|---|---|
| Date | 2011-07-26 08:09 +0200 |
| Message-ID | <8d4f7$4e2e5a25$506c0d9c$24252@news.chello.at> |
| In reply to | #8229 |
cerr wrote: >> It looks like you missed my other comments, go be back and check, 'void' >> isn't a valid parameter value and a member function isn't a valid thread >> function. > > Alright, so how can I pass no parameter then, NULL or (void) NULL > don't work either... > So I cannot have a thread in a class? Alright then...would a different > class by okay or does it need to be global? It's probably not standards conformant, but under g++ you can use static member functions as argument to pthread_create. Pass this as the void* argument parameter. In the static member function reinterpret_cast<> to your class and call a real member function from this pointer. You could for example implement this in a Thread-class with a pure virtual doit() function.
[toc] | [prev] | [next] | [standalone]
| From | cerr <ron.eggler@gmail.com> |
|---|---|
| Date | 2011-07-25 23:15 -0700 |
| Message-ID | <5407c353-b2c4-4684-b94e-9a24c117f587@u28g2000prm.googlegroups.com> |
| In reply to | #8230 |
On Jul 25, 11:09 pm, Paul Brettschneider <paul.brettschnei...@yahoo.fr> wrote: > cerr wrote: > >> It looks like you missed my other comments, go be back and check, 'void' > >> isn't a valid parameter value and a member function isn't a valid thread > >> function. > > > Alright, so how can I pass no parameter then, NULL or (void) NULL > > don't work either... > > So I cannot have a thread in a class? Alright then...would a different > > class by okay or does it need to be global? > > It's probably not standards conformant, but under g++ you can use static > member functions as argument to pthread_create. Pass this as the void* > argument parameter. In the static member function reinterpret_cast<> to your > class and call a real member function from this pointer. > > You could for example implement this in a Thread-class with a pure virtual > doit() function. Alright, I wanna stick with standard c++ for this. But i'm still not sure how I can have the argument being void....anyone?
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-07-26 19:41 +1200 |
| Message-ID | <9979d6FkjuU13@mid.individual.net> |
| In reply to | #8231 |
On 07/26/11 06:15 PM, cerr wrote: > On Jul 25, 11:09 pm, Paul Brettschneider > <paul.brettschnei...@yahoo.fr> wrote: >> cerr wrote: >>>> It looks like you missed my other comments, go be back and check, 'void' >>>> isn't a valid parameter value and a member function isn't a valid thread >>>> function. >> >>> Alright, so how can I pass no parameter then, NULL or (void) NULL >>> don't work either... >>> So I cannot have a thread in a class? Alright then...would a different >>> class by okay or does it need to be global? >> >> It's probably not standards conformant, but under g++ you can use static >> member functions as argument to pthread_create. Pass this as the void* >> argument parameter. In the static member function reinterpret_cast<> to your >> class and call a real member function from this pointer. >> >> You could for example implement this in a Thread-class with a pure virtual >> doit() function. > Alright, I wanna stick with standard c++ for this. But i'm still not > sure how > I can have the argument being void....anyone? The parameter type is void*, not void. Pass NULL. -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Paul Brettschneider <paul.brettschneider@yahoo.fr> |
|---|---|
| Date | 2011-07-26 17:35 +0200 |
| Message-ID | <c0e7f$4e2edec2$506c0d9c$968@news.chello.at> |
| In reply to | #8231 |
cerr wrote:
> On Jul 25, 11:09 pm, Paul Brettschneider
> <paul.brettschnei...@yahoo.fr> wrote:
>> cerr wrote:
>> >> It looks like you missed my other comments, go be back and check,
>> >> 'void' isn't a valid parameter value and a member function isn't a
>> >> valid thread function.
>>
>> > Alright, so how can I pass no parameter then, NULL or (void) NULL
>> > don't work either...
>> > So I cannot have a thread in a class? Alright then...would a different
>> > class by okay or does it need to be global?
>>
>> It's probably not standards conformant, but under g++ you can use static
>> member functions as argument to pthread_create. Pass this as the void*
>> argument parameter. In the static member function reinterpret_cast<> to
>> your class and call a real member function from this pointer.
>>
>> You could for example implement this in a Thread-class with a pure
>> virtual doit() function.
> Alright, I wanna stick with standard c++ for this. But i'm still not
> sure how
> I can have the argument being void....anyone?
pthread_* is not standard C++ anyway, so why bother? If you want to be
conformant, I guess(!) that you will have to use a trampoline function
declared extern "C". You need the arg parameter for passing the pointer to
your object.
See for example this quickly hacked together piece of code (probably full of
"UB", but seems to compile and produce the expected unpredictable results):
#include <pthread.h>
#include <unistd.h>
#include <iostream>
// Forward declaration
extern "C" {
void *trampoline(void *arg);
}
class Thread
{
friend void *trampoline(void *arg);
protected:
virtual void event_loop() = 0;
public:
pthread_t threadid;
void start();
void join();
};
extern "C" {
void *trampoline(void *arg)
{
Thread *thread = static_cast<Thread *>(arg);
thread->event_loop();
return NULL;
}
}
void Thread::start()
{
pthread_create(&threadid, NULL, &trampoline, this);
}
void Thread::join()
{
pthread_join(threadid, NULL);
}
//-------------------------------------
class MyThread : public Thread {
void event_loop();
};
void MyThread::event_loop()
{
std::cout << "Hello\n";
}
//-------------------------------------
int main()
{
MyThread thread;
thread.start();
std::cout << "Thread id is/was " << thread.threadid << '\n';
thread.join();
}
$ g++ -Wall -O3 -lpthread thread.C
$ ./a.out
Thread id is/was 3078568816
Hello
$ ./a.out
Thread id is/was Hello
3078990704
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web