ThreadFuck

From Esolang
Jump to navigation Jump to search

ThreadFuck is an esolang invented by User:None1, inspired by Bukkake and Brainfuck.

Subprograms

ThreadFuck programs consists of lines called subprograms. If the program is empty, it errors. Otherwise the main thread will execute the first line. When a thread's instruction pointer reaches the end of the subprogram, the thread terminates. When there are no threads left, the program terminates.

Threads

Unlike brainfuck, ThreadFuck uses threads. Each thread has a private tape, a private memory pointer, a private progam selector and a private instruction pointer. There is also a public memory pointer and a public tape.

At the start of a thread, all private tape cells are zero, the private program selector selects the subprogram of the thread, the private instruction pointer points to the first character of the subprogram of the thread.

At the start of the program, all public tape cells are zero.

A thread can only control one memory pointer (public/private) at a time, the memory pointer it controls currently is called active pointer. At the start of a thread, the active pointer is the private memory pointer.

Commands

It has all the commands in brainfuck, and 5 extra commands.

Command Description
> Move the active pointer to the right
< Move the pointer to the left
+ Increment the memory cell at the active pointer
- Decrement the memory cell at the active pointer
. Output the character signified by the cell at the active pointer
, Input a character and store it in the cell at the active pointer
[ Jump past the matching ] if the cell at the active pointer is 0
] Jump back to the matching [ if the cell at the active pointer is nonzero
~ Switch active pointer
! Create a thread that executes the subprogram selected by the program selector of this thread
^ If the program selector is selecting the first subprogram, select the last subprogram, otherwise select the subprogram at the next line
v If the program selector is selecting the last subprogram, select the first subprogram, otherwise select the subprogram at the previous line
* Wait until all the other threads terminate

Examples

Fork bomb

!!

Undefined output 1

v!!
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

This is to demonstrate ThreadFuck's threads, it creates two threads printing Hello World! at the same time, so the output is undefined.

On the interpreter, it prints stuff like this:

HelHleo World!
llo World!

Undefined output 2

v!!
~++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

This is to demonstrate a wrong way of sharing data between threads, in this case, both threads are operating on the same tape and the same pointer, so it is thread-unsafe and causes undefined behavior.

On the interpreter, it prints stuff like this:

HeÔÔ×@¯×ÚÔÌAÉÐÐÓA®ÓÖÐÈB

Deadlock

v!!
*

In this program, the main thread creates two threads and then terminates, the other two threads wait for the other to terminate, causing deadlock.

Lock

v!*++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

The main thread creates another thread, then waits for the other thread to print Hello World! then terminate, then the main thread prints Hello World! and terminates, too, so it always prints:

Hello World!
Hello World!

Lock 2

~+v!v!
+[>~[]~++++++++[>++++++++<-]>+.+.+.[-]<<~>-<+~]
+[>~>[]<~++++++++[>++++++++<-]>++++.+.+.[-]<<~->+<~]

The main thread creates a lock (stored in the public tape) and two threads, inintially, thread 2 holds the lock. Thread 2 prints DEF while thread 1 waits for the lock, then thread 2 passes the lock to thread 1, then thread 1 prints ABC and passes the lock to thread 2, this goes on infinitely, so it always prints:

DEFABCDEFABC...

Computation class

It is Turing complete, because any brainfuck program not containing line feeds or the 5 extra characters work the same.

External resources