implementation of asynchronous thread in c++ - c++

I am unable to find a correct usage of asynchronous thread in c++11.The thing i want to do is i want to spwan threads and each thread will function simultaneously without waiting for each other like thread.join(),which makes other thread to wait until the current thread is done with.So,is there any library in c++ which makes threads to run parallely doing their work simultaneously without having to wait for the other to complete.Actually the thing i want is i want to run each threads sumultaneously, so that they don't wait for the other to complete and its functionality is executed simultaneously without having to wait for others to finish .
Thanks,
Kushal
EDIT:
EDIT:: i am posting the code below
#include <signal.h>
#include <thread>
#include <algorithm>
#include <cstring>
#include <csignal>
#include "paho_client.h"
using namespace std;
vector<string> topic_container{"rpi2/temp","sense /bannana","sense/util","mqtt/temp","sense/temp","sense/pine","sense/fortis/udap"};
vector<paho_client> publisher;
vector<paho_client> subscriber;
int finish_thread=1;
void Onfinish(int signum){
finish_thread=0;
exit(EXIT_FAILURE);
}
int main(int argc, char** argv) {
signal(SIGINT, Onfinish);
int topic_index;
if(argc<3){
cout<<"the format of starting commandline argument is"<<endl;
exit(1);
}
while(finish_thread!=0){
//paho_client::get_library_handle();
if(strcmp(argv[1],"create_publisher")){
for(topic_index=0;topic_index<atoi(argv[2]);topic_index++){
thread pub_th;
pub_th = thread([ = ]() {
paho_client client("publisher", "192.168.0.102", "9876",
topic_container[topic_index].c_str());
client.paho_connect_withpub();
publisher.push_back(client);
});
pub_th.join();
}
vector<paho_client>::iterator it;
int publisher_traverse=0;
for(it=publisher.begin();it<publisher.end();publisher_traverse++){
publisher[publisher_traverse].increment_count();
publisher[publisher_traverse].get_count();
}
}
}
return 0;
}
After using async with future am getting the same behaviour as above please point me where am i going wrong
#include <signal.h>
#include <thread>
#include <algorithm>
#include <cstring>
#include <csignal>
#include <future>
#include "paho_client.h"
using namespace std;
vector<string> topic_container{"rpi2/temp","sense/apple","sense/bannana","sense/util","mqtt/temp","sense/temp","sense/pine","sense/fortis/udap"};
vector<paho_client> publisher;
vector<paho_client> subscriber;
int finish_thread=1;
void Onfinish(int signum){
finish_thread=0;
exit(EXIT_FAILURE);
}
int accumulate_block_worker_ret(int topic_index) {
//int topic_index=0;
paho_client client("publisher", "192.168.0.102", "9876",
topic_container[topic_index].c_str());
client.paho_connect_withpub();
publisher.push_back(client);
client.increment_count();
return client.get_count();
}
int main(int argc, char** argv) {
signal(SIGINT, Onfinish);
if(argc<3){
cout<<"the format of starting commandline argument is . /paho_client_emulate <create_publisher><count of publisher client to spawn>" <<endl;
exit(1);
}
while(finish_thread!=0){
// paho_client::get_library_handle();
int topic_index;
if(strcmp(argv[1],"create_publisher")){
for(topic_index=0;topic_index<atoi(argv[2]);topic_index++){
// thread pub_th;
// pub_th = thread([ = ]() {
future<int> f = async(std::launch::async,accumulate_block_worker_ret,topic_index);
// });
// pub_th.join();
cout<<"the returned value from future is"<<f.get()<<endl;
}
vector<paho_client>::iterator it;
int publisher_traverse=0;
for(it=publisher.begin();it<=publisher.end();publisher_traverse++){
cout<<"came here"<<endl;
publisher[publisher_traverse].increment_count();
publisher[publisher_traverse].get_count();
}
}
}
return 0;
}

i want to launch all the publisher clients first (as threads) and
later publish messages from each threads
The pub_th.join() is misplaced inside the loop where the threads are started, thus waiting for the termination of each thread before starting the next one. To let the threads run in parallel, just move the .join() outside that loop. Of course to access the threads after the loop body, they have to be stored somewhere, e. g. in a vector - for this, change the first for loop to
vector <thread> pub_threads;
for (topic_index=0; topic_index<atoi(argv[2]); topic_index++)
{
pub_threads.push_back(thread([ = ]() { /* whatever */ }));
}
and later when done:
for (auto &th: pub_threads) th.join();
Actually i am running infinite while inside every instance of
paho_client so the first thread is not completed …
that thread is run continously
Of course if never done, there's no point to .join().

Related

How can new gnome terminal receive command in C

I have tried to write a program that run in ubuntu terminal .Program will open a new gnome terminal and run command in that new terminal to open new abcd.txt using vim.And then when i Ctrl+C in the first terminal which run the program ,new gnome terminal will shut vim down and have an announcement in the first terminal
I have tried system("`gnome-terminal`<< vim abcd.txt");
and this system("vim abcd.txt>>`gnome-terminal`");
but the new one terminal cannot recieve command
My full code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
int loop=1;
void DEF()
{
system("kill -9 pidof vim");
loop=0;
}
void *subthreads(void *threadid)
{
loop=1;
long tid;
tid=(long)threadid;
system("`gnome-terminal`<< vim abcd.txt");
signal(SIGINT,DEF);
while(loop){}
pthread_exit(NULL);
}
void main()
{
int loop=1;
pthread_t threads;
int check;
long tID;
check= pthread_create(&threads,NULL,&subthreads,(void*)tID);
while(loop){}
printf("Ctrl+C is pressed!\n");
}
Not sure what you are trying to achieve in the end. But here are a few ideas, starting from your code:
The terminal command (in system()) should be something like Mark Setchell pointed out, like for example system("gnome-terminal -e vim file.txt");
The system() command is blocking further execution of your code, so the call to signal() is not happening until you terminate the system() call.
pidof is not working on my Linux system. I would use pkill <program>. Still, that would kill all running instances of , for example vim or your terminal.
You are declaring the variable loop in the global scope first and then redeclaring it in main(). If you really want to use it as a global variable, it should just be loop=1 in main().
You are not using the variable tid for anything.
Here is an improved version of your program, with additional printf calls to explain to the user what is happening. I also used xterm and nano because I don't have gnome-terminal, and I didn't want to interfere with my running instance of vim. But it still is maybe not exactly what you are trying to do. The main problem is that system("xterm -e sh &") is blocking and when you press Ctrl-C, that system call will terminate xterm so that the def() function will do nothing when it is called later.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
int loop = 1;
void def()
{
printf("In def\n");
system("pkill xterm");
loop=0;
}
void *subthreads(void *threadid)
{
printf("Starting subthread\n");
loop = 1;
long tid;
tid = (long)threadid;
signal(SIGINT, def);
system("xterm -e sh -c nano &"); // Note: xterm will still exit when you press Ctrl-C
printf("Terminal exited in subthread\n");
while (loop);
printf("Exited loop in subthread\n");
pthread_exit(NULL);
}
void main()
{
pthread_t threads;
int check;
long tID;
check = pthread_create(&threads, NULL, &subthreads, (void*)tID);
printf("In main after thread creation\n");
while (loop);
printf("Ctrl+C is pressed!\n");
}
Another option is to use fork() instead of pthread to split into a separate process. (Note that processes are like separate applications while threads are processor threads in the same application.)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
void def()
{
system("pkill nano");
printf("def(): Killed nano\n");
}
int subprocess()
{
signal(SIGINT, def);
pid_t parent_id = getpid(); // Get process ID of main process
fork(); // Fork into two identical copies of the running app.
if (getpid() != parent_id) { // The part in the if block is only done in the second process!
system("xterm -e sh -c nano &");
printf("subprocess(): system call ended in forked process\n");
exit(0);
}
}
int main()
{
subprocess();
printf("Entering while loop in main process\n");
while (1);
printf("Exited main thread\n");
}
The one flaw with this version is the same as the previous one: when Ctrl-C is pressed, xterm/nano is killed and def() will subsequently do nothing except catch any Ctrl-C done afterwards.
If you explain further what your final goal is, maybe I can give some suggestions.
Like, why do you want to start vim in a terminal from a C application and then kill vim? Do you want to kill the whole terminal or only vim?

Threading Error static assertion failed C++

I am actually doing a programm to simulate stuffs in c++ , However I have a big problem which is :
error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues|
I did actually made a lot of research but nothing came out and I dont know at all what to do my error is for std::thread AppliWaitThd(AppliMsgWait()); who is not a class and I dont know how to make it work. AppliMsgWait is just a while loop with action inside.
here is my code
#include "../Include/classglConfigCtx.h"
#include "../Include/SupSrv.h"
#include "../Include/ClassRadMgr.h"
#include "../Include/globalDefine.h"
#include "../Include/structGesCan.h"
#include "../Include/AppliMsg.h"
#include "../Include/AppliSNMP.h"
#include "../Include/GesCanSlot.h"
#include "../Include/GesCanSrv.h"
int main (){
SupSrv SupSrvFile("V0", "SupSrV.txt" );
ConfigClass ConfigCtx;
RadMgr RadioManager;
AppliMsgInit();
role InitRole ;
InitRole = ConfigCtx.ConfigClassGetrole_();
if (InitRole==masterRole)
{
GesCanSrvInit();
GesCanSlotInit();
};
std::thread RadMgrThd(&RadMgr::RadMgrWaitEvt,&RadioManager);
std::thread AppliWaitThd(AppliMsgWait());
std::thread AppliSNMPWaitThd(AppliSNMPWait());
if(InitRole==masterRole)
{
std::thread GesCanSrvThd(GesCanSrvRecvMsg());
std::thread GesCanSlotThd(GesCanSlotPeriod());
};
RadMgrThd.join();
AppliWaitThd.join();
AppliSNMPWaitThd.join();
//GesCanSrvThd.join();
//GesCanSlotThd.join();
return 0;
};
Here is my function
int AppliMsgWait(){
while(1)
{
// vérification des données du contexte de config?
if(ConfigCtx.ConfigClassGetFlagChangeAddr_()==true) {
auto newNetCastAddr= ConfigCtx.ConfigClassGetnetCastAddr_();
auto newNeighCastAddr = ConfigCtx.ConfigClassGetneighCastAddr_();
serverAddress.sin_addr.s_addr= inet_addr(newNeighCastAddr.c_str());
ConfigCtx.ConfigClassSetFlagChangeAddr_(false);
}
else if(glCSMAMsgToSCOMLst.size()>0) {
AppliMsgSendSCOM();
}
else {
AppliMsgRecvSCOM();
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}//While
return 1 ;
}//end func

create thread but process shows up?

#include <unistd.h>
#include <stdio.h>
#include <cstring>
#include <thread>
void test_cpu() {
printf("thread: test_cpu start\n");
int total = 0;
while (1) {
++total;
}
}
void test_mem() {
printf("thread: test_mem start\n");
int step = 20;
int size = 10 * 1024 * 1024; // 10Mb
for (int i = 0; i < step; ++i) {
char* tmp = new char[size];
memset(tmp, i, size);
sleep(1);
}
printf("thread: test_mem done\n");
}
int main(int argc, char** argv) {
std::thread t1(test_cpu);
std::thread t2(test_mem);
t1.join();
t2.join();
return 0;
}
Compile it with g++ -o test test.cc --std=c++11 -lpthread
I run the program in Linux, and run top to monitor it.
I expect to see ONE process however I saw THREE.
It looks like std::thread is creating threads, why do I end up with getting processes?
Linux does not implement threads. It only has Light Weight Processes (LWP) while pthread library wraps them to provide POSIX-compatible thread interface. The main LWP creates its own address space while each subsequent thread LWP shares address space with main LWP.
Many utils, such as HTOP (which seems to be on the screenshot) by default list LWP. In order to hide thread LWPs you can open Setup (F2) -> Display Options and check Hide kernel threads and Hide userland process threads options. There is also an option to highlight threads - Display threads in different color.

What is wrong with my thread program?

I have the following code that is supposed to process ever wile with a .NEF extension.
#include <iostream>
#include <regex>
#include <pthread.h>
#include <dirent.h>
using namespace std;
void *workHorse(void*);
int main (int argc, char *argv[]){
pthread_t t1;
int rc, pos1;
DIR *dir;
struct dirent *ent;
regex e("(.*)(\\.)(NEF|nef)");
if ((dir = opendir (".")) != NULL) {
string fn1;
while ((ent = readdir (dir))!=NULL){
fn1.assign(ent->d_name);
if (regex_match ( fn1, e )){
cout<<"F :"<<fn1.c_str()<<" "<<endl;
if (rc=pthread_create( &t1, NULL, &workHorse, (void*)&fn1)){
cout<<"Error creating threads "<<rc<<endl;
exit(-1);
}
}
}
}
return 0;
}
void *workHorse(void *fileName){
int ret;
cout<<"W :"<<((string*)fileName)->c_str()<<endl;
pthread_exit(NULL);
}
There is just one file with .NEF extension in the directory. My expected output is -
F :DSC_0838.NEF
W :DSC_0838.NEF
However, I get
F :DSC_0838.NEF
W :RGBbmp.bmp
RGBbmp.bmp is another file in the same directory. What is wrong with my code? Why does it not work as expected?
The above code was compiled using -
g++ tmp.cpp -pthread --std=c++11
fn1's address is shared between the main thread and the secondary p_thread you create.
While the new thread is bootstrapping, the main thread changes the value in 'fn1' memory address, and the secondary thread reads the name of a different file (because in the main thread fn1 now has a new value).
You need to create a copy of the string you pass to the secondary thread, or you need to syncrhonize your read/write, I would recommend the former since it is way easier.
In this line:
if (rc=pthread_create( &t1, NULL, &workHorse, (void*)&fn1))
You are passing the address of fn1, the value then is changed in the main loop to some other file names, and by the time the tread comes up, it is now in RGBbmp.bmp

Pantheios write to extenal file

I looked around and I couldn't find the answer to how exactly to do this. I am trying to use Pantheios for logging and I want to write to an external file (otherwise whats the point). I am following one of the examples provided but It doesn't seem to be making the log file anywhere. Here is the code:
Edit: Also pantheios_be_file_setFilePath is returning -4 (PANTHEIOS_INIT_RC_UNSPECIFIED_FAILURE) so thats.....not helpful
#include "stdafx.h"
#include <pantheios/pantheios.hpp>
#include <pantheios/implicit_link/core.h>
#include <pantheios/implicit_link/fe.simple.h>
#include <pantheios/implicit_link/be.WindowsConsole.h>
#include <pantheios/implicit_link/be.file.h>
#include <pantheios/frontends/fe.simple.h>
#include <pantheios/backends/bec.file.h>
#include <pantheios/inserters/args.hpp>
PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("LogTest");
int _tmain(int argc, _TCHAR* argv[])
{
try
{
pantheios_be_file_setFilePath(PANTHEIOS_LITERAL_STRING("testlogforme.log"), PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_ALL);
pantheios::log(pantheios::debug, "Entering main(", pantheios::args(argc,argv, pantheios::args::arg0FileOnly), ")");
pantheios::log_DEBUG("debug yo");
pantheios::log_INFORMATIONAL("informational fyi");
pantheios::log_NOTICE("notice me!");
pantheios::log_WARNING("warning!!");
pantheios::log_ERROR("error omg");
pantheios::log_CRITICAL("critical!!!");
pantheios::log_ALERT("alert mang");
pantheios::log_EMERGENCY("EMERGENCY!!!!!");
pantheios_be_file_setFilePath(NULL, PANTHEIOS_BEID_ALL);
system("pause");
return EXIT_SUCCESS;
}
catch(std::bad_alloc&)
{
pantheios::log_ALERT("out of memory");
}
catch(std::exception& x)
{
pantheios::log_CRITICAL("Exception: ", x);
}
catch(...)
{
pantheios::puts(pantheios::emergency, "Unexpected unknown error");
}
return EXIT_FAILURE;
}
Maybe I'm not calling a method or maybe its not being saved to a good location?
It turns out that some of the examples out there for pantheios are incorrect. You DO need to call pantheios_init() even if you are in C++. Here Is the example I got to work after deleting all my code and implementing an example that works.
// Headers for main()
#include <pantheios/pantheios.hpp>
#include <pantheios/backends/bec.file.h>
// Headers for implicit linking
#include <pantheios/implicit_link/core.h>
#include <pantheios/implicit_link/fe.simple.h>
#include <pantheios/implicit_link/be.file.h>
PANTHEIOS_EXTERN_C const char PANTHEIOS_FE_PROCESS_IDENTITY[] = "testLOL";
int main()
{
if(pantheios::pantheios_init() < 0)
{
return 1;
}
pantheios::log_NOTICE("log-1"); // save until log file set
pantheios_be_file_setFilePath("mylogfile.log"); // sets log file; write "log-1" stmt
pantheios::log_NOTICE("log-2"); // write "log-2" stmt
pantheios_be_file_setFilePath(NULL); // close "mylogfile"
pantheios::log_NOTICE("log-3"); // save until log file set
pantheios_be_file_setFilePath("mylogfile2.log"); // sets log file; write "log-3" stmt
pantheios::log_NOTICE("log-4"); // write "log-4" stmt
//system("pause");
return 0;
} // closes "mylogfile2" during program closedown
I found the example on a different post on stack overflow but like I said, the built in examples do not work.