Ajay George
Ajay George

Reputation: 302

pausing main thread execution other than sleep() in C

I need to pause the execution of the main thread with out using sleep statement. is there any function or status values that shows the alive status of other threads like isalive() in java?

Upvotes: 2

Views: 2398

Answers (4)

Keith Thompson
Keith Thompson

Reputation: 263257

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.

Upvotes: 2

Dave
Dave

Reputation: 11162

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

Upvotes: 4

paulsm4
paulsm4

Reputation: 121649

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().

Upvotes: 0

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

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.

Upvotes: 0

Related Questions