Linux-driven core multithreading (iii)

Source: Internet
Author: User

This digest is from

Http://www.cnblogs.com/zhuyp1015/archive/2012/06/13/2548458.html

Next, this article describes another way to communicate between threads: the completion mechanism. The completion mechanism is a lightweight mechanism for inter-thread communication: One thread is allowed to tell another thread that work is done. To use completion, you need to include the header file <linux/completion.h>.

You can create a completion in the following ways:

Declare_completion (my_completion);

Or, dynamic creation and initialization:

struct completion my_completion;

Init_completion (&my_completion);

Waiting for completion is a simple thing to call: void Wait_for_completion (struct completion *c);

Note: This function makes a non-disruptive wait. If your code calls Wait_for_completion and

No one has finished the task, and the result will be a process that cannot be killed.

The completion event may be emitted by calling one of the following:

void complete (struct completion *c);

void Complete_all (struct completion *c);

If more than one thread is waiting for the same completion event, the 2 function practices are different. Complete only

Wakes up a waiting thread, while Complete_all allows all of them to continue.

Here's the implementation code that uses the completion mechanism:

#include <linux/init.h>#include<linux/module.h>#include<linux/kthread.h>#include<linux/wait.h>#include<linux/completion.h>Module_license ("Dual BSD/GPL"); Static structCompletion comp; Static structTask_struct *_tsk; Static structTask_struct *_tsk1;Static intTC =0; Static intThread_function (void*data) {       Do{PRINTK (kern_info"In thread_function thread_function:%d times \ n", TC); Wait_for_completion (&comp); //TC = 0; ///Wherever you are.PRINTK (Kern_info"Have been woke up!\n"); } while(!kthread_should_stop ()); returnTC; }    Static intThread_function_1 (void*data) {       Do{PRINTK (kern_info"In thread_function_1 thread_function:%d times\n", ++TC); if(TC = =Ten) {Complete (&comp); TC=0; } msleep_interruptible ( +); } while(!kthread_should_stop ()); returnTC; }    Static intHello_init (void) {PRINTK (Kern_info"Hello, world!\n."); Init_completion (&comp); _tsk= Kthread_run (Thread_function, NULL,"Mythread"); if(Is_err (_tsk)) {PRINTK (Kern_info"First create Kthread failed!\n"); }      Else{PRINTK (kern_info"First create Ktrhead ok!\n"); } _tsk1= Kthread_run (Thread_function_1,null,"mythread2"); if(Is_err (_tsk1)) {PRINTK (Kern_info"Second Create Kthread failed!\n"); }      Else{PRINTK (kern_info"Second Create Ktrhead ok!\n"); }      return 0; }    Static voidHello_exit (void) {PRINTK (Kern_info"Hello, exit!\n."); if(!Is_err (_tsk)) {          intRET =kthread_stop (_tsk); PRINTK (Kern_info"First thread function has stopped, return%d\n", ret); }      if(!Is_err (_TSK1)) {                   intRET =kthread_stop (_TSK1); PRINTK (Kern_info"Second thread function_1 has stopped, return%d\n", ret);  }} module_init (Hello_init); Module_exit (hello_exit);

Operation Result:

Linux-driven core multithreading (iii)

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.