Preliminary. ThinkMeta ConcurrentTasks and this documentation are preliminary; everything is subject to change.

Threads as tasks

The synchronization primitives wait by suspending the calling task. An ordinary OS thread — the main thread, a UI thread, a thread of another library — is not a task. To let it wait on a primitive, convert it.

Converting

ConvertThreadToTask gives the calling thread a task context. From then on it can call Wait, Enter and the waiting forms of file and socket operations. ConvertTaskToThread releases the context again; call it on the same thread, once no other task can still resume this thread's task.

TC::ConvertThreadToTask();
// ... create a scheduler, add tasks, wait for them ...
TC::ConvertTaskToThread();

How a converted thread waits

A converted thread is not one of a scheduler's threads, so there is no other task for it to run. When it waits, the thread itself blocks until it is resumed. That is fine for main waiting for the program's work, or for a thread that hands work to tasks and waits for the result.

What stays different

  • A converted thread does not run other tasks while it waits.
  • Task::Schedule and GetTaskScheduler need a scheduler task; from a converted thread call AddTask on the scheduler object instead.
  • Calls that never wait — Set, Release, TryWait, AddTask — work from any thread, converted or not.
  • The scheduler's thread callback (ThreadCallbackProc) runs on a plain scheduler thread outside any task and must not use the primitives (see Thread callbacks).

See also