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.
85 lines
2.1 KiB
85 lines
2.1 KiB
/*
|
|
* Copyright 2002-2019 Intel Corporation.
|
|
*
|
|
* This software is provided to you as Sample Source Code as defined in the accompanying
|
|
* End User License Agreement for the Intel(R) Software Development Products ("Agreement")
|
|
* section 1.L.
|
|
*
|
|
* This software and the related documents are provided as is, with no express or implied
|
|
* warranties, other than those that are expressly stated in the License.
|
|
*/
|
|
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
volatile int iteration_main = 0;
|
|
volatile int iteration_second = 0;
|
|
volatile bool shouldExit = false;
|
|
|
|
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
// Prevent inlining of these functions
|
|
#define EMPTY_FUNC(name) \
|
|
extern "C" void name(); \
|
|
__asm__(".globl " #name "\n .type " #name ", @function\n " \
|
|
#name ":\n ret\n");
|
|
|
|
EMPTY_FUNC(SecondThreadIterationCheckpoint1)
|
|
EMPTY_FUNC(SecondThreadIterationCheckpoint2)
|
|
EMPTY_FUNC(SecondThreadIterationCheckpoint3)
|
|
EMPTY_FUNC(MainThreadIterationCheckpoint)
|
|
|
|
void* SecondThread(void* arg)
|
|
{
|
|
for (iteration_second = 0; iteration_second < 100; )
|
|
{
|
|
SecondThreadIterationCheckpoint1();
|
|
pthread_mutex_unlock(&mutex);
|
|
iteration_second++;
|
|
SecondThreadIterationCheckpoint2();
|
|
pthread_mutex_lock(&mutex);
|
|
SecondThreadIterationCheckpoint3();
|
|
|
|
while (iteration_second > iteration_main)
|
|
{
|
|
pthread_mutex_unlock(&mutex);
|
|
sched_yield();
|
|
pthread_mutex_lock(&mutex);
|
|
}
|
|
}
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
shouldExit = true;
|
|
return NULL;
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
pthread_t thd;
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
pthread_create(&thd, NULL, SecondThread, NULL);
|
|
|
|
do
|
|
{
|
|
MainThreadIterationCheckpoint();
|
|
pthread_mutex_lock(&mutex);
|
|
iteration_main++;
|
|
while (iteration_main >= iteration_second && !shouldExit)
|
|
{
|
|
pthread_mutex_unlock(&mutex);
|
|
sched_yield();
|
|
pthread_mutex_lock(&mutex);
|
|
}
|
|
pthread_mutex_unlock(&mutex);
|
|
}
|
|
while (!shouldExit);
|
|
|
|
printf("Application finished successfully!\n");
|
|
|
|
return 0;
|
|
}
|