C-Language under multi-threading

Source: Internet
Author: User

Text: C language under multithreading

"Problem" created 10 threads, 10 threads common to a thread body, created as follows:intt1=0,t2=1,t3=2,t4=3,t5=4,t6=5,t7=6,t8=7,t9=8,t10=9; int*one=&t1,*two=&t2,*three=&t3,*four= &t4,*five=&t5,*six=&t6,*seven=&t7,*eight=&t8,*nine=&t9,*ten=&t10;Thread[3]=createthread (Null,0,processqueue,one,0,null); Thread[4]=createthread (Null,0,processqueue,two,0,null); Thread[5]=createthread (Null,0,processqueue,three,0,null); Thread[6]=createthread (NULL,0,processQueue,four,0, NULL); Thread[7]=createthread (null,0,processqueue,five,0,null); Thread[8]=createthread (NULL,0,processQueue,six,0 , NULL); Thread[9]=createthread (null,0,processqueue,seven,0,null); Thread[10]=createthread (NULL,0,processQueue, Eight,0,null); Thread[11]=createthread (null,0,processqueue,nine,0,null); Thread[12]=createthread (NULL,0, Processqueue,ten,0,null);D word WINAPI processqueue (LPVOID LP) {//Note that when reading the queue, be sure to free space. int i=* ((int *) LP);p rintf ("%d\n", I); return 0;} According to normal thinking, the results of this display should be from 0-9 (of course, the order may vary), but the unknown reason, when displayed, should have been called 10 times, but it will show the 11,12,13 second unstable results "such as: 2 1 0 3 5 4 6 6 4 7 89", because of this problem, I had a whole day with Yang brother, directly touch the head, Zhen elder brother also has 508 of the gang is also to it to have no.
Remember to have seen before, in the use of the Windows class Library, the C language provides two ways of multithreading, one is CreateThread, the other is _beginthreadex. Or else it would be a different approach. So ran to 508 borrowed a C programming, carefully read the _beginthreadex function, it said by default, VC + + in the C + + runtime library does not support the function, because the standard C run-time library is no multi-threading concept. So, we have to set up the VC.Select the Codegeneration category from the user Run-time in the combo box corresponding to the category project--setting->c/c++-> Select Multithreadeddll in the Library Mix ManiaYou can do it.

C-Language under multi-threading

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.