You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
909 B
47 lines
909 B
#include "thread/Thread.h" |
|
#include "thread/Blocker.h" |
|
|
|
#include <stdio.h> |
|
#include <unistd.h> |
|
|
|
static const int COUNT = 3; |
|
static const int SLEEP_US = 200*1000; |
|
|
|
|
|
void* run(void* arg); |
|
|
|
int main() { |
|
Thread* thread = NULL; |
|
BlockerHandle blocker = createBlocker(); |
|
if (blocker == -1) { |
|
printf("Main: Failed to create blocker.\n"); |
|
return 1; |
|
} |
|
|
|
thread = createThread(&run, &blocker); |
|
|
|
for (int i = 0; i < COUNT+1; i++) { |
|
printf("Main: %d\n", i); |
|
usleep(SLEEP_US); |
|
} |
|
notifyBlocker(blocker); |
|
|
|
joinThread(thread); |
|
freeThread(&thread); |
|
freeBlocker(&blocker); |
|
return 0; |
|
} |
|
|
|
// --- |
|
|
|
void* run(void* arg) { |
|
BlockerHandle* blocker = arg; |
|
for (int i = 0; i < COUNT; i++) { |
|
printf("Thread: %d\n", i); |
|
usleep(SLEEP_US); |
|
} |
|
printf("Blocker ID: %d\n", *blocker); |
|
lockBlocker(*blocker); |
|
printf("Thread: Blocker notified, continuing execution.\n"); |
|
return NULL; |
|
}
|
|
|