Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8753895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:31:08+00:00 2026-06-13T13:31:08+00:00

I am attempting on creating multiple threads that each thread calculates a prime. I

  • 0

I am attempting on creating multiple threads that each thread calculates a prime. I am trying to pass a second argument to a function using thread create. It keeps throwing up errors.

void* compute_prime (void* arg, void* arg2)
{

here is my main() with the create thread. &primeArray[i] after &max_prime is giving me the errors.

 for(i=0; i< num_threads; i++)
 {
    primeArray[i]=0;
    printf("creating threads: \n");
    pthread_create(&primes[i],NULL, compute_prime, &max_prime, &primeArray[i]);
    thread_number = i;
    //pthread_create(&primes[i],NULL, compPrime, &max_prime);
 }

 /* join threads */
 for(i=0; i< num_threads; i++)
{
    pthread_join(primes[i], NULL);
    //pthread_join(primes[i], (void*) &prime);
    //pthread_join(primes[i],NULL);
    //printf("\nThread %d produced: %d primes\n",i, prime);
    printf("\nThread %d produced: %d primes\n",i, primeArray[i]);
    sleep(1);
}

the error i get is:

myprime.c: In function âmainâ:
myprime.c:123: warning: passing argument 3 of âpthread_createâ from incompatible pointer type
/usr/include/pthread.h:227: note: expected âvoid * (*)(void *)â but argument is of type âvoid * (*)(void *, void *)â
myprime.c:123: error: too many arguments to function âpthread_createâ

It works fine if i take out the second argument.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:31:09+00:00Added an answer on June 13, 2026 at 1:31 pm

    You can only pass a single argument to the function that you are calling in the new thread. Create a struct to hold both of the values and send the address of the struct.

    #include <pthread.h>
    #include <stdlib.h>
    typedef struct {
        //Or whatever information that you need
        int *max_prime;
        int *ith_prime;
    } compute_prime_struct;
    
    void *compute_prime (void *args) {
        compute_prime_struct *actual_args = args;
        //...
        free(actual_args);
        return 0;
    }
    #define num_threads 10
    int main() {
        int max_prime = 0;
        int primeArray[num_threads];
        pthread_t primes[num_threads];
        for (int i = 0; i < num_threads; ++i) {
            compute_prime_struct *args = malloc(sizeof *args);
            args->max_prime = &max_prime;
            args->ith_prime = &primeArray[i];
            if(pthread_create(&primes[i], NULL, compute_prime, args)) {
                free(args);
                //goto error_handler;
            }
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to parse a function call for a language that I am creating,
I'm attempting to learn how to use Cucumber and creating steps using the following
I'm creating customAttributes in Raphaeljs . I'm attempting to pass additional arguments into an
I'm attempting at creating a menu from a table using the Suckerfish css menu
Possible Duplicate: Creating multiple numbers with certain number of bits set I'm attempting to
I am creating an HttpListener by attempting to grab a random port that is
I'm creating a small forum. Attempting to run SElECT... JOIN... query too pick up
i'm attempting to instantiate a bunch of sounds by creating a string array containing
I am creating a web application with MVC3 and MEF. I am attempting to
I'm creating a Validator<T> class. I'm attempting to implement the Linq SelectMany extension methods

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.