fotg
fotg

Reputation: 639

What is the best way to create suspendable/resumable threads

I am doing some network programming for a microprocessor which sends low buffer notifications and I have a thread that writes a set amount of information. When it is done it needs to enter a suspended state and wait for the low buffer notification to resume.

Is it better to use windows' thread pool api, or to use threads that are created with CreateThread()?

Upvotes: 2

Views: 283

Answers (3)

Tudor
Tudor

Reputation: 62439

It is better to use single threads created with CreateThread. ThreadPool threads are meant to do simple tasks and then return to the pool, they are not meant for long running tasks, waits or I/O operations. This is because they are limited in number and once you have one running and waiting, you cannot use it somewhere else.

Furthermore, ThreadPool threads are managed by the system and are not meant to be identifiable from the outside. You're better off using classic Threads.

Upvotes: 0

Mankarse
Mankarse

Reputation: 40603

The best way to create a suspendable thread is:

std::thread thread(function, arguments);

When you want to suspend the execution of that thread at a later stage you can use the wait() member of std::condition_variable or std::condition_variable_any.

Upvotes: 0

Zan Lynx
Zan Lynx

Reputation: 54325

When your thread needs to wait, it should begin waiting on an event. This suspends the thread automatically.

Windows provides the WaitForMultipleObjects and WaitForSingleObject functions for this. Linux uses condition variables or semaphores.

Upvotes: 4

Related Questions