synchronizing 3 threads


I

iwasinnihon

I am trying to teach myself more about synchronizing threads. I took
the following code from a book and have altered it to read one more
file (the program counts in the words in multiple files). The
original program from the book counted the words in 2 files. I want
to increase that to 3. However, when I add the 3rd thread it just
gets to a certain point and hangs. I think that the answer is to add
another condition variable, however, I can't quite grasp how I would
use this other variable. Can someone point me in the right direction?

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


struct arg_set {
char *fname;
int count;
};

struct arg_set *mailbox;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t flag = PTHREAD_COND_INITIALIZER;

int main( int ac, char *av[] )
{
pthread_t t1, t2, t3;
struct arg_set args1, args2, args3;
void *count_words(void *);
int reports_in = 0;
int total_words = 0;

if(ac != 4) {
printf("usage: %s file1 file2 file3\n", av[0]);
exit(1);
}

pthread_mutex_lock(&lock);

args1.fname = av[1];
args1.count = 0;
pthread_create(&t1, NULL, count_words, (void *) &args1);

args2.fname = av[2];
args2.count = 0;
pthread_create(&t2, NULL, count_words, (void *) &args2);

args3.fname = av[3];
args3.count = 0;
pthread_create(&t3, NULL, count_words, (void *) &args3);

while(reports_in < 3) {
printf("MAIN: waiting for flag to go up\n");
pthread_cond_wait(&flag, &lock);
printf("Main: Wow! flag was raised, I have the lock\n");
printf("%7d: %s\n", mailbox->count, mailbox->fname);
total_words += mailbox->count;
if(mailbox == &args1)
pthread_join(t1, NULL);
if(mailbox == &args2)
pthread_join(t2, NULL);
if(mailbox == &args3)
pthread_join(t3, NULL);
mailbox = NULL;
pthread_cond_signal(&flag);
reports_in++;
}
printf("%7d: total words\n", total_words);
}

void * count_words(void *a)
{
struct arg_set *args = a;
FILE *fp;
int c, prevc = '\0';

if((fp = fopen(args->fname, "r")) != NULL) {
while((c = getc(fp)) != EOF) {
if(!isalnum(c) && isalnum(prevc))
args->count++;
prevc = c;
}
fclose(fp);
}
else
perror(args->fname);
printf("COUNT: waiting to get lock\n");
pthread_mutex_lock(&lock);
printf("COUNT: have lock, storing data\n");
if(mailbox != NULL)
pthread_cond_wait(&flag, &lock);
mailbox = args;
printf("COUNT: raising flag\n");
pthread_cond_signal(&flag);
printf("COUNT: unlocking box\n");
pthread_mutex_unlock(&lock);

return NULL;
}
 
Ad

Advertisements

J

Jack Klein

I am trying to teach myself more about synchronizing threads.

[snip]

You're asking in the wrong place. Threads are not defined by, or part
of, the C language. They are extensions provided by your particular
operating system.

Ask in or an OS specific group, perhaps
or Or a
Windows group like
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

jacob navia

Inspecting your code I do not find anything obviously wrong.

It would be helpful to know what happens really, i.e. what
is the sequence of printf outputs when you "fail". You say:
"It hangs" but it is not helpful for anyone trying to
figure out what happens.

jacob
 
Ad

Advertisements

D

Default User

Army1987 wrote:

Empty posts serve no purpose, and signatures should begin with
<newline>--<newline>. And check wheter there are some missing
apostrophes in yours. :)

Besides what Keith said, your information about .sig separators is
wrong. The standard is (using your descriptive method) is:
<newline>-- <newline>


That space is a vital component.




Brian
 
Ad

Advertisements

C

CBFalconer

iwasinnihon said:
I am trying to teach myself more about synchronizing threads.
....

Threads are off topic here. Look for a newsgroup with 'thread' in
its name.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top