Skip to content

Instantly share code, notes, and snippets.

@wzyy2
Created February 26, 2018 01:51
Show Gist options
  • Select an option

  • Save wzyy2/fc8cbf232d3b9f966306d01cb9664468 to your computer and use it in GitHub Desktop.

Select an option

Save wzyy2/fc8cbf232d3b9f966306d01cb9664468 to your computer and use it in GitHub Desktop.
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_thread.h>
#include <iostream>
#include <queue>
#include <string>
SDL_mutex* mutex;
std::queue<int> shared_queue;
int ProducerThreadFunction(void* data) {
printf("Running Producer thread\n");
while (true) {
SDL_LockMutex(mutex);
if (!shared_queue.empty()) {
std::cout << "Pop" << shared_queue.front() << std::endl;
shared_queue.pop();
}
SDL_UnlockMutex(mutex);
}
return 0;
}
int ConsumerThreadFunction(void* data) {
int i = 0;
std::cout << "Running Consumer thread" << std::endl;
while (true) {
SDL_LockMutex(mutex);
if (shared_queue.empty()) {
shared_queue.push(++i);
std::cout << "Push " << i << std::endl;
}
SDL_UnlockMutex(mutex);
}
return 0;
}
int main(int argc, char* args[]) {
SDL_Thread* producer_thread_id =
SDL_CreateThread(ProducerThreadFunction, "Producer", NULL);
SDL_Thread* consumer_thread_id =
SDL_CreateThread(ConsumerThreadFunction, "Consumer", NULL);
mutex = SDL_CreateMutex();
SDL_WaitThread(consumer_thread_id, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment