如何在普通C中启动线程?

时间:2021-10-30 20:59:53

I have used fork() in C to start another process. How do I start a new thread?

我在C中使用了fork()来启动另一个进程。如何开始一个新的线程?

5 个解决方案

#1


51  

Since you mentioned fork() I assume you're on a Unix-like system, in which case POSIX threads (usually referred to as pthreads) are what you want to use.

由于您提到了fork(),所以我假设您是在一个类unix系统上,在这种情况下,您希望使用POSIX线程(通常称为pthreads)。

Specifically, pthread_create() is the function you need to create a new thread. Its arguments are:

具体地说,pthread_create()是创建新线程所需的函数。它的参数是:

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

The first argument is the returned pointer to the thread id. The second argument is the thread arguments, which can be NULL unless you want to start the thread with a specific priority. The third argument is the function executed by the thread. The fourth argument is the single argument passed to the thread function when it is executed.

第一个参数是指向线程id的返回指针,第二个参数是线程参数,它可以是空的,除非您想以特定的优先级启动线程。第三个参数是线程执行的函数。第四个参数是线程函数执行时传递给它的单个参数。

#2


12  

AFAIK, ANSI C doesn't define threading, but there are various libraries available.

AFAIK, ANSI C没有定义线程,但是有各种可用的库。

If you are running on Windows, link to msvcrt and use _beginthread or _beginthreadex.

如果您在Windows上运行,请链接到msvcrt并使用_beginthread或_beginthreadex。

If you are running on other platforms, check out the pthreads library (I'm sure there are others as well).

如果您正在其他平台上运行,请查看pthreads库(我肯定还有其他平台)。

#3


8  

Threads are not part of the C standard, so the only way to use threads is to use some library (eg: POSIX threads in Unix/Linux, _beginthread/_beginthreadex if you want to use the C-runtime from that thread or just CreateThread Win32 API)

线程不是C标准的一部分,所以使用线程的唯一方法是使用一些库(例如:Unix/Linux中的POSIX线程,如果您希望使用该线程的C-runtime或CreateThread Win32 API)

#4


7  

pthreads is a good start, look here

pthreads是一个良好的开端,请看这里

#5


2  

Check out the pthread (POSIX thread) library.

查看pthread (POSIX thread)库。

#1


51  

Since you mentioned fork() I assume you're on a Unix-like system, in which case POSIX threads (usually referred to as pthreads) are what you want to use.

由于您提到了fork(),所以我假设您是在一个类unix系统上,在这种情况下,您希望使用POSIX线程(通常称为pthreads)。

Specifically, pthread_create() is the function you need to create a new thread. Its arguments are:

具体地说,pthread_create()是创建新线程所需的函数。它的参数是:

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

The first argument is the returned pointer to the thread id. The second argument is the thread arguments, which can be NULL unless you want to start the thread with a specific priority. The third argument is the function executed by the thread. The fourth argument is the single argument passed to the thread function when it is executed.

第一个参数是指向线程id的返回指针,第二个参数是线程参数,它可以是空的,除非您想以特定的优先级启动线程。第三个参数是线程执行的函数。第四个参数是线程函数执行时传递给它的单个参数。

#2


12  

AFAIK, ANSI C doesn't define threading, but there are various libraries available.

AFAIK, ANSI C没有定义线程,但是有各种可用的库。

If you are running on Windows, link to msvcrt and use _beginthread or _beginthreadex.

如果您在Windows上运行,请链接到msvcrt并使用_beginthread或_beginthreadex。

If you are running on other platforms, check out the pthreads library (I'm sure there are others as well).

如果您正在其他平台上运行,请查看pthreads库(我肯定还有其他平台)。

#3


8  

Threads are not part of the C standard, so the only way to use threads is to use some library (eg: POSIX threads in Unix/Linux, _beginthread/_beginthreadex if you want to use the C-runtime from that thread or just CreateThread Win32 API)

线程不是C标准的一部分,所以使用线程的唯一方法是使用一些库(例如:Unix/Linux中的POSIX线程,如果您希望使用该线程的C-runtime或CreateThread Win32 API)

#4


7  

pthreads is a good start, look here

pthreads是一个良好的开端,请看这里

#5


2  

Check out the pthread (POSIX thread) library.

查看pthread (POSIX thread)库。