From be1509dbb81ccace9c639269bfe20bed8cce64ae Mon Sep 17 00:00:00 2001
From: Sosoo <pung725@ajou.ac.kr>
Date: Fri, 14 Aug 2020 16:02:39 +0900
Subject: [PATCH] before

---
 mutex/1.mytwothread.c      | 62 +++++++++++++++++++++++++++
 mutex/2.mymutexthread_st.c | 87 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 149 insertions(+)
 create mode 100644 mutex/1.mytwothread.c
 create mode 100644 mutex/2.mymutexthread_st.c

diff --git a/mutex/1.mytwothread.c b/mutex/1.mytwothread.c
new file mode 100644
index 0000000..b7583ac
--- /dev/null
+++ b/mutex/1.mytwothread.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <pthread.h>
+
+void * thread_function(void *);
+
+int Counter=0;
+
+int main(void)
+{
+	pthread_t tid1, tid2;
+	void *thread_result;
+
+	if(pthread_create(&tid1, NULL, thread_function,  "thread1")!=0) {
+		perror("pthread_create");
+		exit(1);
+	}
+	if(pthread_create(&tid2, NULL, thread_function, "thread2")!=0) {
+		perror("pthread_create");
+		exit(1);
+	}
+
+	if(pthread_join(tid1, &thread_result)!=0) {
+		perror("pthread_join");
+		exit(1);
+	}
+	if(pthread_join(tid2, &thread_result)!=0) {
+		perror("pthread_join");
+		exit(1);
+	}
+
+	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<20; i++) {
+		sprintf(buffer, "%s : Counter : from %d to ", (char*)arg, Counter);
+		write(1, buffer, strlen(buffer));
+
+		temp=Counter;
+		temp = temp+1;
+
+// delay
+		for(j=0; j<500000; j++);
+
+		Counter=temp;
+
+		sprintf(buffer, " %d\n", Counter);
+		write(1, buffer, strlen(buffer));
+
+	}
+	pthread_exit("thread end");
+}
diff --git a/mutex/2.mymutexthread_st.c b/mutex/2.mymutexthread_st.c
new file mode 100644
index 0000000..c1c2143
--- /dev/null
+++ b/mutex/2.mymutexthread_st.c
@@ -0,0 +1,87 @@
+#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
+pthread_mutex_t mutex_id;
+#endif
+
+int main(void)
+{
+	pthread_t tid1, tid2;
+	void *thread_result;
+
+#ifndef NOMUTEX
+	if(pthread_mutex_init(&mutex_id, NULL) != 0){
+		perror("pthread_mutex_init");
+		exit(errno);
+	}
+#endif
+
+	if(pthread_create(&tid1, NULL, thread_function, "thread1")!=0) {
+		perror("pthread_create");
+		exit(1);
+	}
+	
+	if(pthread_create(&tid2, NULL, thread_function, "thread2")!=0) {
+		perror("pthread_create");
+		exit(1);
+	}
+	
+	if(pthread_join(tid1, &thread_result)!=0) {
+		perror("pthread_join");
+		exit(1);
+	}
+	if(pthread_join(tid2, &thread_result)!=0) {
+		perror("pthread_join");
+		exit(1);
+	}
+	
+#ifndef NOMUTEX
+	pthread_mutex_lock(&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
+		pthread_mutex_lock(&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
+		pthread_mutex_unlock(&mutex_id);
+#endif
+	}
+	// getchar();
+	pthread_exit("thread end");
+}
-- 
GitLab