简单的线程!C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1135964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:58:29  来源:igfitidea点击:

Simple pthread! C++

c++pthreads

提问by Angel.King.47

I have no idea why this doesn't work

我不知道为什么这不起作用

#include <iostream>
#include <pthread.h>
using namespace std;

void *print_message(){

    cout << "Threading\n";
}



int main() {

    pthread_t t1;

    pthread_create(&t1, NULL, &print_message, NULL);
    cout << "Hello";

    return 0;
}

The error:

错误:

[Description, Resource, Path, Location, Type] initializing argument 3 of 'int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)' threading.cpp threading/src line 24 C/C++ Problem

[描述、资源、路径、位置、类型] 初始化参数 3 of 'int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)' threading.cpp threading/src line 24 C/ C++问题

回答by Sam Harwell

You should declare the thread main as:

您应该将线程 main 声明为:

void* print_message(void*) // takes one parameter, unnamed if you aren't using it

回答by Martin York

Because the main thread exits.

因为主线程退出了。

Put a sleep in the main thread.

在主线程中休眠。

cout << "Hello";
sleep(1);

return 0;

The POSIX standard does not specify what happens when the main thread exits.
But in most implementations this will cause all spawned threads to die.

POSIX 标准没有指定主线程退出时会发生什么。
但在大多数实现中,这将导致所有生成的线程死亡。

So in the main thread you should wait for the thread to die before you exit. In this case the simplest solution is just to sleep and give the other thread a chance to execute. In real code you would use pthread_join();

因此,在主线程中,您应该在退出之前等待线程死亡。在这种情况下,最简单的解决方案就是休眠并给另一个线程一个执行的机会。在实际代码中,您将使用 pthread_join();

#include <iostream>
#include <pthread.h>
using namespace std;

#if defined(__cplusplus)
extern "C"
#endif
void *print_message(void*)
{
    cout << "Threading\n";
}



int main() 
{
    pthread_t t1;

    pthread_create(&t1, NULL, &print_message, NULL);
    cout << "Hello";

    void* result;
    pthread_join(t1,&result);

    return 0;
}

回答by Falaina

From the pthread function prototype:

从 pthread 函数原型:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
    void *(*start_routine)(void*), void *arg);

The function passed to pthread_create must have a prototype of

传递给 pthread_create 的函数必须有一个原型

void* name(void *arg)

回答by Vijay Mathew

This worked for me:

这对我有用:

#include <iostream>
#include <pthread.h>
using namespace std;

void* print_message(void*) {

    cout << "Threading\n";
}

int main() {

    pthread_t t1;

    pthread_create(&t1, NULL, &print_message, NULL);
    cout << "Hello";

    // Optional.
    void* result;
    pthread_join(t1,&result);
    // :~

    return 0;
}

回答by cwap

When compiling with G++, remember to put the -lpthread flag :)

用 G++ 编译时,记得放 -lpthread 标志:)

回答by Michael van der Westhuizen

Linkage. Try this:

连锁。尝试这个:

extern "C" void *print_message() {...

extern "C" void *print_message() {...