Commits

Jay Barra  committed eab66f6

pthread example

  • Participants
  • Parent commits 4c349b9

Comments (0)

Files changed (1)

File pthread/main.cpp

+#include <stdio.h>
+#include <stdlib.h>
 #include <pthread.h>
-void start();
+
+void* runnable(void* param);
+
+#define ROWS 2
+#define COLS 2
+
+int A[ROWS][COLS] = { { 0, 5 }, { 3, 1 } };
+int B[ROWS][COLS] = { { 4, 1 }, {-5, -2 } };
+int RESULTS[ROWS][COLS];
+
+struct cell {
+  int row;
+  int col;
+};
+
 int main(int argc, char** argv)
 {
-	pthread_t tid;
-	int* status;
+  int iTCount = 0;
+  for (int i = 0; i < ROWS; i++) {
+    int* row = (int*)malloc(COLS * sizeof(int)); 
+    for (int col = 0; col < COLS; col++) {
+      struct cell* data = (struct cell*)malloc(sizeof(struct cell));
+      data->row = i;
+      data->col = col;
 
-	pthread_create(&tid, NULL, start, NULL);
-	pthread_join(tid, (void**)&status);
+      pthread_t tid;
+      pthread_create(&tid, NULL, runnable, data);
+      pthread_join(tid, NULL);
+      
+      iTCount++;
+    }
+  }
 
-	return 0;
+  for (int i = 0; i < ROWS; i++) {
+    for (int j = 0; j < COLS; j++) {
+      printf("%d ", RESULTS[i][j]);
+    }
+    printf("\n");
+  }
 }
 
-void start()
+void* runnable(void* param)
 {
-	int errorcode = 0;
-	pthread_exit(&errorcode);
+  struct cell* data = (struct cell*)param;
+  int sum = 0;
+
+  for (int n = 0; n < ROWS; n++) {
+    sum += A[data->row][data->col] * B[data->row][data->col];
+  }  
+  RESULTS[data->row][data->col] = sum;
+  pthread_exit(0);
 }
+