Lab5
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
#include <stdio.h>
|
||||
#include <wait.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
pthread_t phread_self(void);
|
||||
|
||||
void * start_routine(void * arg);
|
||||
int pthread_create(pthread_t * thread, const pthread_attr_t * attr,
|
||||
void *(start_routine)(void *), void * arg);
|
||||
void pthread_exit(void *retval);
|
||||
*/
|
||||
|
||||
int globalVar = 5;
|
||||
|
||||
void * start_routine(void * arg) {
|
||||
//int pid = pthread_self();
|
||||
//printf("%d\n",pid);
|
||||
globalVar += 1;
|
||||
printf("%d\n",globalVar);
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
int val1 = 5;
|
||||
int val2 = 11;
|
||||
|
||||
pthread_t threads[1000];
|
||||
|
||||
pthread_t thread1;
|
||||
pthread_t thread2;
|
||||
|
||||
for (int i = 0; i < 1000; ++i){
|
||||
pthread_create(&threads[i], NULL, &start_routine, NULL);
|
||||
pthread_join(threads[i], NULL);
|
||||
}
|
||||
|
||||
pthread_create(&thread1, NULL, &start_routine, NULL);
|
||||
pthread_join(thread1, NULL);
|
||||
pthread_create(&thread2, NULL, &start_routine, NULL);
|
||||
pthread_join(thread2, NULL);
|
||||
|
||||
|
||||
//printf("%d\n",globalVar);
|
||||
|
||||
printf("Program started!\n");
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user