473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

synchronizing 3 threads

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;
}

Apr 6 '07 #1
7 1369
On 5 Apr 2007 20:12:30 -0700, "iwasinnihon" <iw*********@hotmail.com>
wrote in comp.lang.c:
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 news:comp.programming.threads, or an OS specific group, perhaps
news:comp.os.linux.development or news:comp.unix.programmer. Or a
Windows group like news:comp.os.ms-windows.programmer.win32.

--
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
Apr 6 '07 #2
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
Apr 6 '07 #3
In article <11**********************@n76g2000hsh.googlegroups .com>,
iwasinnihon <iw*********@hotmail.comwrote:
>I am trying to teach myself more about synchronizing threads. I took
Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

Useful clc-related links:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language

Apr 6 '07 #4
"Kenny McCormack" <ga*****@xmission.xmission.comha scritto nel messaggio
news:ev**********@news.xmission.com...
In article <11**********************@n76g2000hsh.googlegroups .com>,
iwasinnihon <iw*********@hotmail.comwrote:
>>I am trying to teach myself more about synchronizing threads. I took

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

Useful clc-related links:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language
Empty posts serve no purpose, and signatures should begin with
<newline>--<newline>. And check wheter there are some missing apostrophes in
yours. :-)
Apr 6 '07 #5
"Army1987" <pl********@for.itwrites:
"Kenny McCormack" <ga*****@xmission.xmission.comha scritto nel messaggio
news:ev**********@news.xmission.com...
[snip]
Empty posts serve no purpose, and signatures should begin with
<newline>--<newline>. And check wheter there are some missing apostrophes in
yours. :-)
Please don't feed the troll.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 6 '07 #6
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
Apr 6 '07 #7
iwasinnihon wrote:
>
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

--
Posted via a free Usenet account from http://www.teranews.com

Apr 7 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Keith Veleba | last post by:
Hello to all fellow c.l.p'ers! Long-time listener, first-time caller. Background: I'm working on a project where I have to do some serious multithreading. I've worked up a decorator in Python...
0
by: SQLServer007 | last post by:
25 more days until the "get it free" promotion runs out for xSQL Object (you can get it from http://www.x-sql.com) Here are just some of the great features packed in the product: - Compare SQL...
9
by: Ralph Cramden | last post by:
I'm writing a VB6 app which calls several stored procedures on my SQL Server DB. The first stored procedure must complete its inserts before the second stored procedure can query the modified...
1
by: Phil Matish, MCSE | last post by:
I have an Access database that I use frequently. Often, I take it to a home PC, or laptop to work on at night. The next day, I overwrite the old one with the one I have been working on. The...
2
by: Chuck | last post by:
I have not been able to find a good answer about the System.Collections.Queue.Synchronized() method and how it actually works or is to be used. If I create my Queue in the following manner: ...
2
by: Christopher D. Wiederspan | last post by:
We are getting ready to move an ASP.NET application off of a single development machine and onto a "webfarm". Basically our webfarm is a bunch of identical servers with the load-balancing provided...
0
by: Salamandras | last post by:
Greetings I would like some help on achieving synchronization between multiple threads running a for loop. In more detail I would like each thread to be in the same iteration with each other...
1
by: mczard | last post by:
In my application events can be raised in one thread, but delegates can be added to the events from another thread. I wonder if I really have to synchronize the event raising and all += / -=...
8
by: colin | last post by:
Hi, this probaly isnt the most relevant place to ask this, but Im using a windows forms in c# timer to process user input and invalidate a window if its changed. Im having problems in that the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.