ashley
ashley

Reputation: 1175

thread parameter passing c

My question regards parameter passing to a thread.

I have a function Foo that operates on an array, say arrayA. To speed things up, Foo is coded to operate at both directions on the array. So, Foo takes arrayA and an integer X as parameters. Depending on the value of X, it operates in forward or reverse direction.

I'm looking to avoid making global use of "arrayA" and "X". So, I'm after passing "arrayA" and "X" as parameters to Foo, and creating two threads to run Foo-- one in each direction. Here's what I did:

typedef struct {int* arrayA[MSIZE]; int X; } TP; //arrayPack=TP 

void Foo (void *tP) {

    TP *tp = (TP*)tP;  // cast the parameter tP back to what it is and assign to pointer *tp

    int x;
    printf("\nX: %d", tp->X);
    printf("\n  arrayA: "); for (x=0; x<tp->arrayA.size(); printf("%d ", aP->arrayA[x]), x++);  
} // end Foo

void callingRouting ()  {   
    int* arrayA[MSIZE] = {3,5,7,9}; 
    TP tp; tp.arrayA=arrayA; 
    tp.X=0;   _beginthread(Foo, 0, (void*)&tp); // process -- forward 
    tp.X=1;   _beginthread(Foo, 0, (void*)&tp); // process -- reverse   
} 

The values aren`t passed-- my array is printing empty and I'm not getting the value of X printed right. What am i missing ?

I would also appreciate suggestions on some readings on this-- passing parameters to threads-- especially on passing the resources shared by the threads. Thanks.

Upvotes: 0

Views: 227

Answers (2)

Richard Chambers
Richard Chambers

Reputation: 17693

The first thing to remember is that you have a thread that is starting up two other threads. Since you do not have any control over the processor time slice and how that is allocated, you can not be sure when the two other threads will start and may be not even the order that they will start in.

Since you hare using an array on the stack that is local to the function callingRouting () as soon as that function returns the local variables allocated will basically be out of scope and can no longer be depended on.

So there are a couple of ways to do this.

The first is to use global or static memory variables for these data items being passed to the threads.

The other is to start both threads and then wait for both to complete before continuing.

Since you do not know when or the order of the threads being started, you really should use two different TP type variables, one for each thread. Otherwise you run the risk of the time slice allocation to be such that both threads will have the same TP data.

Upvotes: 2

James
James

Reputation: 9288

You're passing the address of a stack variable to your thread function, once callingRouting exits the TP structure no longer exists. They need to be either globals or allocated on the heap.

However you'll need two copies of the TP for each thread as the change tp.X=1 may be visible to both threads.

There are problems there but how you see them depends on how the OS decides to schedule the threads on each execution.

Upvotes: 2

Related Questions