Advertisement
Guest User

Untitled

a guest
Dec 20th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <thread>
  3. #include <mysql/mysql.h>
  4.  
  5. const char *mysql_host = "********";
  6. unsigned the_mysql_port = 13306;
  7. const char *mysql_db = "********";
  8. const char *mysql_user = "********";
  9. const char *mysql_pass = "********";
  10. const char *mysql_ssl_cert = "/usr/local/etc/certs/devmysql-client.cert";
  11. const char *mysql_ssl_key = "/usr/local/etc/certs/devmysql-client.key";
  12.  
  13. void thread_func()
  14. {
  15.     if (mysql_thread_init()) {
  16.         fprintf(stderr, "mysql_thread_init\n");
  17.         abort();
  18.     }
  19.  
  20.     MYSQL* mysql = mysql_init(NULL);
  21.     if (mysql == NULL) {
  22.         fprintf(stderr, "mysql_init\n");
  23.         abort();
  24.     }
  25.  
  26.     if (mysql_ssl_set(mysql, mysql_ssl_key, mysql_ssl_cert, NULL, NULL, NULL)) {
  27.         fprintf(stderr, "mysql_ssl_set\n");
  28.         abort();
  29.     }
  30.  
  31.     if (mysql_real_connect(mysql, mysql_host, mysql_user, mysql_pass, mysql_db, the_mysql_port, NULL, 0) == NULL) {
  32.         fprintf(stderr, "mysql_real_connect, Error: %s\n", mysql_error(mysql));
  33.     } else {
  34.         fprintf(stderr, "Success!\n");
  35.     }
  36.  
  37.     sleep(1);
  38.  
  39.     mysql_close(mysql);
  40.  
  41.     mysql_thread_end();
  42. }
  43.  
  44. int main()
  45. {
  46.     mysql_library_init(0, NULL, NULL);
  47.  
  48.     const size_t THREAD_COUNT = 8;
  49.     std::thread threads[THREAD_COUNT];
  50.     for (size_t i = 0; i < THREAD_COUNT; i++) {
  51.         threads[i] = std::thread(thread_func);
  52.     }
  53.  
  54.     for (size_t i = 0; i < THREAD_COUNT; i++) {
  55.         threads[i].join();
  56.     }
  57.  
  58.     mysql_library_end();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement