I'm working on a program that reads the CAN datastream on a vehicle. The program will be used on an STW TC3g with an embedded linux.
I need to compile my code with a cross compiler based on the powerpc. For multithreading I wrote a little example program to test some functions.
Here is the code:
pthread_t tid[2];
void* thread_pollCan();
int main() {
while(1) {
pthread_create(&tid[0], NULL, &thread_pollCan, NULL);
pthread_create(&tid[1], NULL, &thread_pollCan, NULL);
printf("main:\tpid0:%ld\n", tid[0]);
printf("main:\tpid1:%ld\n", tid[1]);
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
puts("main:\tThreads terminated");
sleep(3);
puts("main:\tLoop end");
}
return 0;
};
void* thread_pollCan(void* arg) {
int i;
// Should thread tid[0] do...
if(pthread_equal(pthread_self(),tid[0])) {
printf("tid[0]: %ld\t thread_self: %ld\n",tid[0],pthread_self());
for(i=0;i<=10;i++) {
printf("In loop %d\n",i);
}
}
// Should thread tid[1] do...
if(pthread_equal(pthread_self(),tid[1])) {
printf("tid[1]: %ld\t thread_self: %ld\n",tid[1],pthread_self());
for(i=0;i<=10;i++) {
printf("In loop %d\n",i);
}
}
pthread_exit((void *)pthread_self());
}
This program runs fine on my developing ubuntu pc if I compile it with gcc:
gcc -Wall -pthread Multithreading.c -o Multithreading
Program output:
main: pid0:140207930042112
main: pid1:140207921649408
tid[0]: 140207930042112 thread_self: 140207930042112
In loop 0
In loop 1
In loop 2
In loop 3
In loop 4
In loop 5
In loop 6
In loop 7
In loop 8
In loop 9
In loop 10
tid[1]: 140207921649408 thread_self: 140207921649408
In loop 0
In loop 1
In loop 2
In loop 3
In loop 4
In loop 5
In loop 6
In loop 7
In loop 8
In loop 9
In loop 10
main: Threads terminated
main: Loop end
But if I compile it for my vehicle computer with the following:
powerpc-stw-linux-uclibc-gcc -O2 -Wall -pthread Multithreading.c -o Multithreading -lm
Its program output is this:
main: pid0:1026
main: pid1:2051
main: Threads terminated
main: Loop end
The problem is, that the pthread_equal()
function never match with the thread id wrote in the tid[]
-array.
So my question is, if there are any other methods to find the correct thread id.
Seems you're trying to read tid[]
that does not have the time to get updated. You should add some mutex
function calls to synchronize all:
// mutex for synchronization
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// pthread ids
pthread_t tid[2];
// thread function
void* thread_pollCan();
int main() {
while(1) {
// lock the mutex: thread will wait for it to be unlocked
pthread_mutex_lock(&mutex);
pthread_create(&tid[0], NULL, &thread_pollCan, NULL);
pthread_create(&tid[1], NULL, &thread_pollCan, NULL);
printf("main:\tpid0:%ld\n", tid[0]);
printf("main:\tpid1:%ld\n", tid[1]);
// unlock the mutex: threads can run
pthread_mutex_unlock(&mutex);
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
puts("main:\tThreads terminated");
sleep(3);
puts("main:\tLoop end");
}
return 0;
};
void* thread_pollCan(void* arg) {
// wait for mutex to be unlocked before working
pthread_mutex_lock(&mutex);
// immediatly release the mutex
pthread_mutex_unlock(&mutex);
// do stuff
if (thread_equal(tid[0], pthread_self())
{ /*...*/ }
}