Unix Programming - New to threads

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2004 > New to threads





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 New to threads
El Durango

2004-03-15, 2:34 am

Hi I am new to learning about threads.
I have been working on example code and had a question regarding the
following piece of code based on threads:
/ ****************************************
**********************/
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>

#define NLOOP 5000

int counter;

void *doit(void *);

int main(int argc, char **argv){
pthread_t tidA, tidB;
char *name = "Steve";

if(argc == 2)
name = argv[1];
pthread_create(&tidA, NULL, &doit, NULL);
pthread_create(&tidB, NULL, &doit, NULL);
/* wait for both threads to terminate */
printf("name: %s\tcounter:%d\n",name,counter);
pthread_join(tidA, NULL);
pthread_join(tidB, NULL);

exit(0);
}

void* doit(void *vptr){
int i, val;
for(i=0; i < NLOOP; i++){
val = counter;
printf("%d: %d\n",pthread_self(), val+1);
counter = val+1;
}
return (NULL);
}
/ ****************************************
******************/

in this code I do not understand how the function pthread_join works, why I
say this is because if I comment out the pthread_join system calls then the
doit function that the pthread_create calls does not run. I even commented
the 2nd pthread_create sys. call just to see if it changed the issue, but it
did not.
So basically in this example the pthread_create is relian on the
pthread_join sys. call to effectively call and run the function. I am
wondering why this is? and if so when is it not reliant on the pthread_join,
again I even tried with only one thread and it still did not work. From my
reading I believe pthread_join is used when there are multiple threads that
are created, no?
If anyone can shed light my ignorance I would appreciate it.
I know my question is probably silly to most of you but it's better that I
ask then remain ignorant.
thank you again.




GVK

2004-03-15, 2:34 am

El Durango wrote:
> Hi I am new to learning about threads.

So am I . Cheers

> in this code I do not understand how the function pthread_join works, why I
> say this is because if I comment out the pthread_join system calls then the
> doit function that the pthread_create calls does not run.


Because there is no way to call doit(), except when the threads are created. Try
adding &doit() to your main() and it should work(does it?)

> From my
> reading I believe pthread_join is used when there are multiple threads that
> are created, no?


pthread_join() waits for a thread to terminate. man pthread_join(3) should help.

> I know my question is probably silly to most of you but it's better that I
> ask then remain ignorant.


No question is silly enough not to post here

GVK

--
Happy Hacking!!!
Marc Rochkind

2004-03-15, 9:37 am

"El Durango" <El_Durango@yah00.c0m> wrote in message news:<Tcc5c.7377$Zk4.2938@newssvr22.news.prodigy.com>...
> Hi I am new to learning about threads.
> I have been working on example code and had a question regarding the
> following piece of code based on threads:
> / ****************************************
**********************/
> #include <pthread.h>
> #include <stdlib.h>
> #include <stdio.h>
>
> #define NLOOP 5000
>
> int counter;
>
> void *doit(void *);
>
> int main(int argc, char **argv){
> pthread_t tidA, tidB;
> char *name = "Steve";
>
> if(argc == 2)
> name = argv[1];
> pthread_create(&tidA, NULL, &doit, NULL);
> pthread_create(&tidB, NULL, &doit, NULL);
> /* wait for both threads to terminate */
> printf("name: %s\tcounter:%d\n",name,counter);
> pthread_join(tidA, NULL);
> pthread_join(tidB, NULL);
>
> exit(0);
> }
>
> void* doit(void *vptr){
> int i, val;
> for(i=0; i < NLOOP; i++){
> val = counter;
> printf("%d: %d\n",pthread_self(), val+1);
> counter = val+1;
> }
> return (NULL);
> }
> / ****************************************
******************/
>
> in this code I do not understand how the function pthread_join works, why I
> say this is because if I comment out the pthread_join system calls then the
> doit function that the pthread_create calls does not run. I even commented
> the 2nd pthread_create sys. call just to see if it changed the issue, but it
> did not.
> So basically in this example the pthread_create is relian on the
> pthread_join sys. call to effectively call and run the function. I am
> wondering why this is? and if so when is it not reliant on the pthread_join,
> again I even tried with only one thread and it still did not work. From my
> reading I believe pthread_join is used when there are multiple threads that
> are created, no?
> If anyone can shed light my ignorance I would appreciate it.
> I know my question is probably silly to most of you but it's better that I
> ask then remain ignorant.
> thank you again.


I think you are erroneously using the presence of output as evidence
that the threads are running. What's happening is that they are
running, but without the pthread_join calls the call to exit occurs
before they output anything. You can test this theory by commenting
out the pthread_join calls and putting in a call to sleep.

--
Marc Rochkind
"Advanced UNIX Programming" (publishing in April 2004)
www.basepath.com/aup
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com