Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ((include <pthread.h> atnclude <math.h>
- 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);
- 1 int push(int value) pthread_mutex lock(8mutex); tf (currentPai Px size) f pthread_mutex unlock(8mutex); return -1; —
- stack[currentPos**1 value; pthread_mutex_unlock(8mutex);
- C Ta6ynnail 8 x S. Cx 1 x
- lnt RN)) pthread_mutexlock(gmutex); If (currentPas 0) f pthreadmutex_unlock(gmutex); return -1;
- 1 currentPos--; pthreadmutex_unlock(gmutex); return stack[currentPas];
- 1 int index 0; pthreadmutex t A = PTHREAP_MUTEX_INITIALIZER; int getIndexaf index,: return index;
- 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;
- 1 ONE (int t = 0; U< 6; Ivi) tf ((currentVal . pap()) , -1) printfClimpanenno It In•, currentVal); else printf(.Cfes noposnini\e);
- int main() f int THREAD_COUNT = 3; pthread_t threads[THREADCOUNT]; createStack(THREAD_COUNT * 5);
- 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);
- return 0;
- Sinde <stdlib.h. ((include <unistd.h> ((include <uthread.h>
- 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
- 3( 3-7). UNOIt
- 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);
- 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);
- lnt RN)) pthread_mutexlock(gmutex); If (currentPas 0) f pthreadmutex_unlock(gmutex); return -1;
- 1 currentPos--; pthreadmutex_unlock(gmutex); return stack[currentPas];
- 1 int index 0; pthreadmutex t A = PTHREAP_MUTEX_INITIALIZER; int getIndexaf index,: return index;
- 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;
- 1 ONE (int t = 0; U< 6; Ivi) tf ((currentVal . pap()) , -1) printfClimpanenno It In•, currentVal); else printf(.Cfes noposnini\e);
- int main() f int THREAD_COUNT = 3; pthread_t threads[THREADCOUNT]; createStack(THREAD_COUNT * 5);
- 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);
- return 0;
- C Illmpmna .6,6068 ■ PIT. 5, CT. 1 ■BC
- 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);
- return 0;
- C Ta6jrnsuil 8 ■ PRA. 37, CT. 19 BC
- C Illmpmna .6,6068 ■ PIT. 5, CT. 1 ■BC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement