#include <xtd/collections/generic/list>
#include <xtd/threading/interlocked>
#include <xtd/threading/semaphore>
#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/startup>
 
 
namespace mutex_example {
  class program {
  public:
    static void main() {
      
      
      
      
      
      
      
      
      for(auto i = 1; i <= 5; ++i) {
        threads.emplace_back(worker);
        
        
        
        threads.back().start(i);
      }
      
      
      
      
      thread::sleep(500);
      
      
      
      
      
      
      
      console::write_line("Main thread calls Release(3).");
      pool.release(3);
      
      thread::sleep(100);
      console::write_line("Main thread exits.");
      
      
      
      thread::join_all(threads, 100);
    }
    
    static void worker(std::any num) {
      
      
      console::write_line("thread {0} begins "
                          "and waits for the semaphore.", num);
      pool.wait_one();
      
      
      int padding = interlocked::add(padding, 100);
      
      console::write_line("thread {0} enters the semaphore.", num);
      
      
      
      
      
      thread::sleep(1000 + padding);
      
      console::write_line("thread {0} releases the semaphore.", num);
      console::write_line("thread {0} previous semaphore count: {1}",
                          num, pool.release());
    }
    
  private:
 
    
    
    
    
    inline static int padding;
    
  };
}
 
 
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:71
 
Limits the number of threads that can access a resource or pool of resources concurrently.
Definition semaphore.hpp:38
 
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:175
 
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:15
 
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:11
 
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10