elad2109 wrote:
However, when I move function display_prompt() to UI.h
You move...What?
You should move just the function prototype to the header file.
However, with the
main
you provided, the output is (almost) unpredictable,
since the main
function may exit well before the thread has a chance to produce its output.
Try this code:
void* display_prompt(void *id);
#include <stdio.h>
#include "ui.h"
void* display_prompt(void *id){
int i;
for (i=0;i<10; i++)
{
printf("Secondary thread %d\n", i);
sleep(2);
}
return NULL;
}
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include "ui.h"
int main(int argc, char** argv) {
int i;
printf("Hello\n");
pthread_t *thread = (pthread_t*) malloc (sizeof(pthread_t));
if (!thread ) return -1;
pthread_create(thread, NULL, display_prompt, NULL);
for (i=0; i<10; i++)
{
printf("Main Thread %d\n", i);
sleep(1);
}
free(thread);
return 1;
}
:)