Skip to content

Commit

Permalink
Changed threads interleave to create two threads that print a character
Browse files Browse the repository at this point in the history
  • Loading branch information
jjolly committed Feb 16, 2018
1 parent 98d8db4 commit c2e5083
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions threads/interleave/interleave.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
#include <stdio.h>
#include <pthread.h>

struct char_print_params {
char print_char;
int number_to_print;
};

/* the thread function */
void *printxs(void *unused) {
while(1)
fputc('x', stderr);
void *char_print(void *params) {
struct char_print_params *p;
p = (struct char_print_params *) params;

int i;
for (i = 0; i < p->number_to_print; i++)
fputc(p->print_char, stderr);

return NULL;
}

int main(int argc, char *argv[]) {
/* the thread handle */
pthread_t x_thread;
pthread_t thread1_handle;
pthread_t thread2_handle;

struct char_print_params thread1_args;
struct char_print_params thread2_args;

pthread_create(&x_thread, NULL, &printxs, NULL);
thread1_args.print_char = 'x';
thread1_args.number_to_print = 3000;
pthread_create(&thread1_handle, NULL, char_print, &thread1_args);

while(1)
fputc('o', stderr);
thread2_args.print_char = 'y';
thread2_args.number_to_print = 5000;
pthread_create(&thread2_handle, NULL, char_print, &thread2_args);

return 0;
}

0 comments on commit c2e5083

Please sign in to comment.