diff --git a/ptypes-semaphore/build.bat b/ptypes-semaphore/build.bat new file mode 100644 index 0000000..0692827 --- /dev/null +++ b/ptypes-semaphore/build.bat @@ -0,0 +1,4 @@ +@echo off +call ../vcppbuild.bat semaphore "/link user32.lib ../ext/libs/ptypes.lib" +semaphore.exe +pause \ No newline at end of file diff --git a/ptypes-semaphore/build.sh b/ptypes-semaphore/build.sh new file mode 100644 index 0000000..6ab5720 --- /dev/null +++ b/ptypes-semaphore/build.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +gcc -o main -lptypes semaphore.cpp +./main \ No newline at end of file diff --git a/ptypes-semaphore/semaphore.cpp b/ptypes-semaphore/semaphore.cpp new file mode 100644 index 0000000..84c8edd --- /dev/null +++ b/ptypes-semaphore/semaphore.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include + +USING_PTYPES + +#define BUFFER_SIZE 10 + +volatile int buffer[BUFFER_SIZE]; +semaphore s(0); + +DWORD WINAPI produce(void * data) +{ + int i; + srand ((unsigned int)time(NULL)); + //while (counter == BUFFER_SIZE - 1); + + for (i = 0; i < BUFFER_SIZE; i++) + buffer[i] = rand() % 20 + 1; // yes I know rand is not thread safe - I just need a way to demonstrate a producing/consuming problem - using the same number doesn't help. + s.post(); + return 0; + +} + +DWORD WINAPI consume(void * data) +{ + s.wait(); + int i = 0; + for(i = 0; i < BUFFER_SIZE; i++) + { + printf("CONSUMER value = %i\n", buffer[i]); + buffer[i] = 0; + } + return 0; +} + +int main() { + int i; + HANDLE thread1, thread2; + for(i = 0; i < BUFFER_SIZE; i++) + buffer[i] = -1; + thread1 = CreateThread(NULL, 0, produce, NULL, 0, NULL); + thread2 = CreateThread(NULL, 0, consume, NULL, 0, NULL); + WaitForSingleObject(thread1, INFINITE); + WaitForSingleObject(thread2, INFINITE); +} \ No newline at end of file