pthread_create - parameter passing?

pthread_create - parameter passing?

Post by Gernot Kerschbaume » Wed, 12 May 1999 04:00:00



hi dear developers,

i'm programming an application in c++ which does some output to standard
output. now i want it to output into an xwindow, launched by the very
application. for the event-loop of the xwindow, i started a thread by calling
pthread_create(). this function has as its last argument a pointer to
arguments which should be passed to the function which is to be running
in a separate thread (third argument of pthread_create()).

my problem is now that i can't pass the pointer to the threaded function.
the void* argument to this function points to a different location than
the argument pointer of pthread_create.

i would appreciate any help on how to pass data to the threaded function.
thanks in advance,

gernot kerschbaumer

--
,-------------------------------------------------------------------------.

| vereinsgasse 32/12     | fax:      +43 1 2160493                        |
| a-1020 wien            | homepage: http://stud2.tuwien.ac.at/~e9018967/ |
`-------------------------------------------------------------------------'  

 
 
 

pthread_create - parameter passing?

Post by Zoran Cutur » Wed, 12 May 1999 04:00:00



> hi dear developers,

> i'm programming an application in c++ which does some output to standard
> output. now i want it to output into an xwindow, launched by the very
> application. for the event-loop of the xwindow, i started a thread by calling
> pthread_create(). this function has as its last argument a pointer to
> arguments which should be passed to the function which is to be running
> in a separate thread (third argument of pthread_create()).

> my problem is now that i can't pass the pointer to the threaded function.
> the void* argument to this function points to a different location than
> the argument pointer of pthread_create.

> i would appreciate any help on how to pass data to the threaded function.
> thanks in advance,

> gernot kerschbaumer

> --
> ,-------------------------------------------------------------------------.

> | vereinsgasse 32/12     | fax:      +43 1 2160493                        |
> | a-1020 wien            | homepage: http://stud2.tuwien.ac.at/~e9018967/ |
> `-------------------------------------------------------------------------'

Hi,

That's how I did it:

main() {

if ( pthread_create(&tidmythreadnoreturnvalue, NULL,
mythreadnoreturnvalue, NULL) != 0){

Quote:}
}

static void * mythreadnoreturnvalue(void * c) {

Quote:}

Hope this helps!

Bye
        Zoran

--
LISP is worth learning for the profound enlightenment experience you
will have when you finally get it; that experience will make you a
better programmer for the rest of your days.         Eric S. Raymond
------------------------------------------------------------------------
   _/_/_/_/_/    _/_/_/_/ from:  Zoran Cutura,
          _/   _/      _/     IMH-Innovative Motorentechnik Prof. Huber,
        _/    _/          post:  DaimlerChrysler AG, EP/VES, T900,
      _/     _/                  70546 Stuttgart, Germany,
    _/      _/            phone: +49711 17-42353
  _/       _/       _/    mobil: +49171 4488407

       PGP fingerprint: F0 C3 30 F4 B3 7E 22 36  1C 51 B7 60 A9 BB 23 BE

 
 
 

pthread_create - parameter passing?

Post by Zoran Cutur » Wed, 12 May 1999 04:00:00



> > That's how I did it:

> > main() {

> > if ( pthread_create(&tidmythreadnoreturnvalue, NULL,
> > mythreadnoreturnvalue, NULL) != 0){
> > }

> > }

> > static void * mythreadnoreturnvalue(void * c) {
> > }

> hi,
> thank you for your answer. but my problem was to pass a parameter
> over to mythreadnoreturnvalue. in your example, NULL is passed from
> pthread_create() to mythreadnoreturnvalue.

> what i want is:

> struct mystruct;

you mean:

struct mystruct *myargsp;

or

struct mystruct myargs;

Quote:

> pthread_create(&tidmythreadnoreturnvalue, NULL, mythreadnoreturnvalue,
>                &mystruct);

pthread_create(&tidmythreadnoreturnvalue, NULL, mythreadnoreturnvalue,
            myargsp);

or

pthread_create(&tidmythreadnoreturnvalue, NULL, mythreadnoreturnvalue,
            &myargs);

Quote:

> and then:

> static void *mythreadnoreturnvalue(void *c) {
>    mystruct *thestruct = (mystruct*)c;
>    // working on mystruct
> }

static void *mythreadnoreturnvalue(void *c) {
   struct mystruct *thestruct;

   thestruct = (struct mystruct *) c;
   // working on mystruct

}
> and the problem is, the pointer isn't passed.

> i hope this description was more clear.

> --
> ,-------------------------------------------------------------------------.

> | vereinsgasse 32/12     | fax:      +43 1 2160493                        |
> | a-1020 wien            | homepage: http://stud2.tuwien.ac.at/~e9018967/ |
> `-------------------------------------------------------------------------'

This is what W.R. Stevens says (UNIX Network Programming Vol2 IPC,
ISBN 0-13-081081-9)

Bye
        Zoran

PS.: You should put this to the newsgroup too!

--
LISP is worth learning for the profound enlightenment experience you
will have when you finally get it; that experience will make you a
better programmer for the rest of your days.         Eric S. Raymond
------------------------------------------------------------------------
   _/_/_/_/_/    _/_/_/_/ from:  Zoran Cutura,
          _/   _/      _/     IMH-Innovative Motorentechnik Prof. Huber,
        _/    _/          post:  DaimlerChrysler AG, EP/VES, T900,
      _/     _/                  70546 Stuttgart, Germany,
    _/      _/            phone: +49711 17-42353
  _/       _/       _/    mobil: +49171 4488407

       PGP fingerprint: F0 C3 30 F4 B3 7E 22 36  1C 51 B7 60 A9 BB 23 BE

 
 
 

pthread_create - parameter passing?

Post by Gernot Kerschbaume » Wed, 12 May 1999 04:00:00


:>
:> what i want is:
:>
:> struct mystruct;

: you mean:

: struct mystruct *myargsp;

: or

: struct mystruct myargs;

yes, you are right.
and then pass &myargs as fourth parameter to pthread_create().

: pthread_create(&tidmythreadnoreturnvalue, NULL, mythreadnoreturnvalue,
:             &myargs);

: static void *mythreadnoreturnvalue(void *c) {
:    struct mystruct *thestruct;

:    thestruct = (struct mystruct *) c;
:    // working on mystruct
: }

but now i noticed that i didn't declare mythreadnoreturnvalua as static.

:> and the problem is, the pointer isn't passed.

i will then investigate if this is still the case.

:>
:> i hope this description was more clear.
:>
:> --
:> ,-------------------------------------------------------------------------.

:> | vereinsgasse 32/12     | fax:      +43 1 2160493                        |
:> | a-1020 wien            | homepage: http://stud2.tuwien.ac.at/~e9018967/ |
:> `-------------------------------------------------------------------------'

: This is what W.R. Stevens says (UNIX Network Programming Vol2 IPC,
: ISBN 0-13-081081-9)

well, what says he?

--
,-------------------------------------------------------------------------.

| vereinsgasse 32/12     | fax:      +43 1 2160493                        |
| a-1020 wien            | homepage: http://stud2.tuwien.ac.at/~e9018967/ |
`-------------------------------------------------------------------------'  

 
 
 

pthread_create - parameter passing?

Post by Zoran Cutur » Wed, 12 May 1999 04:00:00





> :>
> :> what i want is:
> :>
> :> struct mystruct;

> : you mean:

> : struct mystruct *myargsp;

> : or

> : struct mystruct myargs;

> yes, you are right.
> and then pass &myargs as fourth parameter to pthread_create().

> : pthread_create(&tidmythreadnoreturnvalue, NULL, mythreadnoreturnvalue,
> :             &myargs);

> : static void *mythreadnoreturnvalue(void *c) {
> :    struct mystruct *thestruct;

> :    thestruct = (struct mystruct *) c;
> :    // working on mystruct
> : }

> but now i noticed that i didn't declare mythreadnoreturnvalua as static.

> :> and the problem is, the pointer isn't passed.

> i will then investigate if this is still the case.

> :>
> :> i hope this description was more clear.
> :>
> :> --
> :> ,-------------------------------------------------------------------------.

> :> | vereinsgasse 32/12     | fax:      +43 1 2160493                        |
> :> | a-1020 wien            | homepage: http://stud2.tuwien.ac.at/~e9018967/ |
> :> `-------------------------------------------------------------------------'

> : This is what W.R. Stevens says (UNIX Network Programming Vol2 IPC,
> : ISBN 0-13-081081-9)

> well, what says he?

sorry, I mean I checked back that code from his book!

--
LISP is worth learning for the profound enlightenment experience you
will have when you finally get it; that experience will make you a
better programmer for the rest of your days.         Eric S. Raymond
------------------------------------------------------------------------
   _/_/_/_/_/    _/_/_/_/ from:  Zoran Cutura,
          _/   _/      _/     IMH-Innovative Motorentechnik Prof. Huber,
        _/    _/          post:  DaimlerChrysler AG, EP/VES, T900,
      _/     _/                  70546 Stuttgart, Germany,
    _/      _/            phone: +49711 17-42353
  _/       _/       _/    mobil: +49171 4488407

       PGP fingerprint: F0 C3 30 F4 B3 7E 22 36  1C 51 B7 60 A9 BB 23 BE

 
 
 

pthread_create - parameter passing?

Post by Gernot Kerschbaume » Wed, 12 May 1999 04:00:00


:>
:> but now i noticed that i didn't declare mythreadnoreturnvalua as static.
:>
:> :> and the problem is, the pointer isn't passed.
:>
:> i will then investigate if this is still the case.

that was it. because mythreadnoreturnvalue was not declared static,
there were problems with argument passing. now it works. i hope this
helps other people from this newsgroup too. thanks.

--
,-------------------------------------------------------------------------.

| vereinsgasse 32/12     | fax:      +43 1 2160493                        |
| a-1020 wien            | homepage: http://stud2.tuwien.ac.at/~e9018967/ |
`-------------------------------------------------------------------------'  

 
 
 

pthread_create - parameter passing?

Post by Roderick Groesbee » Fri, 21 May 1999 04:00:00


        Goodday Gernot,


> hi dear developers,

> i'm programming an application in c++ which does some output to standard
> output. now i want it to output into an xwindow, launched by the very
> application. for the event-loop of the xwindow, i started a thread by
calling
> pthread_create(). this function has as its last argument a pointer to
> arguments which should be passed to the function which is to be running
> in a separate thread (third argument of pthread_create()).

> my problem is now that i can't pass the pointer to the threaded function.
> the void* argument to this function points to a different location than
> the argument pointer of pthread_create.

> i would appreciate any help on how to pass data to the threaded function.
> thanks in advance,

You have 2 options.

1) Use a seperate C-function.
2) Use a static Class method.

Ad 1)
Something like this:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
extern "C" void* startThread(void* pVoid)
{
  Class* pClass = (Class*) pvoid;

  pClass->TheadMain();
  /* Note that you cannot easily pass arguments! You have to embed
     them in the class. But hey! It is C++. You only use Methods! :c) */

Quote:}

class Class
{
   Class()
   {
        pthread_create(&m_thrId[m_nThreadCount++], NULL, startThread,this);
   }

   int ThreadMain()
   {
        /* Youre Thready code... */
   }

Quote:};

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ad2)
Use a static Method in youre class.
Caution! This is against all linkage specifications!
But it works, and it is how it *should* work on all systems.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Main
{
 Main()
 {
   void* pArg = /* Some nice arguments you can give now.. */
   pthread_create(&thrId[m_nThreadCount++], NULL, StartThread, pArg);
 }

 static void* StartThread(void* pArg)
 {
        /* Youre Trendy Thread code comes here... */
 }

Quote:};
> gernot kerschbaumer

Kind Regards,

Roderick
--
Pettemerstraat 12A                                  T r I p l e
1823 CW Alkmaar                                         T
Tel. +31 (0)72-5129516
fax. +31 (0)72-5129520                              Automatisering
www.triple-it.nl                                 "Laat uw Net Werken!"

------------------  Posted via SearchLinux  ------------------
                  http://www.searchlinux.com

 
 
 

1. passing parameter to shell function as in the normal unix commands (not positional parameter)

Hi,

I have a function defined in in .profile which accepts three
parameters.

function myfn
{
var1=$1
var1=$3
var1=$3

I am wondering, whether the function can be invkoded as

"myfun -c var1 -d var2 -e var3"

The reason, I would prefer this method, because I don't have to know
the order of the parameters and optionally I can ignore one or more of
the parameter and set the default value, inside the program if needed.

note: I know, it can be invoked as "myfun var1 var2 var3"
      I am using ksh under AIX 5.2

Thanks,
Prince.

2. xdm.........Going round in circles!

3. passing parameters, problem

4. ACPI and battery information, 2.5.69

5. Passing Parameters to Korn Shell script

6. why freebsd?

7. Passing Parameters to Script

8. HP720C

9. LILO won't pass ether= parameter

10. passing parameters to printer (ghostscript)

11. passing parameters with spaces to shell function

12. Parameter Passing

13. question about m4 and passing parameters