Czy jest możliwe rzutowanie wskaźnika na unsigned int, a następnie rzutowanie go z powrotem na wskaźnik? Próbuję przechowywać wskaźnik do struct w zmiennej pthread_t, ale nie mogę go uruchomić. Oto kilka fragmentów mojego kodu (tworzę bibliotekę zarządzania wątkami na poziomie użytkownika). Kiedy próbuję wydrukować numer wątku, daje mi to trochę długiego numeru śmieci.Przechowywanie adresu wskaźnika w unsigned int w C
Edycja: Nieważne, mam to do roboty.
Zmieniłem
thread = (pthread_t) currentThread;
do
*thread = (pthread_t) currentThread;
Pomyślałem, że to coś tak głupiego.
Program testowy:
pthread_t thread1;
pthread_t thread2;
pthread_create(&thread1, NULL, runner, NULL);
pthread_create(&thread2, NULL, runner, NULL);
pthread_join(&thread2, NULL);
Moja biblioteka:
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);
}
Nie rozumiem - zdefiniowaniu pthread_create w swoim własnym biblioteki? Dlaczego nazwa jest sprzeczna ze zwykłą funkcją o tej nazwie? –
To jest projekt dla szkoły. –
Nieważne, działa. Edytowałem mój oryginalny wpis. –