---
name: "ns_thread"
section: "3"
platform: "linux"
package: "aolserver4"
version: "4.0.10-7"
release: "Debian 4.0 (Etch)"
platforms: ["linux"]
url: "https://new.carta.tech/ns_thread"
---

# ns\_thread(3)

commands

```

```

## NAME

ns\_thread - commands

## SYNOPSIS

**ns\_thread begin** *script*

**ns\_thread begindetached** *script*

**ns\_thread get**

**ns\_thread getid**

**ns\_thread wait** *tid*

**ns\_thread yield**

```

```

## DESCRIPTION

**ns\_thread begin:**

**begins a new thread which evaluates the specified script and then exits. It returns a thread ID that must eventually be passed to ns\_thread wait. (Failing to call ns\_thread wait will eventually result in no new threads being created.)**

**ns\_thread begindetached:**

**begins a detached thread that doesn't have to be (and can't be) waited for.**

**ns\_thread get:**

**gets the thread ID of the current thread. The result is a thread ID that can be passed to ns\_thread wait and may look something like "tid532".**

**ns\_thread getid:**

**gets the thread integer number for the current thread. The result is a small integer used for identifying threads is a human-readable way, such as "1" or "1120", for example.**

**ns\_thread wait:**

**waits for the specified thread to exit. The tid argument is a thread ID returned by ns\_thread begin or ns\_thread get.**

**ns\_thread yield:**

**causes the current thread to yield.**

## EXAMPLES

**This example is similar to the example under the ns\_sockselect function of connecting to the 10 servers and waiting to service them with the ns\_sockselect command. In this case, though, each connection gets it's own thread.**

**# This is the procedure which is evaluated for each thread and** **# handles a single connection to host number $i**

**proc getpage {i} {**\
     **global pages**\
     **# new thread will start here - first connect to host**\
     **set host \[format "www%2d.foo.com" $i\]**\
     **set fds \[ns\_sockopen $host 80**\
     **set r \[lindex $fds 0\]**\
     **set w \[lindex $fds 1\]**\
     **# next, send request**\
     **puts $w "GET /index.htm HTTP/1.00r"**\
     **flush $w**\
     **# then read page**\
     **set pages($i) \[read $r\]**\
     **# and close sockets**\
     **close $w**\
     **close $r**\
     **# thread goes away here and other threads waiting**\
     **# on ns\_thread wait will wakeup** **}**

**# Here's the loop which creates the threads which run getpage.** **for {set i 1} {$i \< 9} {incr i} {**\
     **set tids($i) \[ns\_thread begin "getpage $i"\]** **}**

**# wait for the threads to exit and then process the pages** **for {set i 1} {$i \< 9} {incr i} {**\
     **ns\_thread wait $tids($i)**\
     **# output page**\
     **... process the page in $pages($i) put there by other thread ...** **}**

**Note that the code here is much simpler to follow than the ns\_sockselect example; that's the benefit of multithreaded programming. However, it uses more resources as threads need to be created and initialized. This can be a problem if you plan to create many threads.**

## SEE ALSO

## KEYWORDS

**threads**
