Skip to content

some puzzles #1

@Spear-Neil

Description

@Spear-Neil

I start with the simple example in readme, but each execution throws a seg fault. It seems that the code in your another repository verlib has some differences. Thus I replace the code in dir flock and parlay with the code in verlib and it works. Is the code in this repository incomplete?

#include <iostream>
#include <vector>
#include <thread>

#include "flock/flock.h"
#include "timer.h"

flck::lock lock;
flck::atomic<int> a = 1;
flck::atomic<int> b = 2;

int main() {
  int nthd = 20;
  uint64_t nops = 100000;

  std::vector<std::thread> workers;
  util::Timer<> timer;
  timer.start();
  for(int tid = 0; tid < nthd; tid++) {
    workers.push_back(std::thread([&]() {
      for(size_t i = 0; i < nops; i++) {
        flck::with_epoch([&]() {
          lock.with_lock([&]() {
            int tmp = a.load();
            a = b.load();
            b = tmp;
            return true;
          });
        });
      }
    }));
  }
  for(int tid = 0; tid < nthd; tid++) {
    workers[tid].join();
  }
  long drt = timer.duration_us();

  std::cout << "-- tpt: " << double(nthd * nops) / drt << std::endl;
  return 0;
}

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions