Untitled
raw download clone
TEXT
views 18
,
size 1618 b
Type to search
Outline
1. Introduction
2. Configuration
3. Compile
4. Race Condition
5. Deadlock
6. Starvation
7. Passing arguments
8. Answers
9. Useful links
Published with GitBook
Passing arguments to pthread function
Recall the helloworld program you compile in the "Compile" section:
#include <stdio.h>
#include <pthread.h>

void * hello(void *input) {
    printf("%s\n", (char *)input);
    pthread_exit(NULL);
}

int main(void) {
    pthread_t tid;
    pthread_create(&tid, NULL, hello, "hello world");
    pthread_join(tid, NULL);
    return 0;
}
We use "pthread_create" to create a thread, thread id is the first argument, NULL is the second argument(which should be some attribute, but we may not use it), the third argument is the function, then the last argument is what we want to pass to the new thread.
When we define the function hello(void *), this function can accept a pointer as an argument.
In this example we just pass one string to the function, what if we want to pass more than one argument? We can use structure in c.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

struct args {
    char* name;
    int age;
};

void *hello(void *input) {
    printf("name: %s\n", ((struct args*)input)->name);
    printf("age: %d\n", ((struct args*)input)->age);
}

int main() {
    struct args *Allen = (struct args *)malloc(sizeof(struct args));
    char allen[] = "Allen";
    Allen->name = allen;
    Allen->age = 20;

    pthread_t tid;
    pthread_create(&tid, NULL, hello, (void *)Allen);
    pthread_join(tid, NULL);
    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.