Reputation: 93
#include pthread.h
#include stdio.h
static int seq[50];
void *runner(void *); /* the thread */
int main(int argc, char *argv[])
{
int y;
pthread_t tid[3]; /* the thread identifier */
pthread_attr_t attr; /* set of attributes for the thread */
if(argc!=2)
{
fprintf(stderr,"you need to enter two arguments");
exit(-1);
}
else
{
int a = atoi(argv[1]);
if(a>0)
{
y=a;
}
else
{
fprintf(stderr,"you need to enter a valid number greater than zero");
exit(-1);
}
}
/* get the default attributes */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
/* create three threads */
pthread_create(&tid, NULL, runner,(void *)y);
/* now wait for the thread to exit */
sleep(2);
printf( " Am in main process, The sequence is ");
int j=0,val=0;
for( j = 0; j < 40; j++)
{
val=seq[j];
if(val>=1)
printf( " %d ", seq[j]);
}
pthread_join(tid, NULL);
pthread_exit(0);
}
/**
* The thread will begin control in this function
*/
void *runner(void *param)
{
int count=0;
int y= (int *) param;
// printf(" Am in runner");
while(y!=1)
{
if(y%2==0)
y = y/2;
else
y= ((3*y)+1);
// printf(" %d ", y);
seq[count] = y;
count++;
}
pthread_exit(0);
}
I am getting the following errors
bonus.c: In function ‘main’:
bonus.c:91: warning: cast to pointer from integer of different size
bonus.c:91: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type
bonus.c:123: warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast
bonus.c: In function ‘runner’:
bonus.c:157: warning: comparison between pointer and integer
bonus.c:159: error: invalid operands to binary %
bonus.c:161: error: invalid operands to binary /
bonus.c:165: error: invalid operands to binary *
bonus.c:171: warning: assignment makes integer from pointer without a cast
Upvotes: 2
Views: 7167
Reputation: 66441
For whatever reason, you're declaring an array of pthread_t
:
pthread_t tid[3];
Don't do that, unless you plan on starting three threads.
You want
pthread_t tid;
Two errors stem from this:
pthread_create(&tid, NULL, runner,(void *)y);
(passing argument 1 of ‘pthread_create’ from incompatible pointer type)
where you pass a pointer to the array instead of a pointer to a pthread_t
, and
pthread_join(tid, NULL);
(passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast)
where you pass a pointer to a pthread_t
instead of a pthread_t
.
The warning
cast to pointer from integer of different size
comes from (void*) y
— an int
is smaller than a void*
if you're compiling for a 64-bit target.
You should use &y
or give y
a type that is as large as a void*
, such as int64_t
.
In runner
you're casting the parameter to int*
, which is very odd since it started out as an int
.
You need to make sure that you cast back to the same type you started with.
You then proceed to initialise an int
with this value, but that can't be your actual code because the following errors are consistent with
int* y = (int *) param;
so I suspect you lost a "*" when entering the code in the question.
If you pass a pointer to int
in the parameter, that should be
int y = *(int*) param;
if you pass an integer reinterpreted as a pointer, it should be
int64_t y = (int64_t) param;
if you settle on int64_t
as your integral type.
Also, keep in mind that printing the elements of an array while a thread is modifying them is possibly not the best of ideas, and neither is not ensuring that the thread doesn't overstep the array boundaries.
Upvotes: 3
Reputation: 2865
pthread_create(&tid, NULL, runner,(void *)y);
==>
for( i = 0; i < 3; i++){
pthread_create(&tid[i], NULL, runner,(void *)y);
}
and
pthread_join(tid, NULL);
==>
for( i = 0; i < 3; i++){
pthread_join(tid[i], NULL);
}
Upvotes: -1