Добавить комментарий - CodeHelper

Добавить комментарий

На CitForum есть достаточно аподробная статься под название "Потоки"(из серии "Программирование для Linux", журнал Linux Format)

Небольшой пример программы оттуда:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
void * thread_func(void *arg)
{ 
  int i;
  int loc_id = * (int *) arg;
  for (i = 0; i < 4; i++) {
    printf("Thread %i is running\n", loc_id);
    sleep(1);
  }
}
int main(int argc, char * argv[])
{ 
  int id1, id2, result;
  pthread_t thread1, thread2;
  id1 = 1;
  result = pthread_create(&thread1, NULL, thread_func, &id1);
  if (result != 0) {
    perror("Creating the first thread");
    return EXIT_FAILURE;
  }
  id2 = 2;
  result = pthread_create(&thread2, NULL, thread_func, &id2);
  if (result != 0) {
    perror("Creating the second thread");
    return EXIT_FAILURE;
  }
  result = pthread_join(thread1, NULL);
  if (result != 0) {
    perror("Joining the first thread");
    return EXIT_FAILURE;
  }
  result = pthread_join(thread2, NULL);
  if (result != 0) {
    perror("Joining the second thread");
    return EXIT_FAILURE;
  }
  printf("Done\n");
  return EXIT_SUCCESS;
}
Внимание! Вы собираетесь отправить информацию от имени анонимного пользователя.
v1.7.123.556
© 2009—2010 CodeHelper FAQ | О сайте | Обратная связь | История изменений | Статьи
Creative Commons LicenseМатериалы сайта распространяются под лицензией Creative Commons Attribution-Share Alike 3.0 Unported.