A multi-threaded problem caused by a murder-(the code snippet is completed, the child process is not completed resulting in a fragment error)

Source: Internet
Author: User

Today, there is a problem, GDB executes the program completely no problem, but the direct execution will be section error, baffled its solution, all kinds of entanglements, a variety of search engines have tried again, no fruit! Then the problem was dug out by myself.

Look at the code below:

int Tasksendcontrol () {pthread_t prov_thread[client_num];int prov[client_num];  for (int i=0; i< client_num; i++)  {  Prov[i] = i;  if (Pthread_create (&prov_thread[i], NULL, Provtasksend, (void*) &prov[i])! = 0)  {  cout << " Tasksendcontrol "<< Endl;gv_alert. Insertalert (Main,1,gv_localname,gv_localip,exception_db_error, Codewhere, "");    Exit ( -1);}  } return 0;} void *provtasksend (void * prov) {int provid = * (int *) Prov; C_node *tmpnode;  while (1) {... Omit program more//Issued idling sleep (2);}}


Do you think there's going to be a problem? I didn't think there would be any problem at the beginning, I will be in the sub-thread in the loop set idling, the program can run normally, so I always think it is a while loop inside the program has a problem, but how can not find out!

I gdb run the time is also normal, which makes me very crash, unable to debug!

I came back to think of a paragraph error must be a pointer access problem, must be illegal.

After careful analysis, the code snippet exits when the thread is created, if the thread has not finished creating it (while the program in the while loop is more), then the temporary variable is released, so that the segment is wrong.

To modify a program:

int Tasksendcontrol () {static pthread_t prov_thread[client_num];static int prov[client_num];  for (int i=0; i< client_num; i++)  {  Prov[i] = i;  if (Pthread_create (&prov_thread[i], NULL, Provtasksend, (void*) &prov[i])! = 0)  {  cout << " Tasksendcontrol "<< Endl;gv_alert. Insertalert (Main,1,gv_localname,gv_localip,exception_db_error, Codewhere, "");    Exit ( -1);}  } return 0;}

Sure enough, the program runs normally!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.