-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed threads interleave to create two threads that print a character
- Loading branch information
Showing
1 changed file
with
23 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |