Содержание
- 2. Средства разработки (A_Quickstart_Guide.pdf) Практика_1 2015 v.01 Средства разработки (QNX Momentics ID, C, C++, библиотеки, документация )
- 3. #include int pthread_create( pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void* ), void* arg ); thread –
- 4. Практика_1 2015 v.01 Создание нити, синхронизация завершения #include #include #include #include void* test_t(void* arg) { printf("Thread
- 5. Семафоры, мьютексы, условные переменные #include int sem_init( sem_t * sem, int pshared, unsigned value ); int
- 6. Вызовы функций семафора #include #include #include #include #include void f(pthread_t id) { printf("Thread %d called this
- 7. Вызовы функций мьютекса #include #include pthread_mutex_t mutex int count = 0; void* thr_1( void* arg )
- 9. Скачать презентацию
Средства разработки
(A_Quickstart_Guide.pdf)
Практика_1 2015 v.01
Средства разработки
(QNX Momentics ID, C, C++, библиотеки,
Средства разработки
(A_Quickstart_Guide.pdf)
Практика_1 2015 v.01
Средства разработки
(QNX Momentics ID, C, C++, библиотеки,
Инструментальная ВМ
(Development host)
QNX Neutrino RTOS,
драйверы, приложение
Целевая платформа
(Target system)
C, C++, библиотеки, документация
QNX Neutrino RTOS,
приложение
VMware player
QNX Momentics ID
#include
int pthread_create( pthread_t* thread, const pthread_attr_t* attr,
void* (*start_routine)(void* ),
#include
int pthread_create( pthread_t* thread, const pthread_attr_t* attr,
void* (*start_routine)(void* ),
thread – идентификатор нити (ID), устанавливается при создании;
attr – атрибутная запись, при значении NULL устанавливается по умолчанию;
void* (*start_routine)(void* ) – функция, код которой выполняется в потоке;
arg – аргумент, передаваемый в функцию потока
Простой вызов - pthread_create(&t, NULL, &func, NULL)
pthread_t pthread_self( void ); - возвращает ID потока;
int pthread_t thread, void** value_ptr ); - возвращает результат выполнения (0 –успешное)
Простой вызов - pthread_join( t, NULL);
#include
unsigned int sleep( unsigned int seconds );
Функции для работы с нитями
Практика_1 2015 v.01
Практика_1 2015 v.01
Создание нити,
синхронизация завершения
#include
#include
#include
#include
void* test_t(void*
Практика_1 2015 v.01
Создание нити,
синхронизация завершения
#include
#include
#include
#include
void* test_t(void*
printf("Thread %d started \n", pthread_self());
int i;
for(i=0; i<=10; i++) {
printf("Thread %d is working %d\n", pthread_self(), i);
sleep(1);
}
printf("Thread %d stop\n”, pthread_self());
return EXIT_SUCCESS;
}
int main(int argc, char *argv[]) {
pthread_t thread_id;
pthread_create(&thread_id, NULL, &test_t, NULL);
printf("Main thread stop\n");
pthread_join(thread_id, NULL);
return EXIT_SUCCESS;
}
Семафоры, мьютексы, условные переменные
#include
int sem_init( sem_t * sem, int pshared,
Семафоры, мьютексы, условные переменные
#include
int sem_init( sem_t * sem, int pshared,
int sem_wait( sem_t * sem );
int sem_post( sem_t * sem );
int sem_trywait( sem_t * sem );
int sem_getvalue(sem_t* sem, int* value)
#include
int pthread_mutex_init( pthread_mutex_t* mutex, const pthread_mutexattr_t* attr );
int pthread_mutex_lock( pthread_mutex_t* mutex );
int pthread_mutex_trylock( pthread_mutex_t* mutex );
int pthread_mutex_timedlock( pthread_mutex_t * mutex, const struct timespec *
abs_timeout );
int pthread_mutex_unlock( pthread_mutex_t* mutex );
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int pthread_cond_init( pthread_cond_t* cond, pthread_condattr_t* attr );
int pthread_cond_wait( pthread_cond_t* cond, pthread_mutex_t* mutex );
int pthread_cond_signal( pthread_cond_t* cond );
int pthread_cond_broadcast( pthread_cond_t* cond );
Практика_1 2015 v.01
Вызовы функций семафора
#include
#include
#include
#include
#include
void f(pthread_t id) {
Вызовы функций семафора
#include
#include
#include
#include
#include
void f(pthread_t id) {
}
sem_t s;
void* thread_2(void* arg) {
pthread_t id = pthread_self();
for(;;) {
printf("Tread %d is working\n", id );
sem_wait(&s);
f(id);
sem_post(&s);
usleep(300);
}
return 0;
}
int main(int argc, char *argv[]) {
pthread_t thr_2;
sem_init(&s, NULL, 10);
pthread_t id = pthread_self();
pthread_create(&thr_2, NULL, &thread_2, NULL);
for(;;) {
printf("Tread %d is working\n", id );
sem_wait(&s);
f(id);
sem_post(&s);
usleep(300);
}
printf("Main thread stop\n");
pthread_join(thr_2, NULL);
return EXIT_SUCCESS;
}
Практика_1 2015 v.01
Вызовы функций мьютекса
#include
#include
pthread_mutex_t mutex
int count = 0;
void* thr_1( void*
Вызовы функций мьютекса
#include
#include
pthread_mutex_t mutex
int count = 0;
void* thr_1( void*
{
int tmp = 0;
while( 1 ) {
pthread_mutex_lock( &mutex );
tmp = count++;
pthread_mutex_unlock( &mutex );
printf( "Count is %d\n", tmp );
sleep( 1 );
}
return 0;
}
void* thr_2( void* arg )
{
int tmp = 0;
while( 1 ) {
pthread_mutex_lock( &mutex );
tmp = count--;
pthread_mutex_unlock( &mutex );
printf( "** Count is %d\n", tmp );
sleep( 2 );
}
return 0;
}
int main( void )
{
pthread_create( NULL, NULL, &thr_1, NULL );
pthread_create( NULL, NULL, &thr_2, NULL );
sleep( 10 );
return 0;
}
Практика_1 2015 v.01