1

Is it possible to implement OS independent threading model of User Level Threads (ULT) in C/C++? In other words, can we break down a process logically into ULTs and dynamically make switches in between them?

mihsathe
  • 8,904
  • 12
  • 38
  • 54
  • You might want to look into what I got when I asked this: http://stackoverflow.com/questions/4298986/is-there-something-to-replace-the-ucontext-h-functions – zneak Dec 18 '10 at 07:08

6 Answers6

5

Boost.Thread offers a fair amount of abstraction for cross-platform threading.

wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
  • 2
    And in the upcoming C++0x standard, you automatically have OS-independent threads with `std::thread` – Charles Salvia Dec 18 '10 at 04:03
  • Oh, good to know this. So it will be a language feature like java? – mihsathe Dec 18 '10 at 04:05
  • 2
    @mihsathe, @Charles Saliva, It's more a library feature than a language feature. – zneak Dec 18 '10 at 04:26
  • 1
    @zneak, The C++ language standard (ISO/IEC 14882) includes the language's standard library. The updated C++ language standard will include a larger standard library. – Matthew Flaschen Dec 18 '10 at 04:52
  • @Matthew Flaschen Got me on that. But I'm pretty sure it isn't the case for Java. – zneak Dec 18 '10 at 07:06
  • Part of the threading support in C++0x is made up of core language features too though (the memory model, for example), so even if you ignored the library parts, it'd still be a language feature. :) – jalf Dec 18 '10 at 11:12
3

Just use POSIX threads. There's a reasonably conformant implementation for Windows if you need to support Windows.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Thank you for answer. But I was asking about the implementation. Is it possible to implement ULTs? – mihsathe Dec 18 '10 at 04:02
  • @mihsathes, ultimately threads and processes are a service provided by the operating system. To spawn a new thread requires access to an OS-specific API (or a wrapper library which talks to the OS) – Charles Salvia Dec 18 '10 at 04:04
  • @Charles then what's the difference in implementation of Kernel Level Thread(KLT) and User Level Thread(ULT) then? – mihsathe Dec 18 '10 at 04:07
  • 4
    There's no *portable* way to implement ULT. The closest thing is the deprecated `makecontext` interface (which depends on POSIX+XSI option and may be removed from future versions of the standard entirely). Another old popular method was setting up a custom signal-handling stack, raising a signal, and calling `setjmp` inside the signal handler to make a new "thread context". This method may work but it's an extremely ugly hack and it's explicitly frowned upon by POSIX (it's considered UB). – R.. GitHub STOP HELPING ICE Dec 18 '10 at 04:11
  • By the way, it should be noted that as of POSIX 2008, threads are no longer an optional feature in POSIX but a mandatory one, so if you're looking towards the future, anything that depends on deprecated/slated-for-removal features or implementation-specific hacks will almost surely result in *less portability* (from a practical standpoint) than just using POSIX threads. If you really want cooperative threading semantics, it's easy to implement that on top of POSIX threads using barriers or condition variables. – R.. GitHub STOP HELPING ICE Dec 18 '10 at 04:21
1

OpenMP is a nice way to handle threads for many common use cases.

arsenm
  • 2,903
  • 1
  • 23
  • 23
1

In C, one somewhat portable (it requires POSIX) user-level threading library is GNU Pth. It uses cooperation instead of preemption, and implements per-thread stack and other structures in user space. As expected, this will not provide the same performance characteristics as OS-level threading. However, it does provide some of the same abstractions.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
1

On windows 7 there is a way to implement your own User Mode Scheduler: http://msdn.microsoft.com/en-us/library/dd627187%28v=vs.85%29.aspx

This is basecally an API to make a User Mode Threads using a User Mode Scheduler and the best thing is opening process explorer and see Kernel Time 0.

Hwever I know people who implemented successfully the UMS but the microsoft reported some errors on the api . . .

DVD
  • 1,744
  • 3
  • 17
  • 34
  • 1
    How is this in any way "OS independent"? It isn't even available on all common versions of Windows! – caf Dec 18 '10 at 06:06
  • It isn't, but it his the only way to make User Mode Threads, no other OS have something like this, i dont think that POSIX are able to mix Kernel Threads with user Threads, this API does do that xD. That i know it's the only way to successful implement ums threads but i dont know it all XD. – DVD Dec 18 '10 at 12:19
1

You may have a look at ZThread is a cross platform c++ library.

Thinking in c++ vol 2 uses this library to explain multi-threading application.

Alessandro Teruzzi
  • 3,918
  • 1
  • 27
  • 41