* pthread & its bug
@ 2007-03-23 21:06 Mohsen Pahlevanzadeh
2007-03-23 21:21 ` John (Eljay) Love-Jensen
0 siblings, 1 reply; 3+ messages in thread
From: Mohsen Pahlevanzadeh @ 2007-03-23 21:06 UTC (permalink / raw)
To: gcc-help
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Dear all,
I'm newbie in multithread programming.I'm practicing that.Now i have
written following code :
My code :
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int g1=0;
int g2=0;
void task1(int *counter);
void task2(int *counter);
int main(int argc,char *argv[])
{
pthread_t *thr1,*thr2;
int ret;
if ((ret = pthread_create(&thr1,NULL,(void *)task1,(void *)&g1)))
{
perror("pthread_create : task1");
exit(EXIT_FAILURE);
}
if ((ret = pthread_create(&thr2,NULL,(void *)task2,(void *)&g2)))
{
perror("pthread_create : task2");
exit(EXIT_FAILURE);
}
pthread_join(thr2,NULL);
pthread_join(thr1,NULL);
cleanup(g1,g2);
exit(EXIT_SUCCESS);
}//end of main program
void task1(int *counter)
{
while(*counter < 5 ){
printf("task1 count: %d\n",*counter);
(*counter)++;
}//end of while
}//end of task1 function
void task2(int *counter)
{
while(*counter < 5 ){
printf("task2 count: %d\n",*counter);
(*counter)++;
}//end of while
}//end of task1 function
void cleanup(int counter1,int counter2)
{
printf("Total iterations: %d\n",counter1+counter2);
}//end of cleanup function
///////////////////////////END OF
CODE////////////////////////////////////////////////////////
Now when i compile it, I receive following errors:
mohsen@debian:~/test/learning/pthread$ make
gcc -c test.c
In file included from test.c:3:
/usr/include/pthread.h:285: error: conflicting types for âpthread_tâ
/usr/include/bits/pthreadtypes.h:152: error: previous declaration of
âpthread_tâ was here
/usr/include/pthread.h:286: error: conflicting types for âpthread_attr_tâ
/usr/include/bits/pthreadtypes.h:54: error: previous declaration of
âpthread_attr_tâ was here
/usr/include/pthread.h:287: error: conflicting types for âpthread_key_tâ
/usr/include/bits/pthreadtypes.h:82: error: previous declaration of
âpthread_key_tâ was here
/usr/include/pthread.h:289: error: conflicting types for
âpthread_mutexattr_tâ
/usr/include/bits/pthreadtypes.h:102: error: previous declaration of
âpthread_mutexattr_tâ was here
/usr/include/pthread.h:290: error: conflicting types for âpthread_mutex_tâ
/usr/include/bits/pthreadtypes.h:95: error: previous declaration of
âpthread_mutex_tâ was here
/usr/include/pthread.h:291: error: conflicting types for
âpthread_condattr_tâ
/usr/include/bits/pthreadtypes.h:79: error: previous declaration of
âpthread_condattr_tâ was here
/usr/include/pthread.h:292: error: conflicting types for âpthread_cond_tâ
/usr/include/bits/pthreadtypes.h:72: error: previous declaration of
âpthread_cond_tâ was here
test.c: In function âmainâ:
test.c:13: warning: passing argument 1 of âpthread_createâ from
incompatible pointer type
test.c:19: warning: passing argument 1 of âpthread_createâ from
incompatible pointer type
test.c:26: warning: passing argument 1 of âpthread_joinâ from
incompatible pointer type
test.c:27: warning: passing argument 1 of âpthread_joinâ from
incompatible pointer type
test.c: At top level:
test.c:48: warning: conflicting types for âcleanupâ
test.c:29: warning: previous implicit declaration of âcleanupâ was here
make: *** [main.o] Error 1
mohsen@debian:~/test/learning/pthread$
//////////////////////////////////////
I have retrieved libdir & includedir with pthread_config command.
Please help me .....
Yours,Mohsen
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFGBG2a/ZBAvBh9bHIRArwvAJ429vihIW7mqsNq4U76vrzCHi7zcwCbBpC3
O07y29P5zpUjfirZOV1vbBk=
=5N0e
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: pthread & its bug
2007-03-23 21:06 pthread & its bug Mohsen Pahlevanzadeh
@ 2007-03-23 21:21 ` John (Eljay) Love-Jensen
2007-03-23 22:57 ` bjorn rohde jensen
0 siblings, 1 reply; 3+ messages in thread
From: John (Eljay) Love-Jensen @ 2007-03-23 21:21 UTC (permalink / raw)
To: Mohsen Pahlevanzadeh, gcc-help
Hi Mohsen,
Your inquiry is off topic for this forum. I do not say this to chastize you. I say it because you may get better / faster / more accurate information from a more appropriate forum that is pthread savvy.
There are a whole bunch of errors in your test source code.
For example, the pthread_create's third parameter takes a function pointer to a function that looks like:
void* task(void* ptr);
You are passing in a function pointer to a function that looks like:
void task(int* counter);
And you are casting the function pointer to a data pointer:
(void*)task1
That doesn't fit.
Another example, pthread_create's first parameter takes a pointer to a pthread_t. You are passing in a poiter to a pointer to a pthread_t. (And that pointer-to-a-pointer has not been allocated anywhere.)
That doesn't fit.
Work through all the mismatched data types, and then see where things end up. GCC helps you, by emitting a lot of warnings and errors. Heed them.
Don't forget to:
gcc test.c -lpthread
HTH,
--Eljay
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: pthread & its bug
2007-03-23 21:21 ` John (Eljay) Love-Jensen
@ 2007-03-23 22:57 ` bjorn rohde jensen
0 siblings, 0 replies; 3+ messages in thread
From: bjorn rohde jensen @ 2007-03-23 22:57 UTC (permalink / raw)
Cc: Mohsen Pahlevanzadeh, gcc-help
John (Eljay) Love-Jensen wrote:
> Hi Mohsen,
>
> Your inquiry is off topic for this forum. I do not say this to chastize you. I say it because you may get better / faster / more accurate information from a more appropriate forum that is pthread savvy.
>
> There are a whole bunch of errors in your test source code.
>
> For example, the pthread_create's third parameter takes a function pointer to a function that looks like:
>
> void* task(void* ptr);
>
> You are passing in a function pointer to a function that looks like:
>
> void task(int* counter);
>
> And you are casting the function pointer to a data pointer:
>
> (void*)task1
>
> That doesn't fit.
>
> Another example, pthread_create's first parameter takes a pointer to a pthread_t. You are passing in a poiter to a pointer to a pthread_t. (And that pointer-to-a-pointer has not been allocated anywhere.)
>
> That doesn't fit.
>
> Work through all the mismatched data types, and then see where things end up. GCC helps you, by emitting a lot of warnings and errors. Heed them.
>
> Don't forget to:
>
> gcc test.c -lpthread
>
> HTH,
> --Eljay
You might want to define your task functions to have the type expected
by phtread_create instead of casting them, something like;
void *task1(void *p)
{
int *counter=(int *)p;
while(*counter < 5 ){
printf("task1 count: %d\n",*counter);
(*counter)++;
}
return NULL;
}
The type system is there to help you, dont cast things unless you really
need to.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2007-03-23 21:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-23 21:06 pthread & its bug Mohsen Pahlevanzadeh
2007-03-23 21:21 ` John (Eljay) Love-Jensen
2007-03-23 22:57 ` bjorn rohde jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).