Problem with two threads switching trying to do printf 
Author Message
 Problem with two threads switching trying to do printf

Hello.  I am programming on NT using threads and I am running into
problems with them both trying to do printf().

I have two threads (dog and cat), which are created suspended.  all
that they do is keep on printing "dog" or "cat".

void dog(char *i) {
        for (i=0; i<1000000; i++) {


void cat(char *i) {
        for (j=0; j<1000000; j++) {


after I create each thread using beginthreadex(), I call a function
which basically allows the threads to take turns running by resuming
one of them, letting it run for one "time unit", then suspending it,
then resuming the other thread and letting it run for one time unit,
then suspending it and so on.  psuedo-code of the function is as

void alternater() {
        HANDLE currentThreadhandle = (handle of dog);
        while (1) {
                currentThread = (calls function that returns the handle of the other
thread. ie if the currentThreadhandle was dog, now it is cat);
                Sleep(0);     /*which is a NT system call which makes alternater
give up the rest of it's time unit and lets another active thread


the output that we are getting is that dog is printing all at once and
then cat start printing only after dog is done printing everything,
when they should be alternating.

Does anybody know what the problem is?  while debugging by steping
through the function, we realized that the alternater is acutally
switching it's CurrentThread value correctly, and that when it is
cat's turn to run, it actually does go into the while loop, however
stops at the printf statement and doesn't do anything, thus resulting
in only dog printing.

I am thinking that the problem is that, the first thread is getting
suspended while it is in the middle of one of it's printf statements
and NT is somehow restricting any one else from being able to call the
printf function.  and since the first thread has printf "locked down",
the second thread is starving and not being allowed to run until the
first thread is done.  is this correct?  if so how do you prevent
this?  if not, what is really going on?

Thank you, i really appreciate your help.

Mon, 04 Apr 2005 15:52:54 GMT  
 Problem with two threads switching trying to do printf


> Hello.  I am programming on NT using threads and I am running into
> problems with them both trying to do printf().

Unfortunately, threads are system-specific - Unix thread help is not
necessarily of any use to NT threads, for example - and this group deals
with portable, Standard C only. Therefore you'll need to ask this in a
group on NT programming, presumably somewhere in microsoft.*.


Mon, 04 Apr 2005 18:52:46 GMT  
 [ 2 post ] 

 Relevant Pages 

1. two DLLs, two threads, problem


3. Two problems when trying to compile

4. Threading problem. I'm doing something wrong

5. Doing stuff in DoModal() - Background thread updating UI problems

6. problem :- worker threads trying to write to the same log file

7. Problem with two threads and AfxMessageBox

8. Problem with two threads and AfxMessageBox

9. bypass /r switch - has it been done?

10. Difference between two different ways of doing things

11. Switch within Struct (trying again)

12. (second try) switch between portrait and landscape in the same document


Powered by phpBB® Forum Software