ideone.com has multithreading disabled, and the compilers on my system don't have std::thread in the standard library yet. Could people run this code can tell me if the output is anything interesting?
If you are compiling with Visual Studio, your source file has a .c extensions (and not .cpp). Visual Studio will disable the C++ "part" of the compiler when compiling C files by default, which is why it can't find iostream.
okay dont worry about me concentrate on doin what lb said, im not using linux I realize I have to do this with sfml, havent put it on my new computer yet, fortunatley naruku gave me an awsome guide.
In addition, synchronized C++ streams are guaranteed to be thread-safe (individual characters output from multiple threads may interleave, but no data races occur)
It was the interleaving characters thing I was interested in - obviously I misunderstood how to reproduce it.
It was the interleaving characters thing I was interested in - obviously I misunderstood how to reproduce it.
it's not guaranteed. In particular, in gcc's implementation, operator<< for strings calls __ostream_insert, which calls __ostream_write, which calls sputn/xsputn, which, for a synchronized cout, directly calls C's fwrite, which, at least according to gnu docs, has an internal mutex to serialize accesses to the FILE*
PS: a simple way to see characters get interleaved on gcc/linux is to use std::wcout to print out some wide characters with UTF-8 locale on
1 2 3 4 5 6 7 8 9 10 11 12 13
int main()
{
std::wcout.imbue(std::locale("en_US.utf8"));
std::setlocale(LC_ALL, "en_US.utf8"); // if you sync with stdio,
// gcc needs this for C layer
for(std::size_t i = 0; i < 64; ++i)
{
std::thread t1 {[&]{ std::wcout << std::wstring(64, L'Я') << std::flush ; } };
std::thread t2 {[&]{ std::wcout << std::wstring(64, L'я') << std::flush ; } };
t1.join(); t2.join();
std::cout << std::endl;
}
}
ideone.com has multithreading disabled, and the compilers on my system don't have std::thread in the standard library yet.
You probably know this, but I thought I'd mention this for completeness, boost::thread offers pretty much the same functionality as std::thread. If I'm not mistaken the latter is actually based on the former.