Windows C + + multitasking concurrent execution design

Source: Internet
Author: User
Tags semaphore

1, problem description

(1) The service side is responsible for issuing the task, and each time a task issued, issued a number of tasks are not fixed (through the issued interface check issued), but the total number of <=4.
(2) The client is responsible for receiving the task and will loop execution after receiving the task, unless it stops executing after receiving the stop instruction.
The instruction types are divided into:
1) Heartbeat instruction--for keepalive connection;
2) task start instruction--Used to start the task;
3) Mission abort instruction--Used to end the task;

2. Detailed Design
(1) The client is running concurrently, and the same interface is invoked to pass different parameter implementations, so multi-threaded synchronization is required.
The number of threads is determined by the client socket receiving information, but the total number will be <=4 and may be expanded in the future.
Therefore, the semaphore semaphore is used here to synchronize.

(2) The same code snippet, which causes an exception to avoid an access violation. You need to use the critical Code snippet Critical section for mutually exclusive access.

3. Analog Code

#include <stdio.h>#include <process.h>#include <windows.h>LongG_nnum =0;unsigned int__stdcall Fun (void*PPM);Const intg_imaxthreadcnt =4;//Client thread Count//Semaphore vs. critical segmentHANDLE G_hthreadparam; Critical_section G_csthreadcode;unsigned int__stdcall Fun (void*PPM) {intNthreadnum = * (int*) PPM; EnterCriticalSection (&g_csthreadcode); while(1) {++g_nnum;printf ("Thread no is%d global resource value is%d\n", Nthreadnum, G_nnum); ReleaseSemaphore (G_hthreadparam,1,NULL);//Signal volume + +LeaveCriticalSection (&g_csthreadcode); Sleep ( +);//Simulation Execution}return 0;}intTestmythread () {//Initialize semaphores and critical segments//Current 0 resources, maximum allowed 1 simultaneous accessG_hthreadparam = CreateSemaphore (NULL,0,1,NULL); InitializeCriticalSection (&g_csthreadcode); HANDLE hthread[g_imaxthreadcnt] = {NULL}; G_nnum =0;inti =0; while(I < g_imaxthreadcnt) {Hthread[i] = (HANDLE) _beginthreadex (NULL,0, Fun, &i,0,NULL); WaitForSingleObject (G_hthreadparam, INFINITE);//wait for the semaphore >0++i;}//wait for all threads to endWaitForMultipleObjects (g_imaxthreadcnt, Hthread,TRUE, INFINITE);//Destroy the semaphore and key segmentDeleteCriticalSection (&g_csthreadcode); CloseHandle (G_hthreadparam); for(i =0; i < g_imaxthreadcnt; i++) {CloseHandle (hthread[i]);}return 0;}


4. Summary
1) The actual project is much more complicated than this, involving socket data receiving and parsing, loop execution and so on.
It is better to loop through the internal implementations of the threads, so that you do not have to loop the calling thread and incur unnecessary overhead.
2) in the heart to have a block diagram content , and think about how to do, multi-test to verify the execution results and expectations, encoding is fully consistent.
3) APIs are often not familiar, be sure to MSDN clear the actual meaning of each field, avoid "self-righteous", affect the overall situation.

2015-8-30 am11:58 at home in front of the bed

Ming Yi World
Reprint please indicate source, original address: http://blog.csdn.net/laoyang360/article/details/48103473
If you feel this article is helpful, please click on the ' top ' support, your support is I insist on writing the most power, thank you!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Windows C + + multitasking concurrent execution design

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.