PDA

View Full Version : threaded program doesn't work?



OcTO_VIII
12-11-2003, 11:59 AM
I created the following multi-threaded program in linux:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

//using namespace std;

void *thread_funk(void *start);

void *thread_funk(void *start)
{
int *value1 = (int *)start;
//while(*value1 < 4)
while(1) {
if (*value1 == 3)
{
printf("Cifra je %d\n", *value1);
pthread_exit(NULL);
}
}

}

int main(int argc, char *argv[])
{
pthread_t thread1;
int *value = (int *)malloc(sizeof(int));
*value = 0;
// Create new thread.
pthread_create(&thread1, NULL, (void *)&thread_funk, (void *)value);
while(*value < 5)
{
//sleep(5);
*value++;
}
free(value);
exit(0);
}
It compiles perfectly bit it would not work! Why?
Please help.

OcTO_VIII
12-11-2003, 12:37 PM
Ok. Found it and fix it and it works now perfectly :)