create thread - passing arguments

passing multiple arguments to threads in c
pass arguments to thread python
passing multiple arguments to threads in c
pass argument to thread c
passing data between threads c++
passing arguments in pthread_create
boost::thread with arguments

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++)
    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);
    //printf("\nThread %d produced: %d primes\n",i, prime);
    printf("\nThread %d produced: %d primes\n",i, primeArray[i]);

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.

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;
    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)) {
            //goto error_handler;
    return 0;

Passing arguments |, Passing arguments to pthread function We use "pthread_create" to create a thread, thread id is the first argument, NULL is the second argument(which should  Firstly, you need to create a new thread in C# − Thread thread = new Thread(threadDemo); Above, threadDemo is our thread function. Now pass a parameter to the thread − thread.Start(str); The parameter set above is − String str = "Hello World!"; Example. Let us see the complete code to pass a parameter to a thread in C#. Live Demo

In case of std::thread, the user can pass arguments to the thread function in the following method


for instance,

//for a thread function, 
void threadFunction(int x,int y){
   std::cout << x << y << std::endl;

// u can pass x and y values as below
std::thread mTimerThread;
mTimerThread = std::thread(threadFunction,1,12);

sending arguments to function using CreateThread, The parameter is a pointer to void, you can send whatever you like if you're sufficiently creative. For example, pass a structure with members  Thread Fundamentals As a quick reminder, we can create a thread in Java by implementing Runnable or Callable. To run a thread, we can invoke Thread#start (by passing an instance of Runnable) or use a thread pool by submitting it to an ExecutorService. Neither of these approaches accepts any extra parameters, though.

This is the code of Manakarse , everything is really good but you need a


just to be sure all of threads will successfully execute before end of main thread("main will "waiting" while all of threads aren't finished yet) ;

C++11 Multithreading – Part 3: Carefully Pass Arguments to Threads , To Pass arguments to thread's associated callable object or function just std::​cout<<"In Main Thread : Before Thread Start x = "<<x<<std::endl;. Passing simple arguments to a std::thread in C++11. #include <iostream>. #include <string>. #include <thread>. void threadCallback(int x, std::string str) {. std::cout<<"Passed Number = "<<x<<std::endl; std::cout<<"Passed String = "<<str<<std::endl;

How to pass arguments to and get results from threads., For more information, see Creating Threads and Passing Data at Start Time. The ParameterizedThreadStart delegate supports only a single parameter. You can  End Sub End Class Public class Simple Public Shared Sub Main () Dim serverObject As New ServerClass () ' Create the thread object, passing in the ' serverObject.InstanceMethod method using a ' ThreadStart delegate. Dim InstanceCaller As New Thread (AddressOf serverObject.InstanceMethod) ' Start the thread.

C++ - Passing Arguments To Threads, Passing Arguments to Threads. ▫ pthread_create(). ○ All arguments must be passed by reference and cast to (void *). ○ Only one argument to the thread start​  Visual Basic and C# users can omit the ThreadStart or ParameterizedThreadStart delegate constructor when creating a thread. In Visual Basic, use the AddressOf operator when passing your method to the Thread constructor; for example, Dim t As New Thread(AddressOf ThreadProc). In C#, simply specify the name of the thread procedure.

ParameterizedThreadStart Delegate (System.Threading), 1) Creates new thread object which does not represent a thread. 2) Move constructor. thread. args -, arguments to pass to the new function  Let’s create an object of class Task i.e. Task * taskPtr = new Task(); Now let’s create a Thread that will use this member function execute() as thread function through its object i.e. // Create a thread using member function std::thread th(&Task::execute, taskPtr, "Sample Task"); Here in std::thread constructor we passed 3 arguments i.e.

  • see answer below, also check out link for a good pthread reference site.
  • Wrong language, C, not C++