libctru  v2.4.1
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)
{
Thread threads[NUMTHREADS];
int i;
s32 prio = 0;
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())
{
u32 kDown = hidKeysDown();
if (kDown & KEY_START)
break; // break in order to return to hbmenu
// Flush and swap framebuffers
}
// 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]);
}
return 0;
}
Central 3DS header.
bool aptMainLoop(void)
Main function which handles sleep mode and HOME/power buttons - call this at the beginning of every f...
PrintConsole * consoleInit(gfxScreen_t screen, PrintConsole *console)
Initialise the console.
void gfxSwapBuffers(void)
Updates the configuration of both screens.
void gfxInitDefault(void)
Initializes the LCD framebuffers with default parameters This is equivalent to calling:
@ GFX_TOP
Top screen.
Definition: gfx.h:26
void gfxExit(void)
Deinitializes and frees the LCD framebuffers.
void gfxFlushBuffers(void)
Flushes the data cache for the current framebuffers.
#define gspWaitForVBlank()
Waits for VBlank.
Definition: gspgpu.h:151
@ KEY_START
Start.
Definition: hid.h:15
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
void hidScanInput(void)
Scans HID for input data.
Result svcGetThreadPriority(s32 *out, Handle handle)
Retrieves the priority of a thread.
#define CUR_THREAD_HANDLE
Pseudo handle for the current thread.
Definition: svc.h:133
void svcSleepThread(s64 ns)
Puts the current thread to sleep.
struct Thread_tag * Thread
libctru thread handle type
Definition: thread.h:22
Thread threadCreate(ThreadFunc entrypoint, void *arg, size_t stack_size, int prio, int core_id, bool detached)
Creates a new libctru thread.
Result threadJoin(Thread thread, u64 timeout_ns)
Waits for a libctru thread to finish (or returns immediately if it is already finished).
void threadFree(Thread thread)
Frees a finished libctru thread.
uint64_t u64
64-bit unsigned integer
Definition: types.h:24
#define U64_MAX
The maximum value of a u64.
Definition: types.h:12
int32_t s32
32-bit signed integer
Definition: types.h:28
uint32_t u32
32-bit unsigned integer
Definition: types.h:23