Skip to content

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

Description

@jnorwood

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

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions