You have a significant scope-lifetime issue in this code. You're sending a data block to your threads that is created/destroyed with each loop iteration. Leaving out the side-lines:
// thread create
int tmp_num = 3;
for(i = 0; i < tmp_num; i++)
{
pthread_t tmp_thread_handler;
struct thread_data tmp_thread_info;
// --- snip ----
rc = pthread_create(&tmp_thread_handler, NULL, test_fun1, (void *)&tmp_thread_info);
// --- snip ----
}
Whether the resulting block is copied into the underlying info vectors or not is not relevant. Once the loop slings around, the object is destroyed, and with it the std::string
member variable ip
. It may reside in the same memory space (the struct), but the reallocation of the string almost certainly will not. In other words, you're invoking undefined behavior.
A possible resolution to this is to use a smart pointer for the structure, passing its get()
member to the thread, and pushing said-same smart pointer into your info vector ( retooled for smart pointers). I personally prefer a simpler approach, namely allocating both vectors before the threads are kicked off, thereby fixing their "innards" for by-address usage. From there you can send the structure address since the vector holds it for you:
// thread create
size_t tmp_num = 3;
m_thread.resize(tmp_num);
threads.resize(tmp_num);
for(size_t i = 0; i < tmp_num; i++)
{
m_thread[i].thread_id = i;
m_thread[i].ip = "127.0.0.1";
m_thread[i].port = 8001;
m_thread[i].timeout = 100;
rc = pthread_create(&threads[i], NULL, test_fun1, &m_thread[i]);
if (rc != 0)
return -1;
}
Regarding other issues, in your thread proc this doesn't compile:
char tmp_buf[1024] = '\0';
But that's pretty minor compared to the undefined behavior you're having.