找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 5759|回复: 4

多线程怎么用?

[复制链接]
发表于 2012-2-6 20:57:46 | 显示全部楼层 |阅读模式
如何使用多线程?请教一下,本人新手{:soso_e113:}
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
发表于 2012-2-6 21:17:33 | 显示全部楼层
你会做双线程序才问这个问题的吧,你想想,把双线程序做成模块,再把模块做成双线程序……以此类推,每加一的模块,就多一条,多线程序的效果不就达到了?
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2012-2-6 21:54:00 | 显示全部楼层
underspeed 发表于 2012-2-6 21:17
你会做双线程序才问这个问题的吧,你想想,把双线程序做成模块,再把模块做成双线程序……以此类推,每加一 ...

。。。。好吧,我在nxt-g里面用过三线,robotc也可以通过函数实现多线程,想问问如何在nxc中启动多线程
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2012-2-7 00:02:34 | 显示全部楼层
Tasks
Since the NXT supports multi-threading, a task in NXC directly corresponds to an NXT thread.

Tasks are defined using the task keyword with the syntax shown in the code sample below.

task name()
{
   // the task's code is placed here
}
The name of the task may be any legal identifier. A program must always have at least one task - named "main" - which is started whenever the program is run. The body of a task consists of a list of statements.

You can start and stop tasks with the start and stop statements, which are discussed below. However, the primary mechanism for starting dependant tasks is scheduling them with either the Precedes or the Follows API function.

The StopAllTasks API function stops all currently running tasks. You can also stop all tasks using the Stop function. A task can stop itself via the ExitTo function. Finally, a task will stop itself simply by reaching the end of its body.

In the code sample below, the main task schedules a music task, a movement task, and a controller task before exiting and allowing these three tasks to start executing concurrently. The controller task waits ten seconds before stopping the music task, and then waits another five seconds before stopping all tasks to end the program.

task music() {
   while (true) {
     PlayTone(TONE_A4, MS_500);
     Wait(MS_600);
   }
}

task movement() {
   while (true) {
     OnFwd(OUT_A, Random(100));
     Wait(Random(SEC_1));
   }
}

task controller() {
   Wait(SEC_10);
   stop music;
   Wait(SEC_5);
   StopAllTasks();
}

task main() {
   Precedes(music, movement, controller);
}

--------------------------------------------------------------------------------

摘自官方手册
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2012-3-6 11:20:22 | 显示全部楼层
mark_多线程
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

手机版|中文乐高 ( 桂ICP备13001575号-7 )

GMT+8, 2024-11-21 20:50 , Processed in 0.100170 second(s), 19 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表