Linux programming - Creation and displaying threads.

Nikhil_19

Commendable
Aug 7, 2016
2
0
1,510
Problem: 1.Create four threads.
2. Thread 1 should display - "Hi im thread 1" and go to sleep for 1 second. This should loop 100times.
3. After 100 times of looping, thread 2 should display,sleep and loop.
4. All four threads must do the same.

Solutionn : I have tried writing a program and im new at this. So it would be a great help if you review this and tell me if its right.
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

void* thread1()
{
for(i=0;i<=100;i++)
{
printf("Hi,I'm thread 1\n");
sleep(1);
}
}

void* thread2()
{
for(i=0;i<=100;i++)
{
printf("Hi,I'm thread 2\n");
sleep(1)
}
}

//similarly thread3 and thread4

void main()
{
pthread_t tid1,tid2,tid3,tid4;
pthread_create(&tid1,NULL,&thread1,NULL);
pthread_create(&tid2,NULL,&thread2,NULL);
pthread_create(&tid3,NULL,&thread3,NULL);
pthread_create(&tid4,NULL,&thread4,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_join(tid4,NULL);
return 0;
}

Is this right ? :)

Thanks
 
Solution

dexxterlab97

Respectable
Jul 4, 2016
208
0
1,910
Why not compile and test it out.
Open a terminal in linux, navigate to where you file is and type gcc filename.c to compile it
Run it with ./a.out
This is forum is more hardware than software so you could try out stackexchange
 

Nikhil_19

Commendable
Aug 7, 2016
2
0
1,510
@dexxterlab97 :
I have linux on virtual box and apparently it does not have the necessary library files for executing the program. And im on a deadline, so i cant afford to download it right away. :(
 

Pinhedd

Distinguished
Moderator


You're getting there, but that's not quite right.

First things first, get into the habbit of using proper program entry and return. Your main entry point should never be a void return as this can lead to non-deterministic program exit codes on some compilers. Always use one of the following

C++:
int main(int argc, char** argv){}
int main(int argc, char*[] argv){}
int main(void){}

The first two are equivalent.

Void returns are fine on non-main functions.

Functions that do not take any parameters should have (void) rather than (). This is a major difference between C and C++. In C, the absence of any parameters means that the number of parameters is indeterminate and compile time checking will not be performed but in C++ it means that there are no parameters, (void) means the same in each language.

Second, never create pthread_t objects on the stack. What you've done is logically sound in this case, but it's a bad habbit to get into. Allocate them statically or on the heap via malloc().

Third, your thread functions are returning void pointers but have no explicit return value. Each thread function should have at the end

C++:
return NULL;

most modern compilers will complain about this, but older ones will simply return whatever happens to be in the ABI's return register.

Void functions should simply have

C++:
return;
 
Solution