So I just started programming for fun and looked into pthread since the topic interests me.
To understand the basics I tried to create a simple program that creates 2 threads and executes a function in each of them.
The Output should be:
Thread1
Thread2
instead I get:
Thread1
Thread1
Thread2
Thread2
Why does it execute the function twice?
Thanks for your time
To understand the basics I tried to create a simple program that creates 2 threads and executes a function in each of them.
C:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *function1(void *printf);
void *function2(void *printf);
int main()
{
pthread_t thread1;
pthread_t thread2;
pthread_create(&thread1, NULL, function1, NULL);
function1(printf);
pthread_join(thread1, NULL);
pthread_create(&thread2, NULL, function2, NULL);
function2(printf);
pthread_join(thread2, NULL);
return 0;
}
void *function1(void *print)
{
printf("Thread1\n");
return NULL;
}
void *function2(void *print)
{
printf("Thread2\n");
return NULL;
}
The Output should be:
Thread1
Thread2
instead I get:
Thread1
Thread1
Thread2
Thread2
Why does it execute the function twice?
Thanks for your time