Skip to content
Snippets Groups Projects
Commit 3103fac6 authored by JieunYoon's avatar JieunYoon
Browse files

Update README.md

parent 6d80c117
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <sys/syscall.h>
void * thread_function(void *);
int Counter=0;
#ifndef NOMUTEX
.... mutex_id;
#endif
int main(void)
{
pthread_t tid1, tid2;
void *thread_result;
#ifndef NOMUTEX
if(________(&mutex_id, NULL) != 0){
perror("pthread_mutex_init");
exit(errno);
}
#endif
if(_______(&tid1, NULL, thread_function, "thread1")!=0) {
perror("pthread_create");
exit(1);
}
if(_______(&tid2, NULL, thread_function, "thread2")!=0) {
perror("pthread_create");
exit(1);
}
if(_______(tid1, &thread_result)!=0) {
perror("pthread_join");
exit(1);
}
if(_______(tid2, &thread_result)!=0) {
perror("pthread_join");
exit(1);
}
#ifndef NOMUTEX
___________(&mutex_id);
#endif
printf(" thread result : %s\n", (char *) thread_result);
return 0;
}
void * thread_function(void * arg)
{
int temp;
int i, j;
char buffer[80];
printf("thread_function called\n");
for(i=0; i<8; i++) {
#ifndef NOMUTEX
_________(&mutex_id);
#endif
sprintf(buffer, "%s: CountRelay: from %d to ", (char*)arg, Counter);
write(1, buffer, strlen(buffer));
temp=Counter;
temp = temp+1;
// delay
for(j=0; j<5000000; j++);
Counter=temp;
sprintf(buffer, "%d\n", Counter);
write(1, buffer, strlen(buffer));
#ifndef NOMUTEX
__________(&mutex_id);
#endif
}
// getchar();
_______("thread end");
}
......@@ -81,8 +81,11 @@ make
89
73
Main start!
====sthread1 start====
합 : 1837, 평균 : 61.233334
====sthread1 end====
Main end!
====sthread2 start====
1등 : 100
2등 : 99
3등 : 96
......@@ -113,7 +116,7 @@ Main end!
28등 : 9
29등 : 4
30등 : 1
====sthread2 end====
```
sthread2가 sthread1보다 연산량이 많아 늦게 끝나는 것을 볼 수 있다.
### 2.mymutexthread.c
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment