Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Macro for pthread_create()

Reply
Thread Tools

Macro for pthread_create()

 
 
daniele.g
Guest
Posts: n/a
 
      01-10-2012
For code portability I need to write a macro THREAD(func, param) which
must be resolved into pthread_create(&func_id, NULL, func, (void *)
param)

I wrote this:

#define THREAD(func,parm) (pthread_create(&func, NULL, func, (void *)
parm))

But it doesn't work. Any clue?

Thanks in advance.
--
Volare e' utile, atterrare e' necessario.
-- Eros Drusiani
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      01-10-2012
daniele.g <> wrote:
> For code portability I need to write a macro THREAD(func, param) which
> must be resolved into pthread_create(&func_id, NULL, func, (void *)
> param)


> I wrote this:


> #define THREAD(func,parm) (pthread_create(&func, NULL, func, (void *)
> parm))


> But it doesn't work. Any clue?


pthread_create() takes (at least in the POSIX version) a
pointer to a pthred_t variable (that can't be NULL and must
be an address the function can write to) as its first ar-
gument, not a function (and also not a pointer to the
pointer to the function to be called). So this macro
can't "work". And what has using such a macro to do with
"portability"?
Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      01-10-2012
On 01/10/2012 01:21 PM, daniele.g wrote:
> For code portability I need to write a macro THREAD(func, param) which
> must be resolved into pthread_create(&func_id, NULL, func, (void *)
> param)


Where does func_id come from?

> I wrote this:
>
> #define THREAD(func,parm) (pthread_create(&func, NULL, func, (void *)
> parm))


There's no func_id in the expansion of that macro.

pthread_create() isn't part of the C standard library. As far as this
newsgroup is concerned, it could be anything, and the correct answer to
your question depends upon precisely how pthead_create is declared.
comp.programming.unix would be a more appropriate forum for such a question.

> But it doesn't work. Any clue?


What exactly does "doesn't work" mean? Before you post to
comp.programming.unix, create a simplified version of your program, as
small as possible, which demonstrates the problem. Do a test build and
run to make sure it actually demonstrates the problem. Give them the
exact text of the program that failed. Tell them precisely which
compiler you're using, the command line options you used, and precisely
what error messages, if any, you got when you compiled and linked it. If
"doesn't work" refers to something that happens when you run the
program, after successfully compiling and linking it, then tell them
precisely what input you gave the program, and what precisely the output
was that convinced you that "it doesn't work".
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      01-10-2012
On 2012-01-10, daniele.g <> wrote:
> For code portability I need to write a macro THREAD(func, param) which
> must be resolved into pthread_create(&func_id, NULL, func, (void *)
> param)


> I wrote this:


> #define THREAD(func,parm) (pthread_create(&func, NULL, func, (void *)
> parm))


> But it doesn't work. Any clue?


Nope.

Suggestion: Maybe you should expand a little on "doesn't work". You
might have meant:
I get a compiler error.
My program crashes.
I looked at preprocessed output and it wasn't what I expected.

The obvious thing that leaps out at me is that you say you want
pthread_create(&func_id, NULL, func, (void *) param)
but you wrote
pthread_create(&func, NULL, func, (void *) param)

which makes me wonder whether you actually wanted the _id, and if so,
why you omitted it. You may be looking for the "token pasting" operator,
##. See the comp.lang.c FAQ for more info on that.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      01-10-2012
Jens Thoms Toerring <> wrote:
> daniele.g <> wrote:
> > For code portability I need to write a macro THREAD(func, param) which
> > must be resolved into pthread_create(&func_id, NULL, func, (void *)
> > param)


> > I wrote this:


> > #define THREAD(func,parm) (pthread_create(&func, NULL, func, (void *)
> > parm))


> > But it doesn't work. Any clue?


> pthread_create() takes (at least in the POSIX version) a
> pointer to a pthred_t variable (that can't be NULL and must
> be an address the function can write to) as its first ar-
> gument, not a function (and also not a pointer to the
> pointer to the function to be called). So this macro
> can't "work". And what has using such a macro to do with
> "portability"?


Sorry, I overlooked that it's 'func_id' in what you want the
macro to expand to. With that I can only guess at your inten-
tions: do you always have a variable for the thread ID (i.e.
the first argument) that has the same name as the function,
but with '_id' appended to it? In that case you could try

#define THREAD(func,parm) pthread_create(&#func_id, NULL, func, (void *) parm)

(assuming that your compiler understands the concatenation
bit '##', which is, as far as I remember from C99).

With this

THREAD( a, b )

should expand to

pthread_create(&a_id, NULL, a, (void *) b)

Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-10-2012
(Jens Thoms Toerring) writes:
<snip>
>> daniele.g <> wrote:
>> > For code portability I need to write a macro THREAD(func, param) which
>> > must be resolved into pthread_create(&func_id, NULL, func, (void *)
>> > param)

>
>> > I wrote this:

>
>> > #define THREAD(func,parm) (pthread_create(&func, NULL, func, (void *)
>> > parm))

>
>> > But it doesn't work. Any clue?

<snip>
> Sorry, I overlooked that it's 'func_id' in what you want the
> macro to expand to. With that I can only guess at your inten-
> tions: do you always have a variable for the thread ID (i.e.
> the first argument) that has the same name as the function,
> but with '_id' appended to it? In that case you could try
>
> #define THREAD(func,parm) pthread_create(&#func_id, NULL, func, (void *) parm)
>
> (assuming that your compiler understands the concatenation
> bit '##', which is, as far as I remember from C99).


It's as old as C89 ("ANSI C") and you've got a typo in the macro. You
presumable wanted to write:

#define THREAD(func,parm) \
pthread_create(&func ## _id, NULL, func, (void *)parm)

<snip>
--
Ben.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      01-10-2012
On 2012-01-10, James Kuyper <> wrote:
> On 01/10/2012 01:21 PM, daniele.g wrote:
>> For code portability I need to write a macro THREAD(func, param) which
>> must be resolved into pthread_create(&func_id, NULL, func, (void *)
>> param)


> Where does func_id come from?


My guess is that the intent is that if you call THREAD(foo, bar), the OP
wants
pthread_create(&foo_id, NULL, foo, (void *) bar);

> pthread_create() isn't part of the C standard library. As far as this
> newsgroup is concerned, it could be anything, and the correct answer to
> your question depends upon precisely how pthead_create is declared.


I am not sure of that -- so far as I can tell, this one's purely a textual
representation question about the preprocessing phases.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      01-10-2012
Ben Bacarisse <> wrote:
> (Jens Thoms Toerring) writes:
> <snip>
> >> daniele.g <> wrote:
> >> > For code portability I need to write a macro THREAD(func, param) which
> >> > must be resolved into pthread_create(&func_id, NULL, func, (void *)
> >> > param)

> >
> >> > I wrote this:

> >
> >> > #define THREAD(func,parm) (pthread_create(&func, NULL, func, (void *)
> >> > parm))

> >
> >> > But it doesn't work. Any clue?

> <snip>
> > Sorry, I overlooked that it's 'func_id' in what you want the
> > macro to expand to. With that I can only guess at your inten-
> > tions: do you always have a variable for the thread ID (i.e.
> > the first argument) that has the same name as the function,
> > but with '_id' appended to it? In that case you could try
> >
> > #define THREAD(func,parm) pthread_create(&#func_id, NULL, func, (void *) parm)
> >
> > (assuming that your compiler understands the concatenation
> > bit '##', which is, as far as I remember from C99).


> It's as old as C89 ("ANSI C")


Thanks, had somehow associated it with C99.

> and you've got a typo in the macro. You presumable wanted
> to write:
> #define THREAD(func,parm) \
> pthread_create(&func ## _id, NULL, func, (void *)parm)


Uups, yes, of course! I thought I had copy-and-paste'd it from
my test program but I seem to have screwed that up

Best regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de
 
Reply With Quote
 
daniele.g
Guest
Posts: n/a
 
      01-10-2012
Seebs <usenet-> writes:

> The obvious thing that leaps out at me is that you say you want
> pthread_create(&func_id, NULL, func, (void *) param)
> but you wrote
> pthread_create(&func, NULL, func, (void *) param)
>
> Which makes me wonder whether you actually wanted the _id, and if so,
> why you omitted it. You may be looking for the "token pasting" operator,
> ##. See the comp.lang.c FAQ for more info on that.


Yur're right, I wrote wrong the macro expansion.
What I want is that when I compile this source under linux the macro is
translated into the pthread_create(...) function, knowing that the macro
has only two parameters. My problem is that I don't know how to create
dinamically a func_id to be inserted into the macro definition.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-10-2012
(daniele.g) writes:

> Seebs <usenet-> writes:
>
>> The obvious thing that leaps out at me is that you say you want
>> pthread_create(&func_id, NULL, func, (void *) param)
>> but you wrote
>> pthread_create(&func, NULL, func, (void *) param)
>>
>> Which makes me wonder whether you actually wanted the _id, and if so,
>> why you omitted it. You may be looking for the "token pasting" operator,
>> ##. See the comp.lang.c FAQ for more info on that.

>
> Yur're right, I wrote wrong the macro expansion.
> What I want is that when I compile this source under linux the macro is
> translated into the pthread_create(...) function, knowing that the macro
> has only two parameters. My problem is that I don't know how to create
> dinamically a func_id to be inserted into the macro definition.


If there is a fixed, textual, relationship between "func" and "func_id"
(i.e. the first parameter is always the third but with _id added to the
name) then the token pasting macro already posted (it uses "func ## _id"
in the body) will do the trick.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dedicated Macro or Normal Macro? John Ortt Digital Photography 5 11-22-2005 12:43 PM
Macro lens on a camera with a macro setting??? mitchell.chris@gmail.com Digital Photography 2 09-28-2005 07:55 AM
in S.E. Asia : Canon EOS 300d with 100 macro ED vs. Nikon D70 with Nikon 105 macro ? J. Cod Digital Photography 0 09-29-2004 05:46 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57