Solved

pass parameter to pthread_create

Posted on 2008-10-22
3
2,493 Views
Last Modified: 2012-05-05
Probably too easy to Google but easier to just ask the experts :P

I have a call to pthread_create(&tid, &attr, workthread, NULL);

I want to be able to send a mixed array as the parameters... to be more precise I want to pass an int (sockfd) a string (message received on the socket).

How do I do this and how to I handle it in workthread?

Cheers guys
0
Comment
Question by:mstrelan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 50 total points
ID: 22776823
You can pass a struct that contains the parameters you want to pass.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 200 total points
ID: 22779321
>How do I do this and how to I handle it in workthread?

int pthread_create(pthread_t *restrict thread,
                                const pthread_attr_t *restrict attr,
                                 void *(*start_routine)(void*),
                                 void *restrict arg);

the last parameter is the argument to the thread. It is a void * and hence you can cast any pointer type to a void *. Thread start routine also expects a void * argument. Inside the thread cast the void pointer to whatever type your thread is expecting. Since you need to pass a mix of values, it makes sense to bundle them in a struct as I08 already pointed out, cast a pointer to this struct to void * and pass it in the call to pthread_create and inside the routine cast it back to your struct pointer type. Something like
struct my_args {
      int val1;
      float val2;
      char * val3;
}
 
...
int main ()
{
   struct my_args args;
   ...
   args.val1 = 10;
   args.val2 = 3.14;
   args.val3 = "Hello world";
   pthread_create (&thread_id, &attr, launch_thread, &args );
   ...
}
 
void * launch_thread (void * args)
{
   struct my_args * pargs = (struct my_args *) args;
   printf ("My parameters %d %f %s\n", pargs->val1, pargs->val2, pargs->val3 );
   ...
}

Open in new window

0
 
LVL 10

Author Comment

by:mstrelan
ID: 22800742
NOTE!!!! There is a semi colon missing after the struct declaration
struct my_args {
      int val1;
      float val2;
      char * val3;
}; // <--------------- added semicolon 

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
valid enum? 6 75
Using ANSI C how to Read a .csv file 10 115
Test the speeds on my PC Drives 12 71
Setting variables in a stored procedure 5 52
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question