Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Распараллеливание программ. Учебник

.pdf
Скачиваний:
0
Добавлен:
07.09.2026
Размер:
1 Мб
Скачать
141
#define errno_abort(text) do { \ fprintf(stderr, «%s at \»%s\»:%d: %s\n», \ text, __FILE__, __LINE__, strerror(errno)); \ abort(); \ } while(0);
/* Структура, которую будем помещать в TLS */ typedef struct tld_tag { pthread_t thread_id; char *string; } tld_t;
pthread_key_t tld_key; /* Thread Local Data Key */ pthread_once_t key_once = PTHREAD_ONCE_INIT;
/* Функция для одноразовой инициализации */ void once_func(void) { int status; printf(«Инициализируем ключ\n»); status = pthread_key_create(&tld_key, free); if (status != 0) err_abort(status, «Create key»);
}
/* Функция нити */ void* thread_func(void *data) { tld_t *value; int status;
status = pthread_once(&key_once, once_func); if (status != 0) err_abort(status, «Once init»); value = (tld_t*) malloc(sizeof(tld_t)); if (value == NULL) errno_abort(«Allocate key value»); status = pthread_setspecific(tld_key, value); if (status != 0) err_abort(status, «Set tld»); printf(«%s set tld value %p\n», data, value); value->thread_id = pthread_self(); value->string = (char*) data; value = (tld_t*) pthread_getspecific(tld_key); printf(«%s starting...», value->string); sleep(2); value = (tld_t*) pthread_getspecific(tld_key); printf(«%s done...», value->string); return NULL;
}
int main(int argc, char *argv[], char *emvp[]) { pthread_t thread1, thread2, thread3; int status;
142
status = pthread_create(&thread1, NULL, thread_func, «Thread 1»); if (status != 0) err_abort(status, «Create thread 1»); status = pthread_create(&thread2, NULL, thread_func, «Thread 2»); if (status != 0) err_abort(status, «Create thread 2»); status = pthread_create(&thread3, NULL, thread_func, «Thread 3»); if (status != 0) err_abort(status, «Create thread 3»); pthread_exit(NULL);
}
143
ПЕРЕДАЧА СООБЩЕНИЙ
    
             
              
Обзор языков и систем
              
 
                  
144
  
                   
              
     
                 
                    
       

145
  
              
C
h a r m ++
       
                             
         
PVM
                                

 
146
     
                                       
                

         
     pvm_     PVMF           
147
   
  
#include «pvm3.h» main () { int cc, tid, msgtag; char buf[100];
printf(«i'm t%x\n», pvm_mytid());
cc = pvm_spawn(«hello_other», (char**)0, 0, «», 1,
&tid);
if (cc == 0) {
msgtag = 1; pvm_recv(tid, msgtag); pvm_upkstr(buf); printf(«from t%x# %s\n», tid, buf); } else printf(«can't start hello_other\n»); pvm_exit();
}
 
pvm_mytid()
           
pvm_spawn()
   
pvm_exit()
hello_other
pvm_recv() 
pvm_upkstr()
                             
pvm_initsend()   
pvm_pkstr()    
pvm_send()

#include «pvm3.h» main () { int ptid, msgtag; char buf[100];
ptid = pvm_parent();
148
strcpy(buf, «hello, world from «); gethostname(buf + strlen(buf), 64); msgtag = 1; pvm_initsend(PvmDataDefault); pvm_pkstr(buf); pvm_send(ptid, msgtag); pvm_exit();
}
                  
      
Erlang
                 
 
  
      
  
149

           
   
        

-module(math1).
-export([factorial/1]). factorial(0) -> 1; factorial(N) -> N*factorial (N-1).

-module(math3).
-export([area/1]). area({ square, Side }) -> Side * Side ; area({ rectangle, X, Y}) ->
X * Y;
area({ circle, Radius }) ->
3.14159 * Radius * Radius ; area({ triangle, A, B, C}) ->
S= (A+B+C)/2,
math: sqrt(S * (S-A) * (S-B) * (S-C)).


123, 3.1415926532, 0.278e1, -1.2e-45
  abc'атом.с.пробелами'hello_world

n      
{a, b, c}, { 1,2,3 }, {wx, 12, 'hello', {}}
       
[], [a, b, 22], [12], ['hello .world', 89]
150



X= { book , preface , acknowledgments , contents , { chapters , [ { chapter , 1, 'An.Erlang.Tutorial' } , { chapter , 2, ... }
] }}.
          
       
-module(dates).
-export([classify_day/1]). classify_day(saturday) -> weekEnd; classify_day(sunday) -> weekEnd; classify_day(_) -> weekDay.
  
-module(temp).
-export([convert/2]). convert({fahrenheit, Temp }, celsius) -> { celsius , 5*(Temp - 32) /9 } ; convert({celsius, Temp}, fahrenheit) -> { farenheit , 32 + Temp*9 / 5 } ; convert({reaumur, Temp}, celsius) -> { celsius , 10*Temp / 8 } ; convert({celsius, Temp}, reaumur ) -> { reaumur , 8*Temp / 10 } ; convert({X, _ }, Y) -> { cannot , convert ,X, to ,Y} .
    
Pattern = Expression,
Expression Pattern  
Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]