Your CGI application is just a C/C++ program. With that in mind, if you already have the code that sends the email, just run it on another thread.
Depending on what you have configured on your project, you can use pthread
or boost::thread
.
Example of thread creation using using pthread
:
#include <pthread.h>
#include <iostream>
typedef struct{
// Sample parameters, use your own.
std::string subject;
std::string sender;
std::string recipient;
std::string message;
} EmailData;
void* do_send_mail(void* void_ptr)
{
EmailData* data = (EmailData*)void_ptr;
// your code that sends the email
delete data;
return NULL;
}
int main()
{
// Thread handle
pthread_t send_mail_thread;
// Your email struct
EmailData* email = new EmailData;
email->subject = "Testing email.";
email->recipient = "nobody@example.com";
email->sender = "youremail@example.com";
email->message = "You just won one billion dollars!";
if(pthread_create(&send_mail_thread, NULL, do_send_mail, (void*)email)) {
std::cout << "Failed to create thread that sends the email." << std::endl;
return 1;
}
// Remove this "join" in your CGI application, it waits for the thread to
// finish (which will make your client wait just like it does now)
pthread_join(send_mail_thread, NULL);
return 0;
}
Example of thread creation using using boost::thread
:
#include <boost/thread.hpp>
typedef struct{
// Sample parameters, use your own.
std::string subject;
std::string sender;
std::string recipient;
std::string message;
} EmailData;
void do_send_mail(EmailData& email)
{
// your code that sends email here
}
int main(int argc, char* argv[])
{
EmailData email;
email.subject = "Testing email.";
email.recipient = "nobody@example.com";
email.sender = "youremail@example.com";
email.message = "You just won one billion dollars!";
boost::thread email_thread(boost::bind<void>(do_send_mail, email));
// Remove this "join" in your CGI application, it waits for the thread to
// finish (which will make your client wait just like it does now)
email_thread.join();
return 0;
}
Example of thread creation using using boost::thread
(with lambda):
#include <boost/thread.hpp>
typedef struct{
// Sample parameters, use your own.
std::string subject;
std::string sender;
std::string recipient;
std::string message;
} EmailData;
int main(int argc, char* argv[])
{
EmailData email;
email.subject = "Testing email.";
email.recipient = "nobody@example.com";
email.sender = "youremail@example.com";
email.message = "You just won one billion dollars!";
boost::thread email_thread(boost::bind<void>([](EmailData& email)->void{
// your code that sends email here
}, email));
// Remove this "join" in your CGI application, it waits for the thread to
// finish (which will make your client wait just like it does now)
email_thread.join();
return 0;
}