36 lines
559 B
C++
36 lines
559 B
C++
|
#include <pthread.h>
|
||
|
#include <stdio.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
int square (int x)
|
||
|
{
|
||
|
return (x * x);
|
||
|
}
|
||
|
|
||
|
void *my_thread(void *arg)
|
||
|
{
|
||
|
int x = 0;
|
||
|
int sqr;
|
||
|
|
||
|
while (true) {
|
||
|
sqr = square(5);
|
||
|
sleep(1);
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
pthread_t thread_id;
|
||
|
if (pthread_create(&thread_id, NULL, my_thread, NULL)) {
|
||
|
fprintf(stderr, "Error creating thread\n");
|
||
|
return 1;
|
||
|
}
|
||
|
if (pthread_join(thread_id, NULL)) {
|
||
|
fprintf(stderr, "Error joining thread\n");
|
||
|
return 2;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|