I want to create a thread with ACE_thread_manager, there is no error when I debug. but the result is not right. The function did not work; code like this:
#include "ace/OS.h"
#include "ace/Log_Msg.h"
#include "ace/Thread_Manager.h"
#include <iostream>
void thread_start(void* arg)
{
std::cout << "Running thread..\n";
}
int main(int argc, char *argv[])
{
ACE_Thread_Manager::instance()->spawn(ACE_THR_FUNC(thread_start), 0, THR_NEW_LWP);
return 0;
}
this demo should print "Running thread.." , but when I debug it ,it print nothing . These Chinese mean "Please press any key to continue" .
You have to wait in your main until your workers threads have finished. As you say, you have to add the following line before the return in main.
ACE_Thread_Manager::instance()->wait();
Related
I'm trying to implement a simple command line program that takes three arguments and prints them on the linux terminal
For example:
>c++ exec.cpp
>./a 32 + 32
Should print out contents like this
32
+
32
But the program is looping indefinitely
I've implemented a check for argc
Like this
if(argc!=3) {
cout << "Exit" << endl;
return -9999;
}
In case the argument count is 3
These lines of code should be executed
else {
for(int i=0;i<argc;i++){
cout << argv[i] << endl;
}
}
But as I explained before the program loops indefinitely
EDIT:
Since I was asked to post the entire code here it is
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string>
using namespace std;
int main(int argc,char* argv[]) {
if(argc!=3) {
cout << "Exit" << endl;
return -9999;
}
else {
for(int i=0;i<argc;i++){
cout << argv[i] << endl;
}
}
}
Your code is working...
I made slight modifications (eg. argc !=4 , i <= argc , etc. )
I compiled it using gcc (g++) on my linux system using:
g++ exec.cpp -o a.out
./a.out
Output:
Exit
When I run:
./a.out 4 + 3
Output:
4
+
3
Now with code, that does look okey to me in fact.
There doesn't seem to be a way to have an endless loop in every case.
What happens if you add return(0); to the very end of the main function?
Main always has to return something and compilers normally either complain or do that on their own hwoever if the propgrammer didn't add it.
Oh, have you tried the cerr variant instead of cout for your error message? Because returning from the main right after, is quite the same as the crash I mentioned.
I'm developing an app that should freeze all input, both keyboard and mouse, for a period of time. I've tried using XGrabKeyboard, but I cannot revert its effect using XUngrabKeyboard, it does nothing.
Here's a minimal example you can easily compile:
#include <iostream>
#include <thread>
#include <chrono>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/cursorfont.h>
int main(int argc, char *argv[])
{
Display * dpy = nullptr;
dpy = XOpenDisplay(0);
if(!dpy)
{
std::cerr << "Error" << std::endl;
return 1;
}
std::cerr << "Grabbing..." << std::endl;
XGrabKeyboard(dpy, DefaultRootWindow(dpy), false, GrabModeAsync, GrabModeAsync, CurrentTime);
std::cerr << "Waiting 2 secs, you shouldn't be able to type anything" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
std::cerr << "Ungrabbing..." << std::endl;
XUngrabKeyboard(dpy, CurrentTime);
std::cerr << "Try to type now" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
}
You can see that you cannot write anything anymore. I've tried clicking on the terminal, in case the focus is lost or anything, to no avail. Once the program finishes the keyboard is released.
Not sure if it has something to do with the parameters in the XGrabKeyboard call, I've tried modifying them (Sync vs Async, etc). But there's no difference.
Adding an XSync(dpy, true); (*) after XUngrabKeyboard makes the code behave in the way you expected. So possibly you have to process all the events you grabbed before the event queue resumes?
(*): don't actually do this, this is just to demonstrate that the problem is with the queued events
Also works:
XUngrabKeyboard(dpy, CurrentTime);
XEvent foo;
while (XPending(dpy)) XNextEvent(dpy, &foo);
Update - also works:
XFlush(dpy);
So... the problem is that the ungrab was not actually sent?
#include <iostream>
int main(int argc, char* args[]) {
std::cout << 'hi';
std::cout << "hello";
}
When I run this code on windows, I don't see any output. What am I doing wrong?
EDIT
I think its an issue on my machine, hence my question. I understand it could work in theory, but I want to know why it isn't working in practice. (On my windows computer)
some of the compilers will close the output windows after executing the code, so we use getchar() or getch() or system("Pause") at the end of the code. so the output windows will wait for a key press event to close the output window. so you can see your output.
std::cout << "hi"; // Double quotes required.
output-here
You should see some output with your original program, though it may not be the desired output. 26729hello
The buffers of std::cout are not being flushed to the console.
Try Running:
#include <iostream>
int main(int argc, char* args[]) {
std::cout << "hi";
std::cout << "hello"<<std:endl;
}
The issue is that the std::cout is storing the text "hihello" in an internal buffer, but this buffer is not being "flushed" which in this case means written to the console window.
I am trying to implement GUI for a simple C++ using Qt to understand how it works. The C++ program and the GUI are in seperate projects in the same solution in VS 2015. The Qt program will call the C++ program using QProcess' start() function. The C++ console application will be running the background will the Qt program acts as an interface. My question is, how I do I pass values to the C++ program using QProcess and How do I obtain the output from the C++ program? Here is the sample program I am working with:-
The C++ Program
#include<iostream>
#include<fstream>
using namespace std;
void main() {
int a;
cout << "Enter a number" << endl;
cin >> a;
cout << "The Square of the number is " << (a*a) << endl;
ofstream write;
write.open("test.txt");
write << (a * a);
write.close();
}
The Qt Program
#include "FrontEnd.h"
#include <QtWidgets/QApplication>
#include <qprocess.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FrontEnd w;
w.show();
QProcess p1;
p1.start("Interface.exe");
p1.write("5",5);
return a.exec();
}
I tried to pass the value using the write() function but it did not seem to work as the test.txt file remained empty. The Qt program I have written lacks the GUI features as I will be adding them once I figure out how to send and recieve data using QProcess. Thank you for your assistance!
You have many problems in the code you are showing:
You are passing write two arguments "5" and 5, this means that it will use 5 chars from the the char* "5". Absolutely, this is not what you want as this will lead to undefined behavior resulted from accessing memory that does not belong to your data.
Instead you should be using the second version of write which accepts a zero-terminated string, like this: p1.write("5");
In order to make cin in your console program know that the number it should read is finished, you should pass a new line after your number, so your call will end up like this: p1.write("5\n");
You should use the readyRead signal in the Qt program to get notified when your process has new output that you can read, and then you may call readAll from a slot which is connected to that signal.
For completeness, Here is how your code should be:
interface.cpp
#include<iostream>
using namespace std;
void main() {
int a;
cin >> a;
//No need to use file output
//it is simpler and more appropriate to read the output from stdout
cout << "The Square of the number is " << (a*a) << endl;
}
The Qt program
#include <QApplication>
#include <QWidget>
#include <QProcess>
#include <QDebug>
#include "FrontEnd.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FrontEnd w;
w.show();
QProcess p1;
p1.start("Interface.exe");
p1.write("5\n");
QObject::connect(&p1, &QProcess::readyRead, [&p1](){
//output to qDebug, you may want to update some GUI component instead
qDebug() << p1.readAll();
});
return a.exec();
}
I installed codellite 7.0 yesterday and have been trying to work with it. But there seem to be some problem. I cannot run any code.
For now code is pretty simple.
#include <stdio.h>
int main(int argc, char **argv)
{
printf("hello world\n");
return 0;
}
however, it returns following and output is blank with Press any key to continue
Current working directory: D:\ .....
Running program: le_exec.exe ./1
Program exited with return code: 0
Your program is running fine, the only problem is that after the printf the program returns 0 and shuts down immediately, it does run and print out "hello world", you just don't have the time to see it.
To make the program wait for user input so that you can see the output use cin.get() :
#include <stdio.h>
#include <iostream>
int main(int argc, char **argv)
{
printf("hello world\n");
std::cin.get();
return 0;
}