Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use of non-threadsafe std::cout in examples, such as fig_3_03 #14

Open
jnorwood opened this issue Jan 2, 2021 · 0 comments
Open

use of non-threadsafe std::cout in examples, such as fig_3_03 #14

jnorwood opened this issue Jan 2, 2021 · 0 comments

Comments

@jnorwood
Copy link

jnorwood commented Jan 2, 2021

The examples often use std::cout inside tasks, and the output can get pretty jumbled.
For example, fig_3_03 output of

std::cout << "first node received: " << in << std::endl;

I've been changing these to, for example
`
std::stringstream ss;

  ss << "first node received: " << in << std::endl; 

  std::cout << ss.str();

`
This seems to work, although it isn't clear to me that even it is guaranteed.
So, I'm adding a cout_locked function that should be guaranteed in my examples

`
tbb::spin_mutex mylock;

void cout_locked(const std::string &ss)

{

tbb::spin_mutex::scoped_lock smut(mylock);

std::cout << ss;

}
`
and so the code above becomes

`
std::stringstream ss;

  ss << "first node received: " << in << std::endl;

  //std::cout << ss.str();

  cout_locked(ss.str());

`

The problem with the current output is easy to see if the fig_3_03 example is modified to send more messages in the test

`
// step 4: send messages

for (int i=0;i<100;i++)

my_first_node.try_put(i);

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant