Skip to content

Instantly share code, notes, and snippets.

@aolo2
Created October 29, 2021 08:02
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 aolo2/3dcb6a0ab70adf380e4fbc05ed1ea470 to your computer and use it in GitHub Desktop.
Save aolo2/3dcb6a0ab70adf380e4fbc05ed1ea470 to your computer and use it in GitHub Desktop.
// compile with
// gcc -O2 001_ok_cow_initial.c -o 001_ok_cow_initial -pthread -Wall -Wextra
#include <stdint.h> // uint8_t
#include <string.h> // memcmp
#include <pthread.h> // pthread_create, pthread_join, pthread_mutex_lock, pthread_mutex_unlock
#include <stdbool.h> // bool, true, false
#define xylen 1024
static uint8_t voxelGroups[321536][xylen];
static int threadCount = 4;
static int identicalVoxelGroupsCount = 0;
static pthread_mutex_t mutex;
bool areVoxelGroupsIdentical(uint8_t firstArray[xylen], uint8_t secondArray[xylen]){
return memcmp(firstArray, secondArray, xylen*sizeof(uint8_t)) == 0;
}
void* getIdenticalVoxelGroupsCount(void* threadNumber){
for(int i = (long)threadNumber-1; i < 321536-1; i += threadCount){
for(int j = i+1; j < 321536; j++){
if(areVoxelGroupsIdentical(voxelGroups[i], voxelGroups[j])){
pthread_mutex_lock(&mutex);
identicalVoxelGroupsCount++;
pthread_mutex_unlock(&mutex);
}
}
}
return 0;
}
int main(){
pthread_t thread1, thread2, thread3, thread4;
pthread_mutex_init(&mutex, 0);
pthread_create(&thread1, NULL, getIdenticalVoxelGroupsCount, (void *)1);
pthread_create(&thread2, NULL, getIdenticalVoxelGroupsCount, (void *)2);
pthread_create(&thread3, NULL, getIdenticalVoxelGroupsCount, (void *)3);
pthread_create(&thread4, NULL, getIdenticalVoxelGroupsCount, (void *)4);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
pthread_join(thread4, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment