Skip to content

Instantly share code, notes, and snippets.

@nyeecola
Created Mar 6, 2019
Embed
What would you like to do?
C version
#include <SDL2/SDL.h>
#include <pthread.h>
#include <stdlib.h>
#define POINTS_LEN 100000
#define THREAD_NUM 4
typedef struct {
float x;
float y;
} vec2;
vec2 points[POINTS_LEN] = {0};
static unsigned int seed = 30;
void * restrict calc(void * restrict args) {
long aux = (long) args;
for (int i = POINTS_LEN/THREAD_NUM * aux; i < ((aux == THREAD_NUM-1) ? POINTS_LEN : (POINTS_LEN/THREAD_NUM * (aux+1))); i++) {
points[i].x += (float)rand_r(&seed)/(float)(RAND_MAX) > 0.5 ? rand_r(&seed)/((float)RAND_MAX/5) : -rand_r(&seed)/((float)RAND_MAX/5);
points[i].y += (float)rand_r(&seed)/(float)(RAND_MAX) > 0.5 ? rand_r(&seed)/((float)RAND_MAX/5) : -rand_r(&seed)/((float)RAND_MAX/5);
}
}
int main(void){
pthread_t threadIds[THREAD_NUM];
Uint64 curTime = SDL_GetPerformanceCounter();
Uint64 lastTime = 0;
double dt = 0;
while (1) {
for(long i = 0; i < THREAD_NUM; i++){
pthread_create(&threadIds[i], NULL, calc, (void *) i);
}
for(int i = 0; i < THREAD_NUM; i++){
pthread_join(threadIds[i], NULL);
}
lastTime = curTime;
curTime = SDL_GetPerformanceCounter();
dt = (double) ((abs(curTime - lastTime)) / (double) SDL_GetPerformanceFrequency());
printf("FPS: %lf\n", 1/dt);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment