Skip to content

Instantly share code, notes, and snippets.

@toddlipcon
Created February 20, 2019 05:06
Show Gist options
  • Select an option

  • Save toddlipcon/d4a44da7dcc4caafad19142da22708e8 to your computer and use it in GitHub Desktop.

Select an option

Save toddlipcon/d4a44da7dcc4caafad19142da22708e8 to your computer and use it in GitHub Desktop.
// Compile with: clang++ -o test -lkrb5 -pthread test.cc
#include <krb5/krb5.h>
#include <iostream>
#include <functional>
#include <atomic>
#include <cassert>
#include <unistd.h>
#include <thread>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
krb5_context ctx;
int rc = krb5_init_context(&ctx);
assert(rc == 0);
#ifdef DO_THROWAWAY_CALL
char* junk = nullptr;
krb5_get_default_realm(ctx, &junk);
free(junk);
#endif
atomic<int> state { 0 };
vector<thread> threads;
for (int i = 0; i < 10; i++) {
threads.emplace_back([&]() {
while (state == 0) {}
while (state == 1) {
krb5_principal princ;
int rc = krb5_parse_name(ctx, "foo", &princ);
if (rc != 0) {
std::unique_ptr<const char, std::function<void(const char*)>> err_msg(
krb5_get_error_message(ctx, rc),
bind(krb5_free_error_message, ctx, std::placeholders::_1));
cerr << "err: " << err_msg.get() << endl;
abort();
}
krb5_free_principal(ctx, princ);
}
});
}
state = 1;
sleep(1);
state = 2;
for (auto& t : threads) t.join();
cout << "Success!" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment