Unix Programming - thread question

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2006 > thread question





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author thread question
steve@stevedurkin.net

2006-02-19, 5:50 pm

Hi, I'm learning about threads, and I'm supposed to find out what the
possible outputs are for the following program. However, something
seems to be amiss with the call to pthread_create(). 'printVals()'
doesn't seem to be executing ('tid' is unchanged in main() after the
call to pthread_create()). Can anyone tell me why/if pthread_create()
is failing?

#include <pthread.h>
#include <stdio.h>

int value;
void * printVals(void * param) {
int number = 1;
int i = (int)param;
value = value + 2;
number = number + 2;
fprintf(stderr, " %d %d %d \n", i, value, number);
pthread_exit(NULL);
}

int main() {
int tid, number = 1;
value = 2;
pthread_create(&tid, NULL, printVals, (void *)1);
value = value + 2;
number = number + 2;
fprintf(stderr, " %d %d %d \n", 0, value, number);
pthread_join(tid, NULL);
return 0;
}

Ian Collins

2006-02-19, 5:50 pm

steve@stevedurkin.net wrote:
> Hi, I'm learning about threads, and I'm supposed to find out what the
> possible outputs are for the following program. However, something
> seems to be amiss with the call to pthread_create(). 'printVals()'
> doesn't seem to be executing ('tid' is unchanged in main() after the
> call to pthread_create()). Can anyone tell me why/if pthread_create()
> is failing?
>
> int main() {
> int tid, number = 1;
> value = 2;
> pthread_create(&tid, NULL, printVals, (void *)1);


tid should be a pthread_t*, not an int*. Didn't you get a compiler
error or warning?


--
Ian Collins.
Paul Pluzhnikov

2006-02-19, 5:50 pm

steve@stevedurkin.net writes:

> Hi, I'm learning about threads, and I'm supposed to find out what the
> possible outputs are for the following program. However, something
> seems to be amiss with the call to pthread_create().


If that's the case, it would return an error value (which you are
supposed to check!).

> 'printVals()' doesn't seem to be executing


This most often happens when you build your program incorrectly.

Instructions on how to build correctly for threads are OS and
compiler specific, and you didn't provide any clues to either.

On Linux, use 'gcc -pthread'; on Solaris, use 'gcc -pthreads' or
'cc -mt'; on AIX use 'cc_r'.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com