开发者

pausing main thread execution other than sleep() in C

开发者 https://www.devze.com 2023-03-28 05:47 出处:网络
I need to pause the execution of the main thread with out using sleep statement. is there any fun开发者_StackOverflow社区ction or status values that shows the alive status of other threads like isaliv

I need to pause the execution of the main thread with out using sleep statement. is there any fun开发者_StackOverflow社区ction or status values that shows the alive status of other threads like isalive() in java?


pause() often works well; it suspends execution until a signal is received.


Standard C provides no way to pause the main thread, because standard C has no concept of threads. (That's changing in C201X, but that new version of the standard isn't quite finished, and there are no implementations of it.)

Even sleep() (which is a function, not a language-defined statement) is implementation-specific.

So it's not really possible to answer your question without knowing what environment you're using. Do you have multiple threads? If so, what threading library are you using? Pthreads? Win32 threads?

Why does sleep() not satisfy your requirements? (Probably because it pauses all threads, not just the current one.)

(Hint: Whenever you ask "How do I do X without using Y?", tell us why you can't use Y.)

Consult the documentation for whatever thread library you're using. It should provide a function that does what you need.


A extremely simple approach would be using something as simple as getchar().

Other approach could be waiting for a signal with pthread_cond_wait (or any other similar function in a different threading API).

Other approach could be sitting on a tight loop and using a semaphore (or something simpler like a global variable value) to wait for the other threads to finish.

Anyway, there are several options. You don't say enough about your problem to tell what's your best choice here.


select() is often a good choice.

On Linux, epoll() is often a good alternative to select().

And every program, "threaded" or not, always has "main thread". If you're actually using threads, however, look at pthread_cond_wait().

0

精彩评论

暂无评论...
验证码 换一张
取 消