Threads

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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <thread>
#include <string>

int main()
{
	for(std::size_t i = 0; i < 64; ++i)
	{
		std::thread t1 {[]{ std::cout << std::string(64, '0') << std::flush; }};
		std::thread t2 {[]{ std::cout << std::string(64, '1') << std::flush; }};
		t1.join(); t2.join();
		std::cout << std::endl;
	}
}
Last edited on
1
2
C:\Users\************\Documents\Untitled1.c|1|fatal error: iostream: No such file or directory|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 1 seconds) ===|


:/
Last edited on
You'd better completely delete and reinstall your compiler ;)

EDIT: Or change the file extension to .cpp ...
Last edited on
closed account (1yR4jE8b)
@devon

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.
Last edited on
Define 'interesting'.
$ ./test
11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000

etc

PS: gets a bit messier with gcc (but not clang/libc++) if you remove thread safety from std::cout by calling std::cout.sync_with_stdio(false);
Last edited on
http://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio
cppreference wrote:
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.
closed account (3qX21hU5)
Had to cut the output a bit to make it fit, but this is on VS2013 I don't really see anything "interesting" personally but I may be missing something.

00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111
00000000000000000000000000000000000000000000000000000000000000001111111111111111
111111111111111111111111111111111111111111111111

Is that in debug or release?
LB wrote:
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;
    }
}


ЯЯЯЯЯЯЯЯЯЯЯяЯЯЯяЯЯЯяЯяяяЯяЯяЯЯЯЯяЯяяяЯяЯяяяЯяЯяяяЯяЯяяяяЯяЯЯЯяЯяЯЯЯяяЯяЯяЯяЯяЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯяяяяяяяяяяяяяяяяяяяяяяяяяяяяяя
яяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯ
ЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяя
ЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯЯяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяяя
яяяяяяяяяяяяЯяЯЯЯЯяяяЯяяяЯяЯяЯЯЯяяЯяЯЯЯяяЯяЯЯЯЯЯяЯяяяЯяЯяяяяЯяЯЯЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯЯяЯяЯяЯяЯяЯяЯяЯяЯЯЯЯЯЯЯЯЯ
яяяяяяяяЯяЯЯЯЯЯЯяяЯяЯяЯяЯяЯяЯяЯяЯЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯЯЯ
яяяяяяяяяяяяяяяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯяЯЯяЯЯЯЯяЯяЯяяяЯяЯЯЯЯЯяяЯяЯЯЯЯЯяЯяяяЯяЯяяяЯяЯяЯЯЯЯяЯяЯЯЯЯЯяЯяяяЯяЯЯЯЯЯяЯяяяЯяЯЯЯ
Last edited on
closed account (o3hC5Di1)
L B wrote:
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.

All the best,
NwN
Topic archived. No new replies allowed.