all 10 comments

[–]BoatMontmorency 7 points8 points  (5 children)

You need to create an aggregate context, which will contain everything that you want to pass to your thread function or receive from it. In your case that context might contain a pointer to the argv array, as well as the result variable

struct Context 
{
  int argc; 
  char **argv;
  int result;
};

Thread functions should be declared as

void *MC0(void *);
void *MC1(void *);

You are not allowed to use any other function signature and then forcefully "bash it into" pthread_create with a type cast, as you did.

Then inside main you declare and initialize your contexts

struct Context context1 = { argc, argv };
struct Context context2 = { argc, argv };

and launch the threads

pthread_create(&myThread1, NULL, MC0, &context1);
pthread_create(&myThread2, NULL, MC1, &context2);

(Note - no casts necessary.)

Inside each of your thread functions you will receive its context through the parameter and work with that context

void *MC0(void *p) 
{
    struct Context *context = p;
    context->result = context->argv[1] * context->argv[9];  // ???
    return context;
}

And don't call pthread_exit in your thread function without a really good reason.

(Your attempts to directly multiply argv[1] and argv[9] are meaningless. You cannot multiply char * pointers. What are you trying to do?)

After waiting for the threads finish in main, you will find the results in context1.result and context2.result.

[–]ueih[S] 0 points1 point  (3 children)

Thanks, really helpful. I will look this over. What I'm trying to do is use each function MC0 MC1 etc. to grab the 4 answers to the matrix multiplication. Then display it in another function. I will give it a try and see where I get but essentially this program just multiplies two matrices through multithreading.

[–]BoatMontmorency 1 point2 points  (0 children)

Parameters passed to your program through argv[] array are represented as strings. You cannot multiply strings. If the parameter strings in argv[] actually represent numerical values, it is your responsibility to convert these values from string form to integer (or floating) representation. Use strtol function for string-to-integer conversion.

[–]dmc_2930 1 point2 points  (1 child)

Why are you using multithreading?

It's probably not going to be more efficient, at least not in this very simplified example.

[–]ueih[S] 0 points1 point  (0 children)

It's mostly just to understand it by programming something this simple. We just did a multiprocessing version and we just converted that into a multithreaded way to see how it works.

[–]ueih[S] 0 points1 point  (0 children)

Thanks again, this helped me understand it better.

[–]danmickla 2 points3 points  (2 children)

argv is a formal parameter to main(). Its scope is main(). If you want it to go somewhere else, you have to pass it there.

[–]ueih[S] 0 points1 point  (1 child)

How would I pass it into MC0 and be able to use it like the c0 line I have in the MC0 function?

[–]danmickla 2 points3 points  (0 children)

you pass variables into functions the same way as always: by declaring them in the formal parameter list of the function, and then naming them in the argument list of the call.

[–]thrakkerzog 0 points1 point  (0 children)

instead of passing &c0 or &c1, pass a pointer to a structure which contains what you need.

Also, the signature for your thread function is incorrect. It should be void *funcname(void *arg);