Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple arguments to a thread in C (pthread_create)

I am trying to pass 2 unsigned integers to a newly created thread in C (using pthread_create()) but nor an array of 2 integers or a struct seems to work.

// In my socket file

struct dimension {
    unsigned int width;
    unsigned int height;
};

unsigned int width, height;

void setUpSocket(void* dimension) {

    struct dimension* dim = (struct dimension*) dimension;

    width = dim->width;
    height = dim->height;

    printf("\n\nWidth: %d, Height: %d\n\n", width, height);

}

// In main.cpp

// Pass a struct in pthread_create
struct dimension dim;
dim.width = w;
dim.height = h;

pthread_create(&ph, &attr, (void * (*)(void *)) setUpSocket, (void *) &dim);

Before calling pthread_create, dim.width and dim.height are correct. In my socket file, only width is set, height is 0, and I do not understand why.

Does anyone know what is wrong please and how to fix it?

Thank you very much.

like image 796
Jary316 Avatar asked Jun 29 '11 16:06

Jary316


People also ask

How do I pass multiple arguments to a thread function?

For cases where multiple arguments must be passed, this limitation is easily overcome by creating a structure which contains all of the arguments, and then passing a pointer to that structure in the pthread_create() routine. All arguments must be passed by reference and cast to (void *).

How many parameters are there in pthread_create )?

How many parameters are there in pthread_create )? pthread_create() gets 4 arguments The first argument is a pointer to thread_id , used by pthread_create() to supply the program with the thread's identifier.

Which argument of pthread_create is thread entry code?

pthread_t is the data type used to uniquely identify a thread. It is returned by pthread_create() and used by the application in function calls that require a thread identifier. The thread is created running start_routine, with arg as the only argument.

What is the last argument passed to the pthread_create () function?

The last parameter of pthread_create() is passed as the argument to the function, whereas the return value is passed using pthread_exit() and pthread_join() .


2 Answers

The way you're passing the arguments should work fine, as long as dim is not allocated on the stack. If it's on the stack, then it could become deallocated before the new thread has a chance to run, resulting in undefined behavior. If you're only creating one thread, you can use a global variable, but the better alternative is to allocate it on the heap.

Also, you should not be casting the function pointer: that is undefined behavior (and in fact, it could crash due to speculative execution on the IA64 architecture). You should declare your thread procedure to return void* and avoid a function pointer cast:

void *setUpSocket(void* dimension) {

    struct dimension* dim = (struct dimension*) dimension;

    width = dim->width;
    height = dim->height;
    // Don't leak the memory
    free(dim);

    printf("\n\nWidth: %d, Height: %d\n\n", width, height);

    return 0;
}

// In main.cpp

// Pass a struct in pthread_create (NOT on the stack)
struct dimension *dim = malloc(sizeof(struct dimension));
dim->width = w;
dim->height = h;

pthread_create(&ph, &attr, setUpSocket, dim);
like image 129
Adam Rosenfield Avatar answered Sep 28 '22 09:09

Adam Rosenfield


How big can width and height be? If not very big, I would do something like this:

 pthread_create(&ph, &attr, setUpSocket, (void *)(65536*height+width));
like image 24
R.. GitHub STOP HELPING ICE Avatar answered Sep 28 '22 09:09

R.. GitHub STOP HELPING ICE