0

I wrote the following code:

 #include<iostream>
 #include<pthread.h>
 using namespace std;

 void* func(void *i)
 {
  cout<<"in func "<<endl;
 }

 int main()
 {
  pthread_t threads[5];

  for(int i=0;i<5;i++)
  {
  pthread_create(&threads[i], NULL, func, (void*)i);
  cout<<"next for loop"<<endl;
  }

  pthread_exit(NULL);
  return 0;
  }

The output was: enter image description here From the output it seems that the endl line changing property after first display of 'next for loop' gets delayed and endl of 'next for loop' and 'in func' are executed one after the other. This happened everytime i ran the program. Can you tell me the reason for this delay?

2
  • It would appear that your output image was not uploaded Commented Aug 26, 2012 at 11:02
  • What delay? Presumably, there is a mutex lock on count() to prevent gross corruption/crash of the output stream and this is released after each stream operation. The created thread probably just got in between the "next for loop" and the endl. Try putting a '\r\n' in the string instead of streaming an endl. Commented Aug 26, 2012 at 11:52

4 Answers 4

3

I think that

cout <<"next for loop" << endl;

is a compact way of writing

cout << "next for loop"; 
cout << endl; 

Since you are working in a multi-threaded environment, the order of execution is unpredictable. That's what happened in your particular case:

cout << "next for loop";
cout << "in func ";
cout << endl;
cout << endl;
...
Sign up to request clarification or add additional context in comments.

1 Comment

That's correct. cout<<"next for loop"<<endl; is equivalent to (cout<<"next for loop")<<endl;. We often think of it as gluing the two things shifted together and then shifting them, but that is not the semantics of operator<<. There is nothing to hold the lock across the two operator<< calls. This can be quite a surprising result.
1

Streams have locks to protect them, so they single-thread. You'll need some sort of buffer of your own to collect results while running threads.

Comments

0

There is not much to say about the behavior of multi-threaded code where there is no explicit control of the execution flow with locks and semaphores.

For example, in the code you have posted the following can happen:

  1. just after pthread_create() the next for loop sentence gets printed
  2. the newly created thread starts and prints its sentence entirely (in func and endl)
  3. The main thread is run again and it prints the remaining endl

It may also happen that the two endl overleaps in a different way (first the one of the main thread, then the one of the newly created thread).

Note: using std::endl and \n or \r\n is not the same thing.

std::endl also includes an output buffer flushing operation.

Comments

0

As pointed to by some answers above cout << something << endl are two separate pieces of instructions and in a multi-threaded environment cout << endl can very well we pre-empted by another thread just after it has done cout << something. Try this C style newline character(escape sequence) :

#include<iostream>
#include<pthread.h>
 using namespace std;

 void* func(void *i)
 {
  cout<<"in func \n";
  //cout << "\n";
  //cout<<endl;
 }

 int main()
 {
  pthread_t threads[5];

  for(int i=0;i<5;i++)
  {
  pthread_create(&threads[i], NULL, func, (void*)i);
  cout<<"next for loop\n";
  //cout << "\n";
  //cout<<endl;
  }

  pthread_exit(NULL);
  return 0;
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.