I am trying to create a thread library and my thread is a `struct` type. Have to follow a certain interface and in that I need to pass the `thread` by value. For ex: to join on a thread my code is as follows:

```
int thread_join(thread_t thread, void **status1)
{
    printf("Joining thread\n");
    long int thId = thread.id;
    printf("Thread id: %ld\n", thId);
    gtthread_t * thrd = getThreadFromID(thId);
    while(thrd->status != EXIT)
    {

    }   
    status1 = &(thrd->ret_value);
    return 0;
}

```

And I an passing a `struct` of type `thread_t` to this function. My problem is when I see the thread's ID in the calling function, its displayed properly but when I check it in the `thread_join` function its displayed as 0. The caller function is as follows:

```
void* caller(void* arg)
{
    thread_t th;

    thread_create(&th, some_function, NULL);
    thread_join(th, NULL);

    while(1);
}

```

Thread create initializes the ID of the thread to a non-zero value and starts the function associated with it.

My thread structure (and other relevant structure is):

```
typedef enum
{
    RUNNING,
    WAITING,
    CANCEL,
    EXIT
} stat;

//Thread
typedef struct 
{
    ucontext_t t_ctxt;
    long int id;
    stat status;
    void  * ret_value;
    int isMain;
} thread_t;

int thread_create(thread_t *thread, void *(*start_routine)(void *), void *arg)
{
    thread = (thread_t *)malloc(sizeof(thread_t));
    thread->id = ++count;
    thread->status = RUNNING;
    thread->ret_value = NULL;
    thread->isMain = 0;
    if(getcontext(&(thread->t_ctxt)) == -1)
        handle_error("getcontext");
    thread->t_ctxt.uc_stack.ss_sp = malloc(SIGSTKSZ);
    thread->t_ctxt.uc_stack.ss_size = SIGSTKSZ;
    thread->t_ctxt.uc_link = &sched_ctxt;
    makecontext(&thread->t_ctxt, (void (*)(void))wrap_func, 2, (void (*)(void))start_routine, arg); 
    enqueue(gQ, thread);
    printf("Thread id: %ld\n", thread->id);
    swapcontext(&(curr_thread->t_ctxt),&sched_ctxt);
    return 0;
}

```

Why does this happen? After all, I am passing by value and this should create a copy of the `thread` with the same values. Thanks.

EDIT:

Basically I am having a queue of threads and there is a scheduler which round-robins. I can post that code here too but I'm sure that's needless and that code works fine.

EDIT2:

I am making a header file from this code and including that header in another file to test it. All my `thread_t` variables are static. The caller is a function which includes my header file.