os-70-350

os-70-350 Commit Details


Date:2014-02-04 23:33:11 (11 years 7 months ago)
Author:Natalie Adams
Branch:master
Commit:9eccbd6041c754096248e9cfd74d6e64a5b66291
Parents: 3ec953c88fcef4e96c7705cefa094f0c76c8a8f7
Message:Adding semaphore example

Changes:

File differences

ptypes-semaphore/build.bat
1
2
3
4
@echo off
call ../vcppbuild.bat semaphore "/link user32.lib ../ext/libs/ptypes.lib"
semaphore.exe
pause
ptypes-semaphore/build.sh
1
2
3
4
#!/bin/bash
gcc -o main -lptypes semaphore.cpp
./main
ptypes-semaphore/semaphore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <ptypes/ptypes.h>
#include <ptypes/pasync.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
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);
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.13120s using 14 queries.