I have to use the clone() system call in the main-function to get 2 threads. (I know, there are other options, but in this case, it has to be clone()).
The system call works and both threads arrive in the designated function (foo). But in this function I need them to call another function with this signature:
void increment(int* a, int b)
(Sidenote: It adds b * 1 to a. (= a+b))
The important thing is, that both, a and b, are declared in the main-function and I don’t know how to pass them to foo.
I already tried different things, but without success. I’ve gotten a hint: Use an adapter.
But I have no clue how to do this. (I also dont know how to use the args in clone with int.)
Any suggestions?
One of the arguments to
clone()is avoid* arg. This lets you pass a void pointer to your function. In order to pass an int pointer and an int instead, you have to create a struct with an int pointer and int assigned toaandbrespectively, then cast a pointer to that struct into a void pointer. Then inside the function you reverse the process.My C is a little rusty and I haven’t compiled this, so don’t quote me on it, but it should look roughly like this:
Note: take care that the struct you create is still in scope when
fnis called, as it isn’t copied. You might have tomallocit.