I practice multithreading.
I create two posix streams that display text on the screen (an endless loop), but it only seems to be the first time the stream starts. I change the program without a loop, first prints the threads, the next - the second thread. It seems that my thread is not parallel, the first thread should end before the start of the second recording. How can I make them parallel?
Thank,
hdr.h
#ifndef HDR_HDR_H_
#define HDR_HDR_H_
#define HDR_HDR_H_
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#endif
multithread01.c
#include "../hdr/myfunc.h"
pthread_mutex_t lock;
int main(int argc, char **argv)
{
pthread_t tid01;
pthread_t tid02;
void * status01;
void * status02;
pthread_create(&tid01, NULL, PrintOut01(), NULL);
pthread_create(&tid02, NULL, PrintOut02(), NULL);
pthread_join(&tid01, &status01);
pthread_join(&tid02, &status02);
return 0;
}
myfunc.h
#ifndef HDR_MYFUNC_H_
#define HDR_MYFUNC_H_
#include "../hdr/hdr.h"
void * PrintOut01 (void);
void * PrintOut02 (void);
#endif
myfunc.c
#include "../hdr/hdr.h"
extern pthread_mutex_t lock;
void * PrintOut01 ()
{
while (1)
{
pthread_mutex_lock(&lock);
printf ("This is thread 01\n");
pthread_mutex_unlock(&lock);
}
}
void * PrintOut02 ()
{
while (1)
{
pthread_mutex_lock(&lock);
printf ("This is thread 02\n");
pthread_mutex_unlock(&lock);
}
}
source
share