0

hello im new to linux programming and i am experiencing a bit of diffuclty with pthreads...basically the pthread_create() function
in my code below i tend to pass an integer variable as the argument for my runner() function but i get a warning and an error..i have tried several ways of passing this argument but im not very good with pointers...a bit of help in my code would be appreciated..

#include<pthread.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<semaphore.h>
#include<unistd.h>

pthread_t t1,t2;
pthread_attr_t tattr;
int counter;
sem_t mutex;

void runner(void *arg)
{
    printf("Thread received %i",*arg);
    int i=0;
    for(i=0;i<5;i++)
    {
        sem_wait(&mutex);
        printf("Thread %i says %i",*((int *)arg),counter);
        counter++;
        sem_post(&mutex);
        sleep(3);
    }
}

int main()
{
    int r;
    printf("hi");
    r=sem_init(&mutex,0,1);
    r=pthread_attr_init(&tattr);
    int t=1;
    r=pthread_create(&t1,&tattr,runner,(void *)&t);
    t=2;
    r=pthread_create(&t2,&tattr,runner,(void *)&t);
    printf("Threads initialised");
    r=pthread_join(t1,NULL);
    r=pthread_join(t2,NULL);
    r=sem_destroy(&mutex);
    return 0;
}
3
Contributors
4
Replies
7
Views
6 Years
Discussion Span
Last Post by gerard4143
Featured Replies
  • I cleaned your code up a little...well a lot [code] #include<pthread.h> #include<stdio.h> #include<sys/types.h> #include<semaphore.h> #include<unistd.h> pthread_t t1,t2; pthread_attr_t tattr; int counter; sem_t mutex; void* runner(void *arg) { int i = 0; printf("Thread received %i\n", *(int*)arg); for(i = 0; i < 5; i++) { sem_wait(&mutex); printf("Thread %i says %i\n",*((int *)arg),counter); counter++; … Read More

1

I cleaned your code up a little...well a lot

#include<pthread.h>
#include<stdio.h>
#include<sys/types.h>
#include<semaphore.h>
#include<unistd.h>

pthread_t t1,t2;
pthread_attr_t tattr;
int counter;
sem_t mutex;

void* runner(void *arg)
{
	int i = 0;

	printf("Thread received %i\n", *(int*)arg);

	for(i = 0; i < 5; i++)
	{
		sem_wait(&mutex);
		printf("Thread %i says %i\n",*((int *)arg),counter);
		counter++;
		sem_post(&mutex);
		sleep(1);
	}
	return (void*)NULL;
}

int main()
{
	int r;
	int t = 1;

	printf("hi");
	r = sem_init(&mutex,0,1);
	r = pthread_attr_init(&tattr);

	r = pthread_create(&t1,&tattr,runner,(void *)&t);

	t = 2;

	r = pthread_create(&t2,&tattr,runner,(void *)&t);

	printf("Threads initialised");
	r = pthread_join(t1,NULL);
	r = pthread_join(t2,NULL);
	r = sem_destroy(&mutex);

	return 0;
}

P.S.

This is a pet peeve of mine

r=sem_destroy(&mutex);

It makes the code impossible to read, please consider a more readable format like below.

r = sem_destroy(&mutex);

compile line:
gcc test.c -Wall -ansi -pedantic -o test -pthread

Edited by gerard4143: n/a

0

i understood my mistakes...thanks gerrard...although in your code...
when starting a new thread the value of t passed is always 2 and never 1...

where it should say Thread 1 started..it says Thread received 2 Thread received 2
instead of Thread received 1 Thread received 2

how is that ?

0

i understood my mistakes...thanks gerrard...although in your code...
when starting a new thread the value of t passed is always 2 and never 1...

where it should say Thread 1 started..it says Thread received 2 Thread received 2
instead of Thread received 1 Thread received 2

how is that ?

Your passing the address of t not its value, so when you change t to two its reflected in both threads.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.