5

Is it possible to cast a pointer to an unsigned int, then later cast it back to a pointer? I'm trying to store the pointer to a struct in a pthread_t variable, but I can't seem to get it to work. Here's some snippets of my code (I'm creating a user-level thread management library). When I try to print out the tid of the thread it gives me some long garbage number.

Edit: Never mind, I got it to work.

I changed

thread = (pthread_t) currentThread;

to

*thread = (pthread_t) currentThread;

Figured it was something stupid like that.


Test program:

pthread_t thread1;
pthread_t thread2;

pthread_create(&thread1, NULL, runner, NULL);
pthread_create(&thread2, NULL, runner, NULL);
pthread_join(&thread2, NULL);

My library:

typedef struct queueItem
{
    int tid;
    ucontext_t context;

    int caller;

    struct queueItem *joiningOn;
    struct queueItem *nextContext;
} queueItem;

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
{
    thread = (pthread_t) currentThread;
}

...

int pthread_join(pthread_t thread, void **retval)
{
    queueItem *t = (queueItem *) thread;

    if(runningContext->joiningOn != NULL) // Current thread is already waiting on another
        return EINVAL;
    if(t == NULL) // If thread to join on is invalid
        return 0;

    fprintf(stdout, "JOINEE: %d\n", t->tid); // Prints weird number

    runningContext->caller = JOIN;
    runningContext->joiningOn = t;
    swapcontext(&(runningContext->context), &scheduleContext);
}
Anthony C.
  • 139
  • 1
  • 3
  • 7

2 Answers2

5

No. On many systems pointer type is bigger than int type. If you have a problem to use pthread_t, ask about it, int is not the answer.

For example, on my machine, the following code:

#include <stdio.h>

int main() {
        printf("unsigned int = %lu\n", sizeof(unsigned int));
        printf("pointer = %lu\n", sizeof(void*));
        return 0;
}

outputs:

unsigned int = 4
pointer = 8
MByD
  • 135,866
  • 28
  • 264
  • 277
4

Sure it's possible, if you make sure your unsigned int is the same size as a void* on your system.

If you have some code that's not working, post it.

Edit: You should read about intptr_t, e.g. here: Why / when to use `intptr_t` for type-casting in C?

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Same or greater. + you forgot about alignment. –  Apr 29 '12 at 00:43
  • Sure, if unsigned int is larger I guess that's OK. As for alignment, do you have a specific circumstance in mind that might be problematic? – John Zwinck Apr 29 '12 at 00:44
  • 1
    Specific - no. But in general, different POD types may require to have different alignment requirement by a CPU architecture. This is not covered by C standard, so if tomorrow I write my CPU in Verilog that has unsigned int of size 64 bit and requires it to be aligned by 16, and then make a pointer of 8 bits requiring it to be aligned on 8, that could result in foobar :) The morale - if you don't know, use `memcpy`. + pthread_t is opaque type... –  Apr 29 '12 at 00:49
  • Sure, copying the bytes from the int to the pointer makes sense if you care about portability. I figure the OP hasn't gotten to that level of refinement yet. :) +1 anyway. – John Zwinck Apr 29 '12 at 00:52
  • I've posted some of my code. The length of the pthread_t type (unsigned int) and queueItem pointer are both 4 on my machine. – Anthony C. Apr 29 '12 at 01:01