[Simple multi-thread control] _ [simulate anti-virus software thread control]

Source: Internet
Author: User

Scenario:
1. There are currently two common statuses in the Business Database: Start and Stop. If you need to add a pause status. Thread control is required.

Requirements:
1. Refer to anti-virus to start anti-virus, continue anti-virus, stop anti-virus, stop anti-virus four States to design a multi-thread program. The main thread controls the pause, continue, and stop of the working thread. Note: Only two threads are allowed. Note that the worker thread can only be detach but not join because the main thread cannot be suspended.
2. The pthread must be used for implementation (the. net built-in thread library can be used for website R & D), and the win32 version can be implemented at your own time. Write a console or interface program. The interface program can imitate the example. The console program can enter a number to replace the button. For example:
1. Start scanning.
2. Continue scanning-> pause scanning switching.
3. Stop scanning.

 

# Include <stdio. h> # include <iostream> # include "pthread. h "typedef enum ButtonType {kStart = 0, // start kPause, // stop kStop, // stop kContinue, // continue} ButtonType; typedef struct ThreadData {ButtonType type _; pthread_mutex_t mutex _; pthread_cond_t mian_cv _; pthread_cond_t work_cv _;} ThreadData; void * WorkThread (void * data) {ThreadData * user_data = (ThreadData *) data; while (1) {switch (user_data-> type _) {case kPause: {Sleep (1 000); pthread_mutex_lock (& user_data-> mutex _); // tell the main thread to continue pthread_cond_signal (& user_data-> mian_cv _); // The thread blocks pthread_cond_wait (& user_data-> work_cv _, & user_data-> mutex _); pthread_mutex_unlock (& user_data-> mutex _); if (user_data-> type _ = kContinue) {pthread_mutex_lock (& user_data-> mutex _); pthread_cond_signal (& user_data-> mian_cv _); pthread_mutex_unlock (& user_data-> mutex _);} // pauseif (user_data-> type _ = kStop) {p Thread_mutex_lock (& user_data-> mutex _); pthread_cond_signal (& user_data-> mian_cv _); Combine (& user_data-> mutex _); return 0;} break;} case kStop: {// tell the main thread to continue pthread_mutex_lock (& user_data-> mutex _); pthread_cond_signal (& user_data-> mian_cv _); pthread_mutex_unlock (& user_data-> mutex _); // End Process return 0;} default: {break; }}// Sleep (1000) ;}} void PrintHelp () {std :: cout <"0 start" <std: endl; std: cout <"1 pause <-> Continue "<std: endl; std: cout <" 2 stop "<std: endl; std :: cout <"q quit" <std: endl ;} // The start state can only be one time // paused <-> continue-> stopped // when the working thread is in the correct state, the main thread can work. // The main thread controls the status of the worker thread. int main (int argc, char * argv []) {ThreadData data; data. type _ = kStop; pthread_cond_init (& data. mian_cv _, NULL); pthread_cond_init (& data. work_cv _, NULL); pthread_mutex_init (& data. mutex _, NULL); // print the help information PrintHelp (); int value; while (1) {value = getchar (); switch (Value) {case '0': // 0 start {if (data. type _! = KStop) {std: cout <"already start! "<Std: endl; break;} std: cout <" start "<std: endl; data. type _ = kStart; pthread_t id; pthread_create (& id, NULL, WorkThread, (void *) & data); pthread_detach (id); break;} case '1 ': // 1 pause <-> continue {if (data. type _ = kStop) {std: cout <"not start! "<Std: endl; break;} pthread_mutex_lock (& data. mutex _); if (data. type _ = kPause) {std: cout <"cotinue" <std: endl; data. type _ = kContinue; pthread_cond_signal (& data. work_cv _);} else {std: cout <"pause" <std: endl; data. type _ = kPause;} pthread_cond_wait (& data. mian_cv _, & data. mutex _); pthread_mutex_unlock (& data. mutex _); break;} case '2': // 2 stop {if (data. type _ = kStop) {std: cout <"already finish! "<Std: endl; break;} std: cout <" stop "<std: endl; pthread_mutex_lock (& data. mutex _); pthread_cond_signal (& data. work_cv _); data. type _ = kStop; pthread_cond_wait (& data. mian_cv _, & data. mutex _); pthread_mutex_unlock (& data. mutex _); break;} case 'q': // quitbreak; default: break;} if (value = 'q') {break;} pthread_cond_destroy (& data. mian_cv _); pthread_cond_destroy (& data. work_cv _); pthread_mutex_destroy (& data. mutex _); return 0 ;}

 

 

References:

Http://blog.csdn.net/ffilman/article/details/4871920

 

 

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.