简体   繁体   English

如果 main 在 c 中也是一个线程,为什么它不与其他线程一起运行

[英]If main is a thread as well in c, why doesn't it run with other threads

I read that main() is single thread itself, so when i create 2 threads in my program like this;我读到 main() 本身就是单线程,所以当我像这样在我的程序中创建 2 个线程时;

#include<stdio.h>
#include<pthread.h>
#include<windows.h>

void* counting(void * arg){
    int i = 0;
    for(i; i < 50; i++){
        printf("counting ... \n");
        Sleep(100);

    }
}

void* waiting(void * arg){
    int i = 0;
    for(i; i < 50; i++){
        printf("waiting ... \n");
        Sleep(100);
    }
}

int main(){
    pthread_t thread1;
    pthread_t thread2;
    pthread_create(&thread1, NULL, counting, NULL);
    pthread_create(&thread2, NULL, waiting, NULL);
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    int i = 0;
    for(i; i < 50; i++){
        printf("maining ... \n");
        Sleep(1000);
    }


}

Is main really a thread in that case?在那种情况下 main 真的是一个线程吗? in that case if in main in sleep for some time, shouldn't the main give the CPU to other threads?在这种情况下,如果主线程处于睡眠状态一段时间,主线程不应该将 CPU 分配给其他线程吗? Is main a threads itself here?这里主要是一个线程本身吗? I am confused a bit here.我在这里有点困惑。 Is there a specific order to main thread execution?主线程执行有特定的顺序吗?

 pthread_join(thread1, NULL);
 pthread_join(thread2, NULL);

You asked the thread to wait until thread1 terminates and then wait until thread2 terminates, so that's what it does.您要求线程等待直到线程thread1终止,然后等待thread2终止,这就是它所做的。

main() is not a thread but a function, so here's a clear "no" to your initial claim. main()不是一个线程而是一个函数,所以这对你最初的声明是一个明确的“不”。 However, if you read a few definitions of what is a thread, you will find that it is something that can be scheduled, ie an ongoing execution of code.然而,如果你阅读了一些关于什么是线程的定义,你会发现它是可以调度的,即代码的持续执行。 Further, a running program will not be able to actually do anything without "ongoing execution of code" without eg main() as first entrypoint.此外,如果没有main()作为第一个入口点,如果没有“持续执行代码”,正在运行的程序将无法实际执行任何操作。 So, definitely, every code executed by a program is executed by a thread, without exceptions.因此,毫无疑问,程序执行的每个代码都由线程执行,无一例外。

BTW: You can retrieve the thread ID of the current thread.顺便说一句:您可以检索当前线程的线程 ID。 Try running that from main() .尝试从main()运行它。 It will work and give you a value that distinguishes this call from calls from other threads.它将工作并为您提供一个值,该值可将此调用与来自其他线程的调用区分开来。

I read that main() is single thread itself我读到 main() 本身就是单线程

No, you have misunderstood.不,你误会了。 Every C program has a function named main() .每个 C 程序都有一个名为main()函数 C language semantics of the program start with the initial entry into that function.程序的 C 语言语义从最初进入该函数开始。 In that sense, and especially when you supply the parentheses, main() , is a function, not a thread.从这个意义上说,尤其是当您提供括号时, main()是一个函数,而不是一个线程。

However, every process also has a main thread which has a few properties that distinguish it from other threads.然而,每个进程也有一个主线程,该主线程有一些区别于其他线程的属性。 That is initially the only thread, so it is that thread that performs the initial entry into the main() function.那最初是唯一的线程,因此该线程执行main()函数的初始入口。 But it is also that thread that runs all C functions called by main() , and by those functions, etc ., so it is not, in general, specific to running only the code appearing directly in the body of main() , if that's what you mean by "main() is a single thread itself".但它也是运行main()调用的所有 C 函数以及这些函数调用的所有 C 函数的线程,所以一般来说,它并不特定于只运行直接出现在main()主体中的代码,如果这就是“main() 本身就是一个单线程”的意思。

, so when i create 2 threads in my program like this; ,所以当我像这样在我的程序中创建 2 个线程时; [...] Is main really a thread in that case? [...] 在那种情况下 main 真的是一个线程吗?

There is really a main thread in that case, separate from the two additional threads that it starts.在这种情况下确实有一个主线程,与它启动的两个附加线程分开。

in that case if in main in sleep for some time, shouldn't the main give the CPU to other threads?在这种情况下,如果主线程处于睡眠状态一段时间,主线程不应该将 CPU 分配给其他线程吗?

If the main thread slept while either of the other two were alive, then yes, one would expect one or both of the others to get (more) CPU time.如果主线程在其他两个线程中的任何一个活着时休眠,那么是的,人们会期望其他一个或两个获得(更多)CPU 时间。 And in a sense, that's exactly what happens: the main thread calls pthread_join() on each of the other threads in turn, which causes it to wait (some would say "sleep") until those threads terminate before it proceeds.从某种意义上说,这正是发生的事情:主线程依次在其他每个线程上调用pthread_join() ,这导致它等待(有些人会说“睡眠”),直到这些线程在继续之前终止。 While it's waiting, it does not contend with the other threads for CPU time, as that's pretty much what "waiting" means.当它在等待时,它不会与其他线程争用 CPU 时间,因为这几乎就是“等待”的意思。 But by the time the main thread reaches the Sleep() call in your program, the other threads have already terminated and been joined, because that's what pthread_join() does.但是当主线程到达程序中的Sleep()调用时,其他线程已经终止并加入,因为这就是pthread_join()所做的。 They no longer exist, so naturally they don't run during the Sleep() .它们不再存在,因此自然不会在Sleep()期间运行。

Is main a threads itself here?这里主要是一个线程本身吗?

There is a main thread, yes, and it is the only one in your particular process that executes any of the code in function main() .有一个主线程,是的,它是您的特定进程中唯一一个执行函数main()任何代码的主线程。 Nothing gets executed except in some thread or other.除了在某个线程或其他线程中之外,什么都不会被执行。

I am confused a bit here.我在这里有点困惑。 Is there a specific order to main thread execution?主线程执行有特定的顺序吗?

As already described, the main thread is initially the only thread.如前所述,主线程最初是唯一的线程。 Many programs never have more than that one.许多程序永远不会超过那个。 Threads other than the main one are created only by the main thread or by another thread that has already been created.主线程以外的线程仅由主线程或已创建的另一个线程创建。 Of course, threads cannot run before they are created, nor, by definition, after they have terminated.当然,线程不能在创建之前运行,也不能在它们终止之后运行。 Threads execute independently of each other, generally without any predefined order, except as is explicitly established via synchronization objects such as mutexes, via for-purpose functions such as pthread_join() , or via cooperative operations on various I/O objects such as pipes.线程彼此独立执行,通常没有任何预定义的顺序,除非通过同步对象(例如互斥锁)、通过专用函数(例如pthread_join()或通过对各种 I/O 对象(例如管道pthread_join()协作操作pthread_join()显式建立。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM