简体   繁体   English

C- POSIX 线程基础

[英]C- POSIX Thread Basics

I am learning the basics of POSIX threads.我正在学习 POSIX 线程的基础知识。 I want to create a program that prints "Hello World!"我想创建一个打印“Hello World!”的程序。 10 times with a delay of a second between each printout. 10 次,每次打印输出之间有 1 秒的延迟。 I've used a for loop to print it 10 times, but I am stuck on how to implement the time delay part.我已经使用 for 循环将它打印了 10 次,但我对如何实现时间延迟部分感到困惑。

This is my code so far:到目前为止,这是我的代码:

#define MAX 10

void* helloFunc(void* tid)
{
printf("Hello World!\n", (int)(intptr_t)tid);
}

int main(int ac, char * argv)
{
pthread_t hej[MAX];
for (int i = 0; i < MAX; i++)
{
    pthread_create(&hej[i], NULL, helloFunc, (void*)(intptr_t)i);
    pthread_join(&hej[i], NULL);
}
pthread_exit(NULL);
return(0);
}

Thanks in advance!提前致谢!

There are two major problems with your code:您的代码有两个主要问题:

  • First of all you must wait for the threads to finish.首先,您必须等待线程完成。 You do that by joining them with pthread_join .您可以通过将它们与pthread_join连接来做到这一点。 And for that to work you must save the pthread_t value from each and every thread (for example in an array).为此,您必须保存每个线程的pthread_t值(例如在数组中)。

    If you don't wait for the threads then the exit call will end the process, and that will also unexpectedly kill and end all threads in the process.如果您不等待线程,则exit调用将结束进程,这也将意外终止并结束进程中的所有线程。

    For all threads to run in parallel you should wait in a separate loop after you have created them:对于所有线程并行运行,您应该在创建它们后在单独的循环中等待:

     pthread_t hej[MAX]; for (int i = 0; i < MAX; i++) { pthread_create(&hej[i], ...); } for (int i = 0; i < MAX; i++) { pthread_join(&hej[i], NULL); }
  • The second problem is that you pass a pointer to i to the thread, so tid inside the thread functions will be all be the same (and a very large and weird value).第二个问题是您将指向i指针传递给线程,因此线程函数内的tid将全部相同(并且是一个非常大且奇怪的值)。 To pass a value you must first cast it to intptr_t and then to void * :要传递一个值,您必须首先将其转换为intptr_t ,然后转换为void *

     pthread_create(..., (void *) (intptr_t) i);

    And in the thread function you do the opposite casting:在线程函数中,您执行相反的转换:

     printf("Hello World %d!\\n", (int) (intptr_t) tid);

    Note that this is an exception to the rule that one should never pass values as pointers (or opposite).请注意,这是永远不应将值作为指针(或相反)传递的规则的一个例外。


Finally for the "delay" bit... On POSIX systems there are many ways to delay execution, or to sleep.最后是“延迟”位...在 POSIX 系统上,有很多方法可以延迟执行或休眠。 The natural and simple solution would be to use sleep(1) which sleeps one second.自然而简单的解决方案是使用sleep(1)睡眠一秒钟。

The problem is where do to this sleep(1) call.问题是在哪里执行此sleep(1)调用。 If you do it in the thread functions after the printf then all threads will race to print the message and then all will sleep at the same time.如果您在printf之后的线程函数中执行此操作,则所有线程将竞相打印消息,然后所有线程都将同时休眠。

If you do it in the loop where you create the threads, then the threads won't really run in parallel, but really in serial where one thread prints it message and exits, then the main thread will wait one second before creating the next thread.如果您在创建线程的循环中执行此操作,则线程不会真正并行运行,而是真正串行运行,其中一个线程打印消息并退出,然后main线程将在创建下一个线程之前等待一秒钟. It makes the threads kind of useless.它使线程变得毫无用处。

As a possible third solution, use the value passed to the thread function to use as the sleep time, so the thread that is created first (when i == 0 ) will primt immediately, the second thread (when i == 1 ) will sleep one second.作为可能的第三种解决方案,使用传递给线程函数的值作为睡眠时间,因此首先创建的线程(当i == 0 )将立即启动,第二个线程(当i == 1 )将睡一秒钟。 And so on, until the tenth thread is created and will sleep nine seconds before printing the message.依此类推,直到第十个线程被创建并在打印消息前休眠九秒。

Could be done as:可以这样做:

void* helloFunc(void* tid)
{
    int value = (int) (intptr_t) tid;
    sleep(value);
    printf("Hello World %d!\n", value);

    // Must return a value, as the function is declared as such
    return NULL;
}

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

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