Warning: mysqli::__construct(): (HY000/1203): User howardkn already has more than 'max_user_connections' active connections in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\includes\artfuncs.php on line 21
Failed to connect to MySQL: (1203) User howardkn already has more than 'max_user_connections' active connections
Warning: mysqli::query(): Couldn't fetch mysqli in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\index.php on line 66
Article <vc1q7b$v34v$1@raubtier-asyl.eternal-september.org>
Deutsch   English   Français   Italiano  
<vc1q7b$v34v$1@raubtier-asyl.eternal-september.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita Montero <Bonita.Montero@gmail.com>
Newsgroups: comp.lang.c
Subject: My PC's cores can synchronize to about 1.000 clock cycles accuracy
Date: Fri, 13 Sep 2024 18:45:41 +0200
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <vc1q7b$v34v$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 13 Sep 2024 18:45:31 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="d68882be51447eabb86122ef614d859e";
	logging-data="1019039"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX192+JMLpBEwr7P394om22jkDS5WktfNKFo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:0gvqbmkqIOUVaTVklGlsG6/pFdo=
Content-Language: de-DE
Bytes: 2248

#include <iostream>
#include <barrier>
#include <thread>
#include <vector>
#if defined(_WIN32)
	#include <intrin.h>
#elif defined(__linux__)
	#include <x86intrin.h>
#endif

using namespace std;

int main()
{
	unsigned hc = thread::hardware_concurrency();
	barrier bar( hc );
	atomic_uint synch( hc );
	atomic_uint64_t zero( 0 );
	atomic_int64_t diffs( 0 );
	auto thr = [&]()
	{
		int64_t sum = 0;
		for( unsigned t = 1'000; t; --t )
		{
			bar.arrive_and_wait();
			if( synch.fetch_sub( 1, memory_order_relaxed ) > 1 )
				while( synch.load( memory_order_relaxed ) );
			uint64_t tsc = __rdtsc(), expected = 0;
			if( !zero.compare_exchange_weak( expected, tsc, memory_order_relaxed, 
memory_order_relaxed ) )
				sum += abs( (int64_t)(expected - tsc) );
			bar.arrive_and_wait();
			synch.store( hc );
			zero.store( 0, memory_order_relaxed );
		}
		diffs.fetch_add( sum, memory_order_relaxed );
	};
	vector<jthread> threads;
	threads.reserve( hc - 1 );
	for( unsigned t = hc - 1; t; --t )
		threads.emplace_back( thr );
	thr();
	threads.resize( 0 );
	cout << (double)diffs.load( memory_order_relaxed ) / (1'000.0 * hc) << 
endl;
}

My PC is a AMD 7950X 16-core system.