Pthreads and QT - linux

I have a issue, where I need the GUI from QT Designer to give values to a separate program running from terminal where the values from the GUI are "printed" onto the terminal interface (Linux with GCC compiler)
I have researched pthreads, but their application examples are limited to In-Application uses. The code in my main file is as follows:
#include "main_window.h"
#include <QApplication>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
void *thr_func(void *thread_id)
{
int tid = thread_id;
pthread_mutex_lock(&lock_x);
cout << "thread" << tid << end1;
cout << xValue << end1;
cout << yValue << end1;
cout << zValue << end1;
pthread_mutex_unlock(&lock_x);
}
int main(int argc, char *argv[])
{
pthread_create(thread_1, NULL, thr_func, NULL)
while(true)
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
pthread_exit(NULL);
}
**Note that xValue, yValue, and zValue already stream to a textfile from the QT application. I am adapting the application for terminal running and control.

Related

Intercept "Q" and company in KeyPress

I am trying to find a way to intercept all keys, but apparently all defaults commands are still there, functioning properly. I've started with the cone example, and tried both vtkCallbackCommand and SetInteractorStyle, as suggested in the examples:
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCallbackCommand.h>
#include <vtkCommand.h>
#include <array>
class KeyPressInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static KeyPressInteractorStyle* New();
vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);
virtual void OnKeyPress() override
{
vtkRenderWindowInteractor* rwi = this->Interactor;
std::string key = rwi->GetKeySym();
std::cout << "Pressed " << key << std::endl;
if (key == "Up")
{
std::cout << "The up arrow was pressed." << std::endl;
}
// Handle a "normal" key
if (key == "q" || key == "Q")
{
std::cout << "The q key was pressed." << std::endl;
return;
}
// DO NOT Forward events
//vtkInteractorStyleTrackballCamera::OnKeyPress();
}
};
vtkStandardNewMacro(KeyPressInteractorStyle);
void KeypressCallbackFunction(vtkObject* caller, long unsigned int eventId,
void* clientData, void* callData)
{
std::cout << "Keypress callback" << std::endl;
vtkRenderWindowInteractor* iren = static_cast<vtkRenderWindowInteractor*>(caller);
std::cout << "Pressed: " << iren->GetKeySym() << std::endl;
}
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
std::array<unsigned char, 4> bkg{{26, 51, 102, 255}};
colors->SetColor("BkgColor", bkg.data());
vtkNew<vtkCylinderSource> cylinder;
cylinder->SetResolution(8);
vtkNew<vtkPolyDataMapper> cylinderMapper;
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
vtkNew<vtkActor> cylinderActor;
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());
cylinderActor->RotateX(30.0);
cylinderActor->RotateY(-45.0);
vtkNew<vtkRenderer> renderer;
renderer->AddActor(cylinderActor);
renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
// Zoom in a little by accessing the camera and invoking its "Zoom" method.
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.5);
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(800, 800);
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("Cylinder");
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
// VIA STYLE
// vtkNew<KeyPressInteractorStyle> style;
// renderWindowInteractor->SetInteractorStyle(style);
// style->SetCurrentRenderer(renderer);
// VIA CALLBACK
vtkNew<vtkCallbackCommand> keypressCallback;
keypressCallback->SetCallback(KeypressCallbackFunction);
renderWindowInteractor->AddObserver(vtkCommand::KeyPressEvent, keypressCallback);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
In both cases, "Q" still quits, "W" still goes into wireframe.
Is there a way to properly hanlde all key events, even the default ones?
For those shortcuts (i.e. letters) there is also the CharEvent that is send, linked to the vtkInteractorStyle::OnChar method where quit and wireframe are implemented.
So either override it in your style (in addition to the OnKeyPress), either handle the event with the observer mechanism.

How to use CGAL::read_ply_points() to read binary ply files?

A new user of CGAL here.
I am currently trying out the example "registration_with_OpenGR.cpp"
https://cgal.geometryfactory.com/CGAL/doc/master/Point_set_processing_3/Point_set_processing_3_2registration_with_OpenGR_8cpp-example.html
It seems that the function CGAL::read_ply_points() that is used to read ply files does not work on binary format, but it works when a ASCII ply file is given. Is there any extra flag that I need to set when reading binary ply files?
here is my current code for reading ply files.
#include <CGAL/property_map.h>
#include <fstream>
#include <iostream>
#include <utility>
#include <vector>
typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_3 Point_3;
typedef K::Vector_3 Vector_3;
typedef std::pair<Point_3, Vector_3> Pwn;
typedef CGAL::First_of_pair_property_map<Pwn> Point_map;
typedef CGAL::Second_of_pair_property_map<Pwn> Normal_map;
namespace params = CGAL::parameters;
int main(int argc, const char** argv) {
const char* fname1 = "data/reference.ply";
const char* fname2 = "data/1.ply";
std::vector<Pwn> pwns1, pwns2;
std::ifstream input(fname1);
if (!input ||
!CGAL::read_ply_points(input, std::back_inserter(pwns1),
CGAL::parameters::point_map(CGAL::First_of_pair_property_map<Pwn>()).
normal_map(Normal_map())))
{
std::cerr << "Error: cannot read file " << fname1 << std::endl;
return EXIT_FAILURE;
}
input.close();
input.open(fname2);
if (!input ||
!CGAL::read_ply_points(input, std::back_inserter(pwns2),
CGAL::parameters::point_map(Point_map()).
normal_map(Normal_map())))
{
std::cerr << "Error: cannot read file " << fname2 << std::endl;
return EXIT_FAILURE;
}
input.close();
std::cerr << "SUCCESS" << std::endl;
return EXIT_SUCCESS;
}
You need to give std::ios_base::binary to your stream constructor:
std::ifstream input(fname1,std::ios_base::binary);

Why sleep() in a thread cannot be interrupted by signal in my code?

#include <iostream>
#include <thread>
#include <signal.h>
#include <unistd.h>
void handler(int sig){
std::cout << "handler" << std::endl;
}
void func() {
sleep(100);
perror("sleep err:");
}
int main(void) {
signal(SIGINT, handler);
std::thread t(func);
pthread_kill(t.native_handle(), SIGINT);
perror("kill err:");
t.join();
return 0;
}
If I put sleep() inside main function, and send a signal by pressing ctrl+c, sleep will be interrupted and return immediately with perror() saying it's interrupted.
But with the code above, the "handler" in handler function will be printed, but sleep will not return and the program keeps running. The output of this program is:
kill err:: Success
handler
And if I replace sleep() with recvfrom(), recvfrom() will not be interrupted even it's inside the main thread.
#include <vector>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
void SigHandler(int sig){
std::cout << "handler" << std::endl;
}
int main(void) {
signal(SIGINT, SigHandler);
int bind_fd_;
if ((bind_fd_ = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
std::cout << "socket creation failed " << strerror(errno) << std::endl;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(12345);
if (bind(bind_fd_, reinterpret_cast<const struct sockaddr *>(&servaddr),
sizeof(servaddr)) < 0) {
std::cout << "socket bind failed " << strerror(errno) << std::endl;
}
struct sockaddr_in cliaddr;
socklen_t cliaddr_len = sizeof(cliaddr);
std::vector<char> buffer(10*1024*1024,0);
std::cout << "Wait for new request"<< std::endl;
int n = 0;
while (n == 0) {
std::cout << "before recvfrom" << std::endl;
n = recvfrom(bind_fd_, buffer.data(), buffer.size(), 0,
reinterpret_cast<struct sockaddr *>(&cliaddr), &cliaddr_len);
// sleep(100);
perror("recvfrom err: ");
std::cout << "recv " << n << " bytes from " << cliaddr.sin_port<< std::endl;
}
}
I don't know what is wrong with my code, hoping your help, thanks
At the time you direct the signal to the thread, that thread has not yet proceeded far enough to block in sleep(). Chances are that it has not even been scheduled for the first time. Change the code to something like
std::thread t(func);
sleep(5); // give t enough time to arrive in sleep()
pthread_kill(t.native_handle(), SIGINT);
and you'll see what you expect.
Note that using signals in a multithreaded program is not usually a good idea because certain aspects are undefined/not-so-clearly defined.
Note also that it is not correct to use iostreams inside a signal handler. Signal handlers run in a context where pretty much nothing is safe to do, much like an interrupt service routine on bare metal. See here for a thorough explanation of that matter.

Using CLOCK_MONOTONIC type in the 'condition variable' wait_for() notify() mechanism

I am using code that runs on ARM (not Intel processor). Running c++11 code example (CODE A) from: http://www.cplusplus.com/reference/condition_variable/condition_variable/wait_for/ to test the wait_for() mechanism. This is not working right - looks like the wait_for() does not wait. In Intel works fine. After some research and using pthread library directly and setting MONOTONIC_CLOCK definition, solves the issue (CODE B).
(Running on ARM is not the issue)
My problem is :
How can I force the C++11 API wait_for() to work with MONOTONIC_CLOCK?
Actually I would like to stay with 'CODE A' but with the support or setting of MONOTONIC_CLOCK.
Thanks
CODE A
// condition_variable::wait_for example
#include <iostream> // std::cout
#include <thread> // std::thread
#include <chrono> // std::chrono::seconds
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable, std::cv_status
std::condition_variable cv;
int value;
void read_value() {
std::cin >> value;
cv.notify_one();
}
int main ()
{
std::cout << "Please, enter an integer (I'll be printing dots): \n";
std::thread th (read_value);
std::mutex mtx;
std::unique_lock<std::mutex> lck(mtx);
while
(cv.wait_for(lck,std::chrono::seconds(1))==std::cv_status::timeout)
{
std::cout << '.' << std::endl;
}
std::cout << "You entered: " << value << '\n';
th.join();
return 0;
}
CODE B
#include <sys/time.h>
#include <unistd.h>
#include <iostream> // std::cout
#include <thread> // std::thread
#include <chrono> // std::chrono::seconds
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable, std::cv_status
const size_t NUMTHREADS = 1;
pthread_mutex_t mutex;
pthread_cond_t cond;
int value;
bool done = false;
void* read_value( void* id )
{
const int myid = (long)id; // force the pointer to be a 64bit integer
std::cin >> value;
done = true;
printf( "[thread %d] done is now %d. Signalling cond.\n", myid, done
);
pthread_cond_signal( &cond );
}
int main ()
{
struct timeval now;
pthread_mutexattr_t Attr;
pthread_mutexattr_init(&Attr);
pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex, &Attr);
pthread_condattr_t CaAttr;
pthread_condattr_init(&CaAttr);
pthread_condattr_setclock(&CaAttr, CLOCK_MONOTONIC);
pthread_cond_init(&cond, &CaAttr);
std::cout << "Please, enter an integer:\n";
pthread_t threads[NUMTHREADS];
int t = 0;
pthread_create( &threads[t], NULL, read_value, (void*)(long)t );
struct timespec ts;
pthread_mutex_lock( &mutex );
int rt = 0;
while( !done )
{
clock_gettime(CLOCK_MONOTONIC, &ts);
ts.tv_sec += 1;
rt = pthread_cond_timedwait( & cond, & mutex, &ts );
std::cout << "..." << std::endl;
}
pthread_mutex_unlock( & mutex );
std::cout << "You entered: " << value << '\n';
return 0;
}
The documentation for std::condition_variable::wait_for says:
A steady clock is used to measure the duration.
std::chrono::steady_clock:
Class std::chrono::steady_clock represents a monotonic clock. The time points of this clock cannot decrease as physical time moves forward.
Unfortunately, this is gcc Bug 41861 (DR887) - (DR 887)(C++0x) does not use monotonic_clock that it uses system_clock instead of steady_clock for condition variables.
One solution is to use wait_until (be sure to read Notes section) function that allows to specify durations relative to a specific clock. E.g.:
cv.wait_until(lck, std::chrono::steady_clock::now() + std::chrono::seconds(1))

QThread::getCurrentThread() from non-Qt thread

What will I get from QThread::getCurrentThread(), if it is called from non-Qt thread?
Thanks!
QThread is just a wrapper, behind the scene it uses native threads.
QThread::currentThread creates and initialises a Q(Adopted)Thread instance if it doesn't exist yet.
In case of unix it uses pthreads.
#include <iostream>
#include <thread>
#include <pthread.h>
#include <QThread>
#include <QDebug>
void run() {
QThread *thread = QThread::currentThread();
qDebug() << thread;
std::cout << QThread::currentThreadId() << std::endl;
std::cout << std::this_thread::get_id() << std::endl;
std::cout << pthread_self() << std::endl;
thread->sleep(1);
std::cout << "finished\n";
}
int main() {
std::thread t1(run);
t1.join();
}
Output:
QThread(0x7fce51410fd0)
0x10edb6000
0x10edb6000
0x10edb6000
finished
I see that there is initialisation of Qt application main thread:
data->threadId = (Qt::HANDLE)pthread_self();
if (!QCoreApplicationPrivate::theMainThread)
QCoreApplicationPrivate::theMainThread = data->thread;
So there might be some side effects.
I'd advise not to mix QThread with non-Qt threads.

Resources