C Programming - Multithreading using pthreads

I want to understand how multithreading in C works. I am trying to convert the following code below to a multithreaded version in C programming language, using pthread.h, getopt function to take user input and create specified multithreads, in the C  library. Thank you in advance!
If anyone can convert it for me or provides a simple example/ advice for using getopt with pthreads please post!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> //Header file for sleep(). man 3 sleep for details. 
#include <pthread.h> 



/* Return 1 if 'i'th bit of 'n' is 1; 0 otherwise */

#define EXTRACT_BIT(n,i) ((n&(1<

int check_circuit (int z) {
// check_circuit check z is the correct solution to the circuit

  int v[16];        /* Each element is a bit of z; array to hold 16 bits of the 65,536 possible combinations*/

  int i; //variable to create 16 bit array to represent decimals, send it to i parameter is z so i =z

  for (i = 0; i < 16; i++) v[i] = EXTRACT_BIT(z,i); //this is the logical circuit assume it is true

  if ((v[0] || v[1]) && (!v[1] || !v[3]) && (v[2] || v[3])

      && (!v[3] || !v[4]) && (v[4] || !v[5])

      && (v[5] || !v[6]) && (v[5] || v[6])

      && (v[6] || !v[15]) && (v[7] || !v[8])

      && (!v[7] || !v[13]) && (v[8] || v[9])

      && (v[8] || !v[9]) && (!v[9] || !v[10])

      && (v[9] || v[11]) && (v[10] || v[11])

      && (v[12] || v[13]) && (v[13] || !v[14])

      && (v[14] || v[15])) {

    printf ("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d\n",

      v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9], //print out of the 65,536 possible solutions that are true which is 9 solutions will not change

      v[10],v[11],v[12],v[13],v[14],v[15]);

    return 1; //return 1 if solution is true (9)

  } else return 0; //or return 0 if solution is not true

}

int main (int argc, char *argv[])

{

/*

solution algo: create a getopt function to take in CMD ./thdcircuit 3 ; where the user specifies the # of multithreads,

take user input and create specified multithreads, divide the calculations based on user defined multithreads, and divide / load balance

the calculations of 65,356 possibilies amoung the user defined multithreads, execution time should decrease as multithreads increase

*/

  int count, i;

  struct timeval t1, t2; //struc from #include

  gettimeofday(&t1, 0);//struc from #include ;  get time before t1 (before the work)

  count = 0;

  for (i = 0; i < 65536; i++)//for every number you send it to the function if it is true, it adds it to the count

    count += check_circuit (i); //send it to the array tto check if it is true or false

  gettimeofday(&t2, 0);//get time after t2 (after the work)

  printf ("Execution time %fs\n", (t2.tv_sec-t1.tv_sec)+(t2.tv_usec-t1.tv_usec)*1e-6);//execution time to compare runtimes in multi thread and monothread

  /*//execution time is finding the difference of t1 from t2

execution time should decrease as user specifies more multithreads

execution run without multithread config will decrease because computer is set to run at different times

  */

  printf ("There are %d solutions\n", count);

  return 0;

}
Last edited on
It's not clear that anything in this program is parallel.

It's also not clear how attempting it will teach you anything about multithreading.

It's not really fair to dishonestly try to get others to do your work for you. If you want help, just ask for it.
i am asking for help, just a simple example it doesnt have to be the complete program about multithreading
Step 1, get the work you need to do into a separate function.
1
2
3
4
5
6
7
8
9
10
11
12
13
int check_circuit(int n) {
  printf("C=%d\n", n);
  return 1;
}


int main ( ) {
  int count = 0;
  for ( int i = 0; i < 20; i++) {
    count += check_circuit (i);
  }
  printf("Final Answer=%d\n",count);
}


Step 2, partition the problem as you would like it to be threaded.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main ( ) {
  int count = 0;
  int min = 0, max = 20;
  int n = 3;  // what will become number of threads
  int partition = max / n;
  for ( int t = 0 ; t < n ; t++ ) {
    int first = t * partition;
    int last = (t+1) * partition;
    if ( t == n-1 ) last = max;
    for ( int i = first; i < last; i++) {
      count += check_circuit (i);
    }
  }
  printf("Final Answer=%d\n",count);
}


Step 3, refactor as a single function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// as a function
int worker(int first, int last) {
  int count = 0;
  for ( int i = first; i < last; i++) {
    count += check_circuit (i);
  }
  return count;
}

int main ( ) {
  int count = 0;
  int min = 0, max = 20;
  int n = 3;  // what will become number of threads
  int partition = max / n;
  for ( int t = 0 ; t < n ; t++ ) {
    int first = t * partition;
    int last = (t+1) * partition;
    if ( t == n-1 ) last = max;
    count += worker(first,last);
  }
  printf("Final Answer=%d\n",count);
}


Step 4, make it threaded.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
struct thread_params {
  int first;
  int last;
};
struct thread_result {
  int count;
};
void *worker_thread(void *p) {
  struct thread_params *params = p;
  struct thread_result *result = malloc(sizeof(*result));
  result->count = worker(params->first,params->last);
  free(params);
  return result;
}
pthread_t thread_launcher(int first, int last) {
  struct thread_params *params = malloc(sizeof(*params));
  params->first = first;
  params->last = last;
  pthread_t tid = 0;
  if ( pthread_create(&tid,NULL,worker_thread,params) ) {
  }
  return tid;
}
int thread_joiner(pthread_t tids[], int n) {
  int count = 0;
  for ( int t = 0 ; t < n ; t++ ) {
    void *resptr;
    int r = pthread_join(tids[t],&resptr);
    if ( r == 0 ) {
      struct thread_result *result = resptr;
      count += result->count;
      free(result);
    } else {
      perror("Bad?");
      printf("R=%d\n",r);
    }
  }
  return count;
}

int main4 ( ) {
  int count = 0;
  int min = 0, max = 20;
  int n = 3;  // what will become number of threads
  pthread_t tids[n];
  int partition = max / n;
  for ( int t = 0 ; t < n ; t++ ) {
    int first = t * partition;
    int last = (t+1) * partition;
    if ( t == n-1 ) last = max;
    tids[t] = thread_launcher(first,last);
  }
  count = thread_joiner(tids,n);
  printf("Final Answer=%d\n",count);
}


Topic archived. No new replies allowed.