Skip to content

Instantly share code, notes, and snippets.

@nixpulvis
Last active March 19, 2020 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixpulvis/8b143383c4d797858f7b83b5cd53e1da to your computer and use it in GitHub Desktop.
Save nixpulvis/8b143383c4d797858f7b83b5cd53e1da to your computer and use it in GitHub Desktop.
Intel pthreads Issue
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
static pthread_t thread;
static pthread_mutex_t mutex;
void* work(void* ptr)
{
pthread_detach(pthread_self());
for (;;) {
if (pthread_mutex_trylock(&mutex))
continue;
printf("thread");
printf("\n");
pthread_mutex_unlock(&mutex);
}
}
int main()
{
pthread_mutex_init(&mutex, NULL);
pthread_create(&thread, NULL, work, NULL);
for (;;) {
// XXX: This will cause some systems to segfault.
// https://software.intel.com/en-us/forums/intel-isa-extensions/topic/675036
pthread_mutex_unlock(&mutex);
if (pthread_mutex_trylock(&mutex))
continue;
printf("main");
printf("\n");
pthread_mutex_unlock(&mutex);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment