How create thread from float function?

In my class I have a float function that should work always and return value to use it in other place, but I can't create thread for this float function
1
2
3
4
5
6
7
8
9
10
11
12
13
 pthread_create(&th, NULL, getload, NULL);
	 pthread_join(th, NULL);
float data::getload(void *arg) {
       float util;
    while(condition)
     {

      sleep(1);
                }
 
    return util;

}



Last edited on
Leaving aside for the moment that pthread knows nothing about classes, your getload() would need to be a static function to be callable from pthread_create.

1
2
3
4
5
6
7
pthread_create(&th, NULL, getload, NULL);
// wait around for a while
void *result;
pthread_join(th, &result);
float *answer = reinterpret_cast<float*> (result);
// use the answer
delete answer;


1
2
3
4
5
6
7
8
9
10
11
void *data::getload(void *arg) {
    float util;
    while(condition)
    {
      sleep(1);
    }
 
    float *result = new float;
    *result = util;
    return reinterpret_cast<void*> (result);
}


If you can, use https://www.cplusplus.com/reference/thread/ instead.
When the program received to this pthread_create(&th, NULL, getload, NULL); line
thread not running
Last edited on
Works for me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <iomanip>
#include <pthread.h>
using namespace std;

class data {
public:
  static void *getload(void* arg);
};

void *data::getload(void *arg) {
  cout << "In getload" << endl;
  float *result = new float;
  *result = 42;
  return reinterpret_cast<void*> (result);
}

int main ( ) {
  pthread_t th;
  pthread_create(&th, NULL, data::getload, NULL);
  // wait around for a while
  void *result;
  pthread_join(th, &result);
  float *answer = reinterpret_cast<float*> (result);
  cout << "Got " << *answer << endl;
  delete answer;
}

$ g++ foo.cpp -pthread
$ ./a.out 
In getload
Got 42

When I define function in static, I get an error from line 3 : cannot call member function ‘bool data::get_times(size_t&, size_t&)’ without object

1
2
3
4
5
6
7
8
9
void* data::getload(void *arg) {

	for (size_t idle_time, total_time; get_times(idle_time, total_time); sleep(1)) { //while(true)
	        
                }
            float *result = new float;
            *result = util;
           return reinterpret_cast<void*> (result);
}
Last edited on
Well typically, you would use the arg for the thread to pass in the instance you want to use with your thread.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <iomanip>
#include <pthread.h>
using namespace std;

class data {
public:
  static void *getload(void* arg);
  bool get_times(size_t idle_time, size_t total_time) {
    return true;
  }
};

void *data::getload(void *arg) {
  data *ptr = reinterpret_cast<data*> (arg);
  cout << "In getload" << endl;
  bool foo = ptr->get_times(0,0);
  float *result = new float;

  *result = 42;
  return reinterpret_cast<void*> (result);
}

int main ( ) {
  pthread_t th;
  data  something;
  pthread_create(&th, NULL, data::getload, &something);
  // wait around for a while
  void *result;
  pthread_join(th, &result);
  float *answer = reinterpret_cast<float*> (result);
  cout << "Got " << *answer << endl;
  delete answer;
}

@salem c, Thanks for the help but I still have a problem, now the issue is thread is run but does not get sleep and the result is not returned, I think because of for/while(true) I should use pthread_detach(th);


Last edited on
Topic archived. No new replies allowed.