Callback function of pthread_create not getting called? - c++

#include <iostream>
#include <pthread.h>
using namespace std;
class Base
{
private:
public:
void threadCall1( void * value)
{
cout<<"inside threadCall1"<<endl;
cout<<"Value is"<<(int *)value<<endl;
}
protected:
};
class Derived
{
private:
public:
void threadCall2 ();
protected:
};
void *passValue(void * q)
{
cout<<"inside passValue"<<endl;
Base *b = new Base();
b->threadCall1(q);
cout<<"after threadCall1"<<endl;
Derived *d;
cout<<(int *)q<<endl;
pthread_exit(NULL);
}
void Derived::threadCall2()
{
cout<<"inside threadCall2"<<endl;
}
int main ()
{
int k = 2;
pthread_t t1;
cout<<"inside main"<<endl;
pthread_create(&t1,NULL,&passValue,(void *)k);
cout<<"after pthread_create"<<endl;
return 0;
}
Output:
inside main
after pthread_create
Everything seems fine but don't know why passValue is not getting called and I get the above output but other logs namely inside passValue is missing

Your main terminates early and kills the thread immediately after creation. Add this line before return 0; of the main:
pthread_join(t1, NULL);
This will make the main thread wait for (blocks) for the termination of t1.

Related

C++ class function pointer

I have a request for function pointer by C++. below is the sample what I need:
in API file:
class MyClass {
public:
void function1();
void function2();
void function3();
void function4();
};
in main file:
MyClass globalglass;
void global_function_call(???)// <---- how to do declaration of argument???
{
//Do *function
}
int main()
{
global_function_call(&globalglass.function1()); // <---- pseudocode, I need to use global class
global_function_call(&globalglass.function2());
global_function_call(&globalglass.function3());
global_function_call(&globalglass.function4());
return 1;
}
I have no idea to do declaration...
To do what you are asking for, you can use a pointer-to-member-method, eg:
MyClass globalglass;
void global_function_call(void (MyClass::*method)())
{
(globalglass.*method)();
}
int main()
{
global_function_call(&MyClass::function1);
global_function_call(&MyClass::function2);
global_function_call(&MyClass::function3);
global_function_call(&MyClass::function4);
return 1;
}
Online Demo

Destructors output in C++

The following code does not print "Destructor is called". Why? However, my book prints it out. How to print the destructor statement? Please suggest.
#include <iostream>
using namespace std;
class HelloWorld
{
public:
//Constructor
HelloWorld()
{
cout<<"Constructor is called"<<endl;
}
//Destructor
~HelloWorld()
{
cout<<"Destructor is called"<<endl;
}
};
int main()
{
//Object created
HelloWorld obj;
system("PAUSE");
return 0;
}
Try this:
int main()
{
{ // start a scope
//Object created
HelloWorld obj;
} // scope ends here, obj will be deleted here
// now pause so you can see the output before program ends.
system("PAUSE");
return 0;
}

Thread inside a class with member function from another class

Can someone tell me how can I run a new thread with member function from a object of diffrent class as a member function of this class ? What ever im trying to do Im still getting errors.
no match for call to '(std::thread) (void (Boo::*)(), Boo&)'|
no match for call to '(std::thread) (void (Foo::*)(), Foo*)'|
#include <iostream>
#include <thread>
using namespace std;
class Boo
{
public:
void run()
{
while(1){}
}
};
class Foo
{
public:
void run()
{
t1(&Boo::run,boo);
t2(&Foo::user,this);
}
void user();
~Foo(){
t1.join();
t2.join();
}
private:
std::thread t1;
std::thread t2;
Boo boo;
};
int main()
{
Foo foo;
foo.run();
}
You need to use operator= to assign the threads after construction
Working example below (see it in action):
#include <thread>
#include <iostream>
class Boo
{
public:
void run()
{
int i = 10;
while(i--)
{
std::cout << "boo\n";;
}
}
};
class Foo
{
public:
void run()
{
t1 = std::thread(&Boo::run,boo); // threads already default constructed
t2 = std::thread(&Foo::user,this); // so need to *assign* them
}
void user()
{
int i = 10;
while(i--)
{
std::cout << "foo\n";;
}
}
~Foo()
{
t1.join();
t2.join();
}
private:
std::thread t1;
std::thread t2;
Boo boo;
};
int main()
{
Foo foo;
foo.run();
}

returning from spawned thread after a particular action in c++

I am new to thread related concepts.I have program with main function where I call function a() that spawns a thread(NewThread) using boost.now as part of thread i do some initialization of some variable and then start a while(1) loop.
I want give the control to function b() when control reaches inside while(1),
currently it is reaching to b() without starting while loop.
Please guide me.
void NewThread()
{
//initialization of some modules
//infinite while loop
while(1)
{
}
}
void a()
{
this->libThread = new boost::thread(boost::bind(&NewThread));
}
void b()
{
cout<<"function b";
}
int main()
{
a();
b();
}
you can use something like that with c++11. Note that you need to use an atomic variable for the condition, you can't just use a boolean variable. You can also use a condition variable.
#include <thread>
#include <atomic>
#include <memory>
std::unique_ptr<std::thread> t;
std::atomic<bool> condition(false);
void f() {
while(true) {
if(condition) {
return;
}
}
}
void a () {
t.reset(new std::thread(&f));
}
void b() {
condition = true;
t->join();
}
int main() {
a();
b();
}
update: you need to join the thread after setting the condition. which will block the main thread until the other thread returns.

Call member function of an object using pthread

How can I call the thread_ready_function into a thread as commented, using pthread ? I need to call it with the class object (In the real world the function uses attributes previously set).
MWE
#include <iostream>
#include <pthread.h>
class ClassA
{
public:
void * thread_ready_function(void *arg)
{
std::cout<<"From the thread"<<std::endl;
pthread_exit((void*)NULL);
}
};
class ClassB
{
ClassA *my_A_object;
public:
void test(){
my_A_object = new ClassA();
my_A_object->thread_ready_function(NULL);
// my_A_object->thread_ready_function(NULL);
// ^
// I want to make that call into a thread.
/* Thread */
/*
pthread_t th;
void * th_rtn_val;
pthread_create(&th, NULL, my_A_object.thread_ready_function, NULL);
pthread_join(th, &th_rtn_val);
*/
}
};
int main()
{
ClassB *my_B_object = new ClassB();
my_B_object->test();
return 0;
}
if you don't want to use C++11 or stl or boost, you must use the static key word for your member function,so that the pthread can call your member function!
example code:
#include <iostream>
#include <pthread.h>
using namespace std;
class A{
public:
static void* thread(void* args);
int parella_thread(int thread_num);
};
void* A::thread(void* args)
{
cout<<"hello world"<<endl;
}
int A::parella_thread(int thread_num)
{
pthread_t* thread_ids = new pthread_t[thread_num];
for(int i=0;i<thread_num;i++)
{
pthread_create(&thread_ids[i],NULL,thread,(void*)NULL);
}
delete[] thread_ids;
}
int main(int argc,char*argv[])
{
A test;
test.parella_thread(4);
return 0;
}