threads/thread-basic/source/main.c
#include <string.h>
#include <stdio.h>
#include <3ds.h>
#define NUMTHREADS 3
#define STACKSIZE (4 * 1024)
volatile bool runThreads = true;
void threadMain(void *arg)
{
u64 sleepDuration = 1000000ULL * (u32)arg;
int i = 0;
while (runThreads)
{
printf("thread%d says %d\n", (int)arg, i++);
svcSleepThread(sleepDuration);
}
}
int main(int argc, char** argv)
{
gfxInitDefault();
consoleInit(GFX_TOP, NULL);
Thread threads[NUMTHREADS];
int i;
s32 prio = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
printf("Main thread prio: 0x%lx\n", prio);
for (i = 0; i < NUMTHREADS; i ++)
{
// The priority of these child threads must be higher (aka the value is lower) than that
// of the main thread, otherwise there is thread starvation due to stdio being locked.
threads[i] = threadCreate(threadMain, (void*)((i+1)*250), STACKSIZE, prio-1, -2, false);
printf("created thread %d: %p\n", i, threads[i]);
}
// Main loop
while (aptMainLoop())
{
gspWaitForVBlank();
hidScanInput();
u32 kDown = hidKeysDown();
if (kDown & KEY_START)
break; // break in order to return to hbmenu
// Flush and swap framebuffers
gfxFlushBuffers();
gfxSwapBuffers();
}
// tell threads to exit & wait for them to exit
runThreads = false;
for (i = 0; i < NUMTHREADS; i ++)
{
threadJoin(threads[i], U64_MAX);
threadFree(threads[i]);
}
gfxExit();
return 0;
}
3ds.h
Central 3DS header.
aptMainLoop
bool aptMainLoop(void)
Main function which handles sleep mode and HOME/power buttons - call this at the beginning of every f...
consoleInit
PrintConsole * consoleInit(gfxScreen_t screen, PrintConsole *console)
Initialise the console.
gfxSwapBuffers
void gfxSwapBuffers(void)
Updates the configuration of both screens.
gfxInitDefault
void gfxInitDefault(void)
Initializes the LCD framebuffers with default parameters This is equivalent to calling:
GFX_TOP
@ GFX_TOP
Top screen.
Definition: gfx.h:26
gfxExit
void gfxExit(void)
Deinitializes and frees the LCD framebuffers.
gfxFlushBuffers
void gfxFlushBuffers(void)
Flushes the data cache for the current framebuffers.
gspWaitForVBlank
#define gspWaitForVBlank()
Waits for VBlank.
Definition: gspgpu.h:151
KEY_START
@ KEY_START
Start.
Definition: hid.h:15
hidKeysDown
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
hidScanInput
void hidScanInput(void)
Scans HID for input data.
svcGetThreadPriority
Result svcGetThreadPriority(s32 *out, Handle handle)
Retrieves the priority of a thread.
CUR_THREAD_HANDLE
#define CUR_THREAD_HANDLE
Pseudo handle for the current thread.
Definition: svc.h:133
svcSleepThread
void svcSleepThread(s64 ns)
Puts the current thread to sleep.
Thread
struct Thread_tag * Thread
libctru thread handle type
Definition: thread.h:22
threadCreate
Thread threadCreate(ThreadFunc entrypoint, void *arg, size_t stack_size, int prio, int core_id, bool detached)
Creates a new libctru thread.
threadJoin
Result threadJoin(Thread thread, u64 timeout_ns)
Waits for a libctru thread to finish (or returns immediately if it is already finished).
threadFree
void threadFree(Thread thread)
Frees a finished libctru thread.
u64
uint64_t u64
64-bit unsigned integer
Definition: types.h:24
U64_MAX
#define U64_MAX
The maximum value of a u64.
Definition: types.h:12
s32
int32_t s32
32-bit signed integer
Definition: types.h:28
u32
uint32_t u32
32-bit unsigned integer
Definition: types.h:23