×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

pthread_create question

pthread_create question

pthread_create question

(OP)
Hey, kinda feels like I'm in over my head here, but trying to learn how to use pthread_create and am having problems with passing in arguments. Right now, I have
 
 a = pthread_create(&thread1, NULL, MatrixTimes, (Matrixa, 0, i, MAX, Matrixb));

in my code calling pthread create, and

void MatrixTimes(int Matrixa[5][5], int start, int end, int max, int Matrixb[5][5])

as my function prototype. What I can gather from reading and the internet is that I need to create a void pointer and pass my arguements in that way... and that's where I go over my head. I see how it works with one item passed in, but looking to pass in several, 5 to be exact. Any help, or directions to a good pthread tutorial, would be much appreciated. Thanks.

RE: pthread_create question

If you've got multiple arguments, then you need to put them into a struct, then pass a pointer to an instance of that struct.

CODE

struct matArgs {
    int Matrixa[5][5];
    int start;
    int end;
    int max;
    int Matrixb[5][5];
};

Which would be used as

CODE

struct matArgs args;
// fill in args
a = pthread_create(&thread1, NULL, MatrixTimesWrapper, &args);

Where MatrixTimesWrapper is something like

CODE

void *MatrixTimesWrapper ( void *p ) {
    struct matArgs *m = p;
    MatrixTimes(m->Matrixa, m->start, m->end, m->max, m->Matrixb);
    return NULL;
}

If you're intending to use the same function over and over, in different threads, with different arguments, then it's a really good idea to use malloc() to allocate each thread-specific instance of matArgs where the threads are created, and call free() inside the thread when it has finished looking at the parameters.
 

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close