📄 sample-twothreads.html
字号:
<!-- Copyright (C) 2003 Red Hat, Inc. --><!-- This material may be distributed only subject to the terms --><!-- and conditions set forth in the Open Publication License, v1.0 --><!-- or later (the latest version is presently available at --><!-- http://www.opencontent.org/openpub/). --><!-- Distribution of the work or derivative of the work in any --><!-- standard (paper) book form is prohibited unless prior --><!-- permission is obtained from the copyright holder. --><HTML><HEAD><TITLE>A Sample Program with Two Threads</TITLE><meta name="MSSmartTagsPreventParsing" content="TRUE"><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="eCos User Guide"HREF="ecos-user-guide.html"><LINKREL="UP"TITLE="Building and Running Sample Applications"HREF="building-and-running-sample-appliations.html"><LINKREL="PREVIOUS"TITLE="Building and Running Sample Applications"HREF="building-and-running-sample-appliations.html"><LINKREL="NEXT"TITLE="More Features — Clocks and AlarmHandlers"HREF="clocks-and-alarm-handlers.html"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">eCos User Guide</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="building-and-running-sample-appliations.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 13. Building and Running Sample Applications</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="clocks-and-alarm-handlers.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SAMPLE-TWOTHREADS">A Sample Program with Two Threads</H1><P>Below is a program that uses some of <SPANCLASS="PRODUCTNAME">eCos</SPAN>' system calls. Itcreates two threads, each of which goes into an infinite loop in whichit sleeps for a while (using cyg_thread_delay()). This code is foundin the file <TTCLASS="FILENAME">twothreads.c</TT>in the examples directory.</P><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN871"><SPANCLASS="PRODUCTNAME">eCos</SPAN> two-threaded program listing</H2><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">#include <cyg/kernel/kapi.h>#include <stdio.h>#include <math.h>#include <stdlib.h>/* now declare (and allocate space for) some kernel objects, like the two threads we will use */cyg_thread thread_s[2]; /* space for two thread objects */char stack[2][4096]; /* space for two 4K stacks *//* now the handles for the threads */cyg_handle_t simple_threadA, simple_threadB;/* and now variables for the procedure which is the thread */cyg_thread_entry_t simple_program;/* and now a mutex to protect calls to the C library */cyg_mutex_t cliblock;/* we install our own startup routine which sets up threads */void cyg_user_start(void){ printf("Entering twothreads' cyg_user_start() function\n"); cyg_mutex_init(&cliblock); cyg_thread_create(4, simple_program, (cyg_addrword_t) 0, "Thread A", (void *) stack[0], 4096, &simple_threadA, &thread_s[0]); cyg_thread_create(4, simple_program, (cyg_addrword_t) 1, "Thread B", (void *) stack[1], 4096, &simple_threadB, &thread_s[1]); cyg_thread_resume(simple_threadA); cyg_thread_resume(simple_threadB);}/* this is a simple program which runs in a thread */void simple_program(cyg_addrword_t data){ int message = (int) data; int delay; printf("Beginning execution; thread data is %d\n", message); cyg_thread_delay(200); for (;;) { delay = 200 + (rand() % 50); /* note: printf() must be protected by a call to cyg_mutex_lock() */ cyg_mutex_lock(&cliblock); { printf("Thread %d: and now a delay of %d clock ticks\n", message, delay); } cyg_mutex_unlock(&cliblock); cyg_thread_delay(delay); }}</PRE></TD></TR></TABLE><P>When you run the program (by typing <BCLASS="COMMAND">continue</B> atthe (<SPANCLASS="emphasis"><ICLASS="EMPHASIS">gdb</I></SPAN>) prompt) the output should look likethis:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">Starting program: <TTCLASS="REPLACEABLE"><I>BASE_DIR</I></TT>/examples/twothreads.exeEntering twothreads' cyg_user_start()functionBeginning execution; thread data is 0Beginning execution; thread data is 1Thread 0: and now a delay of 240 clock ticksThread 1: and now a delay of 225 clock ticksThread 1: and now a delay of 234 clock ticksThread 0: and now a delay of 231 clock ticksThread 1: and now a delay of 224 clock ticksThread 0: and now a delay of 249 clock ticksThread 1: and now a delay of 202 clock ticksThread 0: and now a delay of 235 clock ticks</PRE></TD></TR></TABLE><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Note: </B>When running in a simulator the delays might be quite long. On a hardware board (where the clockspeed is 100 ticks/second) the delays should average toabout 2.25 seconds. In simulation, the delay will depend on thespeed of the host processor and will almost always be much slower thanthe actual board. You might want to reduce the delay parameter when runningin simulation.</P></BLOCKQUOTE></DIV><P><AHREF="sample-twothreads.html#FIGURE-TWOTHREADS-WITH-SIMPLE-PRINTS">Figure 13-1</A> shows how thismultitasking program executes. Note that apart from the threadcreation system calls, this program also creates and uses a<SPANCLASS="emphasis"><ICLASS="EMPHASIS">mutex</I></SPAN> for synchronizationbetween the <TTCLASS="FUNCTION">printf()</TT> calls in the twothreads. This is because the C library standard I/O (by default) isconfigured not to be thread-safe, which means that if more than onethread is using standard I/O they might corrupt each other. This isfixed by a mutual exclusion (or <SPANCLASS="emphasis"><ICLASS="EMPHASIS">mutex</I></SPAN>) lockoutmechanism: the threads do not call <TTCLASS="FUNCTION">printf()</TT> until<TTCLASS="FUNCTION">cyg_mutex_lock()</TT> has returned, which only happenswhen the other thread calls<TTCLASS="FUNCTION">cyg_mutex_unlock()</TT>.</P><P>You could avoid using the mutex by configuring the C library tobe thread-safe (by selecting the component<TTCLASS="LITERAL">CYGSEM_LIBC_STDIO_THREAD_SAFE_STREAMS</TT>).</P><DIVCLASS="FIGURE"><ANAME="FIGURE-TWOTHREADS-WITH-SIMPLE-PRINTS"><P><B>Figure 13-1. Twothreads with simple print statements after random delays</B></P><P><IMGSRC="pix/twothreads2.png"></P></DIV></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="building-and-running-sample-appliations.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ecos-user-guide.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="clocks-and-alarm-handlers.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Building and Running Sample Applications</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="building-and-running-sample-appliations.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">More Features — Clocks and AlarmHandlers</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -