0
$\begingroup$

Does every thread have its own main function?

I know that its have its own stack, but what about main function (not necessarily a function which called main).

For example, when creating a thread, we pass a function as an argument for the new thread to run it.

I tried to search about this topic, but couldn't find answers.

$\endgroup$
4
  • $\begingroup$ Can you define "main function"? $\endgroup$
    – harold
    Jul 27, 2020 at 18:54
  • $\begingroup$ @harold A function which the thread executes. Just like that the first thread of a program runs the main method (the "actual" main) $\endgroup$
    – Dani
    Jul 27, 2020 at 19:03
  • 1
    $\begingroup$ A thread has an entry point, but whether that qualifies as a "main function" depends on your definition and it is still vague. It's also not clear what you mean by "its own". $\endgroup$
    – harold
    Jul 27, 2020 at 19:17
  • $\begingroup$ Cross-posted: stackoverflow.com/q/63121642/781723, cs.stackexchange.com/q/128760/755. Please do not post the same question on multiple sites. $\endgroup$
    – D.W.
    Nov 27, 2021 at 23:05

1 Answer 1

1
$\begingroup$

For any reasonable definition of a 'main function', no.

Each thread has to start at some point in the code. For the main thread that is usually called the main function.

Modern programming languages are often designed that when starting a new thread it starts in a given function. You could call that the 'main function' of that thread, but is more commonly called its entry point.

However the entry point of a thread does not have to be at some dedicated function, or even at the start of a function. As a counterexample I would invite you to look at POSIX fork().

$\endgroup$
2
  • $\begingroup$ Isn't fork used for creating a new process (not a new thread)? $\endgroup$
    – Dani
    Jul 27, 2020 at 19:51
  • $\begingroup$ @Dani A new process implies a new thread. Each process has at least one thread. $\endgroup$
    – orlp
    Jul 27, 2020 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.