Sha256: 17b180ff5cceef279a663b43edc72c79af2e8be1465e6930160d999f039dc251

Contents?: true

Size: 648 Bytes

Versions: 1

Compression:

Stored size: 648 Bytes

Contents

#include <iostream>
#include <mutex>
#include <thread>

int g_i = 0;
std::mutex g_i_mutex;  // protects g_i

volatile __thread char *c;

void safe_increment() {
  std::lock_guard<std::mutex> lock(g_i_mutex);
  ++g_i;

  for (size_t i = 0; i < 100; ++i) {
    c = new char[16];
    delete c;
  }

  std::cout << std::this_thread::get_id() << ": " << g_i << '\n';

  // g_i_mutex is automatically released when lock
  // goes out of scope
}

int main() {
  std::cout << __func__ << ": " << g_i << '\n';

  std::thread t1(safe_increment);
  std::thread t2(safe_increment);

  t1.join();
  t2.join();

  std::cout << __func__ << ": " << g_i << '\n';
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mesh-rb-0.0.1 ext/mesh/mesh/src/testing/thread.cc