c - Linux thread id comparison -


so, i'm having bad time finding current thread id in list of structures, reflect information threads.

basically, each element list has field pthread_t id. search function like:

int thread_pos(pthread_t tid) {     int i;      (i = 0; < my_threads.thread_num; i++)     {            printf("for: %d %d %d\n", my_threads.vector[i].id, tid, pthread_equal(my_threads.vector[i].id, tid));         if (pthread_equal(my_threads.vector[i].id, tid))         {                printf("found\n");             return i;         }        }         return -1;  } 

as there 1 element in my_threads.vector, prints 1 line:

419817216 419817216 0 

data structures:

struct my_thread_t {     pthread_t id; };  struct my_threads_t {     struct my_thread_t vector[100];     int thread_num; }; 

i add elements list way:

pthread_create(&new_tid, null, start_thread, &my_threads.vector[my_threads.thread_num].thread_arg);  my_threads.vector[my_threads.thread_num].id = new_tid; my_threads.thread_num++; 

the problem is, though element there, in list, fails find it. printed line on each comparison (for example 419817216 419817216 0, showing tid in list, key tid, , result of pthread_equal). can see, numerical representation same, somehow pthread_equal says aren't.

what i'm missing?

if add / remove / check elements my_threads different threads wild. fragment of code suspect don't have mutex protection structure.

if did not implement locking , need read list more write, consider pthread_rwlock() interface.

update: please check sizeof(pthread_t) on platform? if 8 (unsigned long), should @ least use %lu format in printf.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -