Advertisement
daniv1

Untitled

Dec 4th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.57 KB | None | 0 0
  1.  
  2. ((include <pthread.h> atnclude <math.h>
  3. i3thread_mutex t mutex = PTHREAP_MUTEX_INITIALIZER; int* stack;int size; int currentPos . 0; void createStack(int s)f size s; stack x new int[sice);
  4. 1 int push(int value) pthread_mutex lock(8mutex); tf (currentPai Px size) f pthread_mutex unlock(8mutex); return -1;
  5. stack[currentPos**1 value; pthread_mutex_unlock(8mutex);
  6. C Ta6ynnail 8 x S. Cx 1 x
  7. lnt RN)) pthread_mutexlock(gmutex); If (currentPas 0) f pthreadmutex_unlock(gmutex); return -1;
  8. 1 currentPos--; pthreadmutex_unlock(gmutex); return stack[currentPas];
  9. 1 int index 0; pthreadmutex t A = PTHREAP_MUTEX_INITIALIZER; int getIndexaf index,: return index;
  10. 1 void* threadFunc(void* p) f pthread_mutex lock(81); int value = gitIndex(); pthread_mutex_unlock(81); int currentVal . value; for (int i . 1; 1 < R; ++l)) if (push(currentVal) 1= -1) printfnlogananno 56-1 In., currentVal); else printf(.Cfes mammAl\n.); currentVal . value *pow(10,1) currentVal;
  11. 1 ONE (int t = 0; U< 6; Ivi) tf ((currentVal . pap()) , -1) printfClimpanenno It In•, currentVal); else printf(.Cfes noposnini\e);
  12. int main() f int THREAD_COUNT = 3; pthread_t threads[THREADCOUNT]; createStack(THREAD_COUNT * 5);
  13. for (int I = 1; A<= THREAD_COUNT; ITS) f pthread_create(gthreads[i - 1], Nutt, threadFunc, NULL); 1 for (int I . 0; I < THREAD_COUNT; *al) pthread_jain(threads[i] , NULL);
  14. return 0;
  15. Sinde <stdlib.h. ((include <unistd.h> ((include <uthread.h>
  16. p.1;r1tIliztacke ruled(= PTHREADJINLOCK_INITIALIZER; uthreadmutex_t AId 11111= PTHREAD_MUTEX_INITIALIZER; int index . int 9etIndex(){ return index; C Illxpuue.a6ynnmff: 8 PI.. 37, Cr. 19 BC
  17. 3( 3-7). UNOIt
  18. void* readThread(void* p) f pthread_mutexlock(8indexPutex); int index . getIndex(); pthread_mutex_uniock(8indexPutex); pthreadrulock_rdlock(8rulock); printf(.NoTis Xi nowan unTaTm\e, index); usieep(500); printf(.NoTis Hopep Xi amima Howep %i\n., index, soneVal); pthread_rulock_unlock(8rulock);
  19. void* writeThread(void* p)f pthreadmutexlock(8indexPutex); int index . getIndex(); pthread_mutex_uniock(8indexPutex); pthreadrwlockwriock(8rulock); printrCrloTis %i aanncyc\e, index); usieep(500); soneVal = index * 100; usieep(500); printfCrloTix Xi sanepumn sannc\e, index); pthread_rulock_unlock(8rwlock);
  20. lnt RN)) pthread_mutexlock(gmutex); If (currentPas 0) f pthreadmutex_unlock(gmutex); return -1;
  21. 1 currentPos--; pthreadmutex_unlock(gmutex); return stack[currentPas];
  22. 1 int index 0; pthreadmutex t A = PTHREAP_MUTEX_INITIALIZER; int getIndexaf index,: return index;
  23. 1 void* threadFunc(void* p) f pthread_mutex lock(81); int value = gitIndex(); pthread_mutex_unlock(81); int currentVal . value; for (int i . 1; 1 < R; ++l)) if (push(currentVal) 1= -1) printfnlogananno 56-1 In., currentVal); else printf(.Cfes mammAl\n.); currentVal . value *pow(10,1) currentVal;
  24. 1 ONE (int t = 0; U< 6; Ivi) tf ((currentVal . pap()) , -1) printfClimpanenno It In•, currentVal); else printf(.Cfes noposnini\e);
  25. int main() f int THREAD_COUNT = 3; pthread_t threads[THREADCOUNT]; createStack(THREAD_COUNT * 5);
  26. for (int I = 1; A<= THREAD_COUNT; ITS) f pthread_create(gthreads[i - 1], Nutt, threadFunc, NULL); 1 for (int I . 0; I < THREAD_COUNT; *al) pthread_jain(threads[i] , NULL);
  27. return 0;
  28. C Illmpmna .6,6068 ■ PIT. 5, CT. 1 ■BC
  29.  
  30. 1 int main() f pthread_t Ni. ti, t3; pthread create(8t1, Nun, readThread, NULL); pthreadcreate(8tZ, Nun, writeThread, NULL); pthread:create(8t3, Nun, readThread, NULL); pthreadjoin(t1, NULL); pthreadjoin(tZ, NULL); pthreadjoin(t3, NULL);
  31. return 0;
  32. C Ta6jrnsuil 8 ■ PRA. 37, CT. 19 BC
  33.  
  34. C Illmpmna .6,6068 ■ PIT. 5, CT. 1 ■BC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement