Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
C++17 – New features in old language

C++20: semaphores

There are two types of semaphores:

  • std::binary_semaphore
  • std::counting_semaphore

Semaphore alllows multiple access to shared resources. Unlike std::mutex, semaphore can be locked in one thread, and unlocked in another.

std::binary_semaphore

std::binary_semaphore is just alias for std::counting_semaphore.

using binary_semaphore = std::counting_semaphore<1>;

std::counting_semaphore

Semaphore has in implementation counter, which defines, how many threads can enter to locked section.

For example:

std::counting_semaphore<3> sem{ 0 };

In this case, maximum counter values can be 3. And counter is initialized to 0.

acquire() – decrements value, and if counter is already 0, will block until counter is larger than 0.

release() – increments counter

For example:

#include <iostream>

#include <thread>

#include <semaphore>

std::counting_semaphore<1> sem{ 0 };

void thread_func1()

{

sem.acquire();

std::cout << "analyze data()" << std::endl;

}

void thread_func2()

{

std::cout << "getting data()" << std::endl;

sem.release();

}

int main()

{

std::jthread thread1{ thread_func1 };

std::jthread thread2{ thread_func2 };

}

In this case, first we need to wait for data in first thread.

C++20: std::jthread


Warning: Undefined array key 0 in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 532

Fatal error: Uncaught Error: Call to a member function id() on array in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php:36 Stack trace: #0 /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php(538): CrayonFormatter::format_code('', Array, Object(CrayonHighlighter)) #1 [internal function]: CrayonFormatter::delim_to_internal(Array) #2 /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php(516): preg_replace_callback('#()#msi', 'CrayonFormatter...', 'void _internal_...') #3 /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_highlighter.class.php(166): CrayonFormatter::format_mixed_code('void _internal_...', Object(CrayonLang), Object(CrayonHighlighter)) #4 /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_highlighter.class.php(186): CrayonHighlighter->process() #5 /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php(703): CrayonHighlighter->output(true, false) #6 /home/cplusp00/cplusplus2017.info/www/wp-includes/class-wp-hook.php(310): CrayonWP::the_content('\n<p class="has-...') #7 /home/cplusp00/cplusplus2017.info/www/wp-includes/plugin.php(205): WP_Hook->apply_filters('\n<p class="has-...', Array) #8 /home/cplusp00/cplusplus2017.info/www/wp-includes/post-template.php(256): apply_filters('the_content', '<!-- wp:paragra...') #9 /home/cplusp00/cplusplus2017.info/www/wp-content/themes/twentyseventeen/template-parts/post/content.php(54): the_content('Continue readin...') #10 /home/cplusp00/cplusplus2017.info/www/wp-includes/template.php(787): require('/home/cplusp00/...') #11 /home/cplusp00/cplusplus2017.info/www/wp-includes/template.php(720): load_template('/home/cplusp00/...', false, Array) #12 /home/cplusp00/cplusplus2017.info/www/wp-includes/general-template.php(206): locate_template(Array, true, false, Array) #13 /home/cplusp00/cplusplus2017.info/www/wp-content/themes/twentyseventeen/index.php(47): get_template_part('template-parts/...', '') #14 /home/cplusp00/cplusplus2017.info/www/wp-includes/template-loader.php(106): include('/home/cplusp00/...') #15 /home/cplusp00/cplusplus2017.info/www/wp-blog-header.php(19): require_once('/home/cplusp00/...') #16 /home/cplusp00/cplusplus2017.info/www/index.php(17): require('/home/cplusp00/...') #17 {main} thrown in /home/cplusp00/cplusplus2017.info/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 36