How to call a function in a new thread as std::thread does using wxWidgets? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
tomay3000
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Mon Apr 24, 2017 4:23 am

How to call a function in a new thread as std::thread does using wxWidgets?

Post by tomay3000 »

Hello,
I need to know if there is a way to call a function in a new thread as std::thread does using just wxWidgets as it does for wxEvtHandler::CallAfter (simple and easy and accepts lambda functions as well).

I don't need to write the entire wxThread derived class declaration and implementation from scratch (it is very annoying).

What I need is a ONE LINE of code to call the needed function in a new thread as std::thread does.

Is it possible using wxWidgets? any suggestions would be appreciated.

Sorry I am lazy to do wxThreading.

and Thank you in advance.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to call a function in a new thread as std::thread does using wxWidgets?

Post by doublemax »

Why don't you just use std::thread if it suits you better?
Use the source, Luke!
tomay3000
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Mon Apr 24, 2017 4:23 am

Re: How to call a function in a new thread as std::thread does using wxWidgets?

Post by tomay3000 »

I just thought, it may exist in the toolkit and just not documented, and I am not seeing it.
Since I am using wxWidgets, why not using such a function (if it exists) as for example I am using wxString all the time instead of std::string.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to call a function in a new thread as std::thread does using wxWidgets?

Post by doublemax »

tomay3000 wrote: Fri May 10, 2019 9:03 pm I just thought, it may exist in the toolkit and just not documented, and I am not seeing it.
In this case, there isn't.
Use the source, Luke!
tomay3000
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Mon Apr 24, 2017 4:23 am

Re: How to call a function in a new thread as std::thread does using wxWidgets?

Post by tomay3000 »

Thank you.
alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 329
Joined: Tue Oct 18, 2016 2:31 pm

Re: How to call a function in a new thread as std::thread does using wxWidgets?

Post by alys666 »

tomay3000 wrote: Fri May 10, 2019 8:07 pm Hello,
I need to know if there is a way to call a function in a new thread as std::thread does using just wxWidgets as it does for wxEvtHandler::CallAfter (simple and easy and accepts lambda functions as well).

I don't need to write the entire wxThread derived class declaration and implementation from scratch (it is very annoying).

What I need is a ONE LINE of code to call the needed function in a new thread as std::thread does.

Is it possible using wxWidgets? any suggestions would be appreciated.

Sorry I am lazy to do wxThreading.

and Thank you in advance.
write your simplest class:

Code: Select all

class EasyThread:public wxThread{
	void (*_func)(); //function to call in body
	
	//body - hidden, we are simulating std::thread
	wxThread::ExitCode Entry() override{
		if(_func!=nullptr) _func(); //if function is not null call it!!!
		return 0; //return some blabla
	};

	//create and start thread instance
	void start(){
		this->Create();
		this->Run(); //Run will enter Entry() function(see above)
	}

public:
	//ctor with given body function
	EasyThread(void (formal_func)() ):wxThread(), _func(formal_func){
		this->start(); //start the thread
	}
	virtual ~EasyThread(){};
};

//test for c++ corectness
//declare body function
void ff(){}
EasyThread test1(ff); //create thread with ff body and immediately run it
EasyThread test2(ff); //create second thread with ff body and immediately run it

1. drawbacks of std::thread - it has been created and immediately running. it's not good. better to create threads and run them separately.
2. it's simplest implementation. i have to read wxThread(and std::thread) docs to completely simulate std::thread. imho the code needs a couple of additional lines somewhere.
ubuntu 20.04, wxWidgets 3.2.1
alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 329
Joined: Tue Oct 18, 2016 2:31 pm

Re: How to call a function in a new thread as std::thread does using wxWidgets?

Post by alys666 »

i slightly corrected the code given above - to have joinable thread and added join() member.
but not tested in real life.

Code: Select all

class EasyThread:public wxThread{
	void (*_func)(); //function to call in body
	
	//body - hidden, we are simulating std::thread
	wxThread::ExitCode Entry() override{
		if(_func!=nullptr) _func(); //if function is not null call it!!!
		return 0; //return some blabla
	};

	//create and start thread instance
	void start(){
		this->Create();
		this->Run(); //Run will enter Entry() function(see above)
	}

public:
	//ctor with given body function
	EasyThread(void (formal_func)() ):wxThread(wxTHREAD_JOINABLE), _func(formal_func){
		this->start(); //start the thread
	}
	virtual ~EasyThread(){};
	void join(){this->Wait();}
};

//test for c++ corectness
//declare body function

void myBody(){};
void testEasyThread(){
	EasyThread test1(myBody);
	EasyThread test2(myBody);
	test1.join();
	test2.join();
}
ubuntu 20.04, wxWidgets 3.2.1
tomay3000
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Mon Apr 24, 2017 4:23 am

Re: How to call a function in a new thread as std::thread does using wxWidgets?

Post by tomay3000 »

Interesting std::thread simulation.
I will give it a try.

Thank you.
alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 329
Joined: Tue Oct 18, 2016 2:31 pm

Re: How to call a function in a new thread as std::thread does using wxWidgets?

Post by alys666 »

tomay3000 wrote: Fri May 10, 2019 11:51 pm Interesting std::thread simulation.
I will give it a try.

Thank you.
but.. std::thread has template constructor with variadic param list.
std::thread t(func)
std::thread t(func, param)
std::thread t(func, param, param...)

it can be simulated via template class EasyThread... and after a bit of thinking, i just recommend you to use std::thread(if you need constructor with variadic parameters). you can use std::thread with wxWidgets without limitations, instead of its simulation via wxThread.
ubuntu 20.04, wxWidgets 3.2.1
Post Reply