Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make thread in C without using POSIX library <pthread.h> [closed]

I want to implement the multiple threading in C without using any of the POSIX library. Any help would be appreciated.

Not : Don't use fork() or vfork().

like image 327
Rahul Sharma Avatar asked Nov 08 '12 05:11

Rahul Sharma


1 Answers

See:

  • setjmp,
  • longjmp,
  • sigaltstack,
  • sigaltstack

for UNIX like systems.

Also see:

  • getcontext and setcontext,
  • and more generally ucontext

for BSDs and modern UNIXes.

This page gives many examples of barebone implementations using these primitives and more.

You can use atomic instructions to implement the locking primitives (mutex, semaphores).

I also suggest looking at actual implementations of userland thread libraries to get some hints. See this page which gives a list of implementations for Linux.

Finally, you might want to get some information on coroutines and perhaps trampolines, although the later isn't as closely related.

like image 192
didierc Avatar answered Oct 24 '22 22:10

didierc