简体   繁体   English

Pthread和C语言

[英]Pthread and C language

Here is my code: 这是我的代码:

static long x = 0;
void * thread_func(void *arg){
while(1){
    printf("Thread 2 says %ld\n",++x);
    sleep(1);
}
}
int main(){
pthread_t tid;
pthread_create(&tid,NULL,thread_func,NULL);
while(x < 10)   {
    printf("Thread 1 says %ld\n",++x);
    sleep(2);
}
return 0;
}

Now what shall be the output, the book says it will be: 现在,输出应该是什么,该书说它将是:

OUTPUT
Thread 1 says 1
Thread 2 says 2
Thread 2 says 3
Thread 1 says 4
Thread 2 says 5
Thread 2 says 6
Thread 1 says 7
Thread 2 says 8
Thread 2 says 9
Thread 1 says 10
Thread 2 says 11
Thread 2 says 12

Now shall not the first one should be Thread 2 says 1, since the pthread_create() is before the while loop in main. 现在不应该第一个是线程2说1,因为pthread_create()在main中的while循环之前。 Also how does sleep work here? 还有睡眠在这里如何工作? Like what exactly is a thread sleeping for, and what process executes when the executing thread sleeps.? 就像线程到底在做什么,而当执行线程在睡觉时,什么进程在执行呢?

ie sleep(1) and sleep(2) what does 1 and 2 signify? 即sleep(1)和sleep(2)1和2分别代表什么?

I am not sure what book you are reading, but you should stop reading it immediately. 我不确定您正在阅读哪本书,但是您应该立即停止阅读。 First of all, you cannot guarantee that thread will be up and running before entering a loop in main . 首先,您不能保证在main进入循环之前线程将启动并运行。 It could start before or after. 它可以在之前或之后开始。 Second of all, there is no access synchronization when accessing variable x from two different threads, thus no guarantee as for what value it will have. 其次,当从两个不同的线程访问变量x时,没有访问同步,因此无法保证它将具有什么值。 Calling printf synchronizes on stdout object, and calling sleep swaps the thread out, and it takes a lot of time to schedule it again. 调用printfstdout对象上进行同步,而调用sleep则将线程换出,这需要大量时间来重新安排它。 This example is useless and assumptions about the output are wrong. 这个例子是没有用的,关于输出的假设是错误的。

Now, some answers.. 现在,一些答案..

Now shall not the first one should be Thread 2 says 1, since the pthread_create() is before the while loop in main. 现在不应该第一个是线程2说1,因为pthread_create()在main中的while循环之前。

Not necessarily. 不必要。 It takes hell of a lot CPU cycles to start another thread. 启动另一个线程需要大量的CPU周期。 So it is most likely that you will enter while loop in main before that other thread starts. 因此,最有可能在其他线程启动之前,在main进入while循环。 But, not necessarily. 但是,不一定。

Also how does sleep work here? 还有睡眠在这里如何工作?

It tells the kernel not to execute the calling thread for a given number of seconds. 它告诉内核在给定的秒数内不要执行调用线程。 The actual sleeping time could be a bit longer due to overhead associated with scheduling. 由于与计划相关的开销,实际的睡眠时间可能会更长一些。 Other threads are not affected by that (well, they are, but not directly, ie other threads can have more CPU time etc.). 其他线程不受此影响(嗯,它们受但不是直接影响,即其他线程可能有更多的CPU时间等)。

what process executes when the executing thread sleeps? 执行线程休眠时执行什么进程?

Process never executes anything. 进程从不执行任何操作。 It is the kernel's schedule that executes processes (and threads). 它是执行进程(和线程)的内核调度。 By default, process consists of one main thread, and kernel executes it. 默认情况下,进程由一个主线程组成,并由内核执行。 When you create more threads, scheduler executes more threads inside the process etc. When one or more threads are sleeping, schedulers executes those not sleeping. 当您创建更多线程时,调度程序将在进程等内部执行更多线程。当一个或多个线程处于休眠状态时,调度程序将执行那些未处于休眠状态的线程。 Think of the threads as of processes sharing the same address space - that's what they are. 可以将线程视为共享相同地址空间的进程,这就是它们的本质。

The program has a huge flaw that no multithreaded program can afford to have . 该程序具有一个巨大的缺陷,是任何多线程程序都无法承受的。 I actually allows a global varaible "x" to be manipulated by 2 threads without any sort of synchronization between them . 实际上,我允许全局变量“ x”由2个线程操纵,而它们之间没有任何形式的同步。 this will surely result in the data contained in the variable becoming inconsistent . 这肯定会导致变量中包含的数据变得不一致。 Unpredictable output will result . 将导致不可预测的输出。 you need to refer to a better book with a good example . 您需要参考带有范例的更好的书。 basically the idea is to use locks while accessing the variable 'x' . 基本上,这个想法是在访问变量'x'时使用锁。

As others above have pointed out sleep will stop the kernel from executing the thread for the desired number of seconds . 正如上面的其他人所指出的,睡眠将使内核在所需的秒数内停止执行线程。 I feel that it is being used here as a means of synchronization . 我觉得它在这里被用作同步手段。 however sleep is never a good option as it will not guarantee that the operation on variable 'x' will not become inconsistent . 但是睡眠永远不是一个好的选择,因为它不能保证变量'x'的操作不会变得不一致。 Dont rely on sleep . 不要依靠睡眠。 use locks . 使用锁。 An excellent place to start thread programming is 一个开始线程编程的好地方是

http://cs.tju.edu.cn/orgs/hpclab/course/undergraduate/Ref/POSIXMultithreadProgrammingPrimer.pdf http://cs.tju.edu.cn/orgs/hpclab/course/undergraduate/Ref/POSIXMultithreadProgrammingPrimer.pdf

Its the pthread primer . 它是pthread入门。

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

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