in function threadx , what I want to do, is instead of printing the value of i, I want to pass the output to another thread.
so, right now it prints 80 81 82 and so on
I want to pass a string 80 81 82 83 84 and have another thread print it. My question is how to save the values and pass them
thank you
#include <stdio.h>
#include <pthread.h>
#define N 2
void *thread(void *vargp);
void *threadx(void *vargp);
char **ptr;
int glob1;
int glob2;
int main()
{
int i;
pthread_t tid[2];
char *msgs[N] = {
"Hello from foo",
"Hello from bar"
};
printf("Parent thread started with PID= %d and parent PID %d\n", getpid(), getppid());
ptr = msgs;
pthread_create(&tid[0], NULL, thread, (void *)1);
printf(" 1st thread started with PID= %d and parent PID %d\n", getpid(), getppid());
glob1 = 80;
pthread_create(&tid[1], NULL, threadx, (void *)2 );
printf("2nd thread started with PID= %d and parent PID %d\n", getpid(), getppid());
pthread_join(tid[1], NULL);
pthread_join(tid[0], NULL);
}
void *thread(void *vargp)
{
int myid = (int)vargp;
static int cnt = 0;
printf("[%d]: %s (cnt=%d) with PID= %d and parent PID %d\n", myid, ptr[myid], ++cnt, getpid(), getppid());
if (myid = 1)
{
int i=cnt;
for (;i <10 ;i=i+1)
{
printf("[%d] %d\n",myid, i);
sleep(cnt);
}
return NULL;
}
}
void *threadx(void *vargp )
{
int myid = (int)vargp;
static int cnt = 0;
printf("xxxx[%d]: %s (cnt=%d) with PID= %d and parent PID %d\n", myid, ptr[myid], ++cnt, getpid(), getppid());
int i;
for (i=glob1;i <100 ;i=i+1)
{
printf("[%d] %d\n",myid, i);
sleep(cnt);
}
return NULL;
}
Aucun commentaire:
Enregistrer un commentaire