_set_se_translator not working for a thread - multithreading

I want to direct my exceptions raised in thread using _set_se_translator function
Here is the implementation
Thread
_set_se_translator(ExceptionHandler)
Int x = 10;
Int y = x / 0;
Exception handler
Void ExceptionHandler( unsigned code, EXCEPTION_POINTERS*)
{
Throw Exception(code);
}
Exception Class
class Exception
{
Public:
Exception(unsigned codePar) { code =codePar}
Unsigned code;
Const char * getcodestring(void);
Char message[250];
};
getcodestring finction
Const char " Exception::getcodestring()
{
swtich(code)
{
Case EXCEPTION_FLT_DIVIDE_BY_ZERO:
Sprintf(message, "by zero");
Breask;
}
Return message
}
But I never got the divide by zero message infa t I think it's not going through the translator function in my thread
This is what I am getting
Unhandled win32 structured exception
Code : C000094
Where is that I am missing?
PN: there is no try catch block in my thread. Assuming the exception is caught by the translator

Related

How to solve "Cross-thread operation not valid" in Visual C++/CLI

I have an issue (Windows Forms, C++, .NET) like this:
When I am trying to modify any WinForms object element (label, button etc) from a manage thread, I get the following error:
Sample Code:
static void myThread(System::Object^ obj, int nSocket) {
while (true) {
MyForm^ ob = (MyForm^)obj;
char buff[256 + 1] = { 0, };
recv(nSocket, buff, 255, 0);
string recvStr(buff);
ob->label3->Text = gcnew String(recvStr.c_str());
}
}
Exception thrown:
'System.InvalidOperationException' in System.Windows.Forms.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
Cross-thread operation not valid: Control 'label3' accessed from a thread other than the thread it was created on.
How to solve that issue in C++/CLI?

use a lambda to start a thread which is a class attribute

I would like to assign a name to a thread, the thread itself must do this. The thread is a class member of the class foo.
I would like to start this thread with a lambda but unfortunately I get the error message:
no match for call to '(std::thread) (foo::start()::<lambda()>)
Can someone explain to me where the problem is?
Previously I had created a temporary thread object, and put this with move on the thread "manage", however, I can then give no name.
class foo {
public:
int start()
{
this->manage([this](){
auto nto_errno = pthread_setname_np(manage.native_handle(),"manage"); // Give thread an human readable name (non portable!)
while(1){
printf("do work");
}
});
return 1;
}
private:
int retVal;
std::thread manage;
};
You passed the lambda in a wrong way, after initialization the manage thread can't be initialized again. you should create a new std::thread and assign it.
the following compiles and indeed prints "manage".
class foo {
public:
int start()
{
manage = std::thread([this]{
auto nto_errno = pthread_setname_np(manage.native_handle(),"manage");
char name[16];
pthread_getname_np(pthread_self(), &name[0], sizeof(name));
cout << name << endl;
});
manage.join();
return 1;
}
private:
int retVal;
std::thread manage;
};

RXCPP , stack overflow when I did use retry operator indefinitely in observable

I'm trying to make an observable that when an error is detected, this will be execute again, but did notice something , when "on_error()" with "retry" operator is execute, this only re-run again the Observable but, the current instance of Observable still in the current stack, in other words is still alive
I did make a test to verify the behavior
#include <string>
#include "rxcpp/rx.hpp"
class test_class
{
public:
int a;
test_class() {
printf("Create Obj \n");
a = 1;
}
~test_class() {
printf("Destroy Obj \n");
a = 0;
}
};
int main()
{
// Create Observable request
auto values = rxcpp::observable<>::create<std::string>(
[&](rxcpp::subscriber<std::string> subscriber) {
test_class test;
while (subscriber.is_subscribed()) {
std::exception_ptr eptr = std::current_exception();
subscriber.on_error(eptr);
int a;
a = 2;
subscriber.on_next("normal");
}
})
.retry()
.as_dynamic();
values.
subscribe(
[](std::string v) {
printf("OnNext: %s\n", v.c_str()); },
[](std::exception_ptr ep) {
printf("OnError: %s\n", rxcpp::util::what(ep).c_str()); },
[]() {
printf("OnCompleted\n"); });
}
So, my input output is
Create Obj
Create Obj
Create Obj
Create Obj
...
I did expect to see "Destroy Obj" output as well
also I got a Stack overflow exception
My goal is , execute an Observable Object, that when an error is triggered, this could be restart again, but destroying curruent one, in orden to prevent Stack overflow exception
Maybe there's exist another way to make this, could you help me?
I found a possible solution, I only remove the loop inside Observable and retry operator, then I add a loop in Subscribe operation
I know is not an "Elegant" solution but that is the idea that I want to do, could you help me on this?
How could be the better way using RxCPP library?
#include <string>
#include "rxcpp/rx.hpp"
class test_class
{
public:
int a;
test_class() {
printf("Create Obj \n");
a = 1;
}
~test_class() {
printf("Destroy Obj \n");
a = 0;
}
};
int main()
{
// Create Observable request
auto values = rxcpp::observable<>::create<std::string>(
[&](rxcpp::subscriber<std::string> subscriber) {
test_class test;
//while (subscriber.is_subscribed()) {
std::exception_ptr eptr = std::current_exception();
subscriber.on_error(eptr);
int a;
a = 2;
subscriber.on_next("normal");
//}
});
//.retry()
//.as_dynamic();
for (;;) {
values.
subscribe(
[](std::string v) {
printf("OnNext: %s\n", v.c_str()); },
[](std::exception_ptr ep) {
printf("OnError: %s\n", rxcpp::util::what(ep).c_str()); },
[]() {
printf("OnCompleted\n"); });
}
}
Here my output:
Create Obj
OnError: bad exception
Destroy Obj
Create Obj
OnError: bad exception
Destroy Obj
Without stack overflow exception error

Creating new thread causing exception

I have a timer that will create a new thread and wait for the timer to expire before calling the notify function. It works correctly during the first execution, but when the timer is started a second time, an exception is thrown trying to create the new thread. The debug output shows that the previous thread has exited before attempting to create the new thread.
Timer.hpp:
class TestTimer
{
private:
std::atomic<bool> active;
int timer_duration;
std::thread thread;
std::mutex mtx;
std::condition_variable cv;
void timer_func();
public:
TestTimer() : active(false) {};
~TestTimer() {
Stop();
}
TestTimer(const TestTimer&) = delete; /* Remove the copy constructor */
TestTimer(TestTimer&&) = delete; /* Remove the move constructor */
TestTimer& operator=(const TestTimer&) & = delete; /* Remove the copy assignment operator */
TestTimer& operator=(TestTimer&&) & = delete; /* Remove the move assignment operator */
bool IsActive();
void StartOnce(int TimerDurationInMS);
void Stop();
virtual void Notify() = 0;
};
Timer.cpp:
void TestTimer::timer_func()
{
auto expire_time = std::chrono::steady_clock::now() + std::chrono::milliseconds(timer_duration);
std::unique_lock<std::mutex> lock{ mtx };
while (active.load())
{
if (cv.wait_until(lock, expire_time) == std::cv_status::timeout)
{
lock.unlock();
Notify();
Stop();
lock.lock();
}
}
}
bool TestTimer::IsActive()
{
return active.load();
}
void TestTimer::StartOnce(int TimerDurationInMS)
{
if (!active.load())
{
if (thread.joinable())
{
thread.join();
}
timer_duration = TimerDurationInMS;
active.store(true);
thread = std::thread(&TestTimer::timer_func, this);
}
else
{
Stop();
StartOnce(TimerDurationInMS);
}
}
void TestTimer::Stop()
{
if (active.load())
{
std::lock_guard<std::mutex> _{ mtx };
active.store(false);
cv.notify_one();
}
}
The error is being thrown from my code block here:
thread = std::thread(&TestTimer::timer_func, this);
during the second execution.
Specifically, the error is being thrown from the move_thread function: _Thr = _Other._Thr;
thread& _Move_thread(thread& _Other)
{ // move from _Other
if (joinable())
_XSTD terminate();
_Thr = _Other._Thr;
_Thr_set_null(_Other._Thr);
return (*this);
}
_Thrd_t _Thr;
};
And this is the exception: Unhandled exception at 0x76ED550B (ucrtbase.dll) in Sandbox.exe: Fatal program exit requested.
Stack trace:
thread::move_thread(std::thread &_Other)
thread::operator=(std::thread &&_Other)
TestTimer::StartOnce(int TimerDurationInMS)
If it's just a test
Make sure the thread handler is empty or joined when calling the destructor.
Make everything that can be accessed from multiple threads thread safe (specifically, reading the active flag). Simply making it an std::atomic_flag should do.
It does seem like you are killing a thread handle pointing to a live thread, but hard to say without seeing the whole application.
If not a test
...then generally, when need a single timer, recurreing or not, you can just go away with scheduling an alarm() signal into itself. You remain perfectly single threaded and don't even need to link with the pthread library. Example here.
And when expecting to need more timers and stay up for a bit it is worth to drop an instance of boost::asio::io_service (or asio::io_service if you need a boost-free header-only version) into your application which has mature production-ready timers support. Example here.
You create the TestTimer and run it the first time via TestTimer::StartOnce, where you create a thread (at the line, which later throws the exception). When the thread finishes, it sets active = false; in timer_func.
Then you call TestTimer::StartOnce a second time. As active == false, Stop() is not called on the current thread, and you proceed to creating a new thread in thread = std::thread(&TestTimer::timer_func, this);.
And then comes the big but:
You have not joined the first thread before creating the second one. And that's why it throws an exception.

PostMessage from WorkerThread to Main Window in MFC

I have a MFC application, which has a worker thread, what I want to do is to post message from worker thread to the Main GUI thread to update some status messages on GUI. What I have done so far is Registered a new window message
//custom messages
static UINT FTP_APP_STATUS_UPDATE = ::RegisterWindowMessageA("FTP_APP_STATUS_UPDATE");
Added this message to the message map of dialog class
ON_MESSAGE(FTP_APP_STATUS_UPDATE, &CMFC_TestApplicationDlg::OnStatusUpdate)
The prototype of OnStatusUpdate is
afx_msg LRESULT OnStatusUpdate(WPARAM, LPARAM);
and definition is
LRESULT CMFC_TestApplicationDlg::OnStatusUpdate(WPARAM wParam, LPARAM lParam)
{
//This function is not called at all.
return 0;
}
and the worker thread calling code is
void CMFC_TestApplicationDlg::OnBnClickedMfcbutton1()
{
ThreadParams params;
params.m_hWnd = m_hWnd;
params.FTPHost = "test_host";
params.FTPUsername = "test";
params.FTPPassword = "test";
AfxBeginThread(FTPConnectThread,&params);
}
and Worker thread code is
//child thread function
UINT FTPConnectThread( LPVOID pParam )
{
if(pParam == NULL)
{
return 0;
}
ThreadParams *params = (ThreadParams*)pParam;
OutputDebugString(params->FTPHost);
Sleep(4000); //simulating a network call
CString * message = new CString("Conencted");
PostMessage(params->m_hWnd,FTP_APP_STATUS_UPDATE,0,(LPARAM)message);
//PostMessage do nothing? what I am doing wrong?
return 1;
}
the problem is when the PostMessage function is called the OnStatusUpdate should be called, but it is not being called, no exception or assertion is thrown, What I am doing wrong? I have tried ON_REGISTERED_MESSAGE and ON_MESSAGE but no success, any help?
CMFC_TestApplicationDlg::OnBnClickedMfcbutton1() may return before the thread starts. This causes your ThreadParams to go out of scope, so when you access it from the thread, you are accessing freed memory. You need to allocate it some other way, such as:
void CMFC_TestApplicationDlg::OnBnClickedMfcbutton1()
{
ThreadParams* params = new ThreadParams();
params->m_hWnd = m_hWnd;
params->FTPHost = "test_host";
params->FTPUsername = "test";
params->FTPPassword = "test";
AfxBeginThread(FTPConnectThread,params);
}
//child thread function
UINT FTPConnectThread( LPVOID pParam )
{
if(pParam == NULL)
{
return 0;
}
ThreadParams *params = (ThreadParams*)pParam;
OutputDebugString(params->FTPHost);
Sleep(4000); //simulating a network call
CString * message = new CString("Conencted");
PostMessage(params->m_hWnd,FTP_APP_STATUS_UPDATE,0,(LPARAM)message);
delete params;
return 1;
}

Resources