所有,
下面的代码来自"Unix环境中的高级编程",它创建一个新线程,并打印主线程和新线程的进程ID和线程ID.
在本书中,它表示在linux中,此代码的输出将显示两个线程具有不同的进程ID,因为pthread使用轻量级进程来模拟线程.但是当我在Ubuntu 12.04中运行此代码时,它有内核3.2,打印相同的pid.
那么,新的linux内核是否会改变pthread的内部实现?
#include "apue.h"
#include <pthread.h>
pthread_t ntid;
void printids(const char *s) {
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n",
s, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
}
void *thread_fn(void* arg) {
printids("new thread: ");
return (void *)0;
}
int main(void) {
int err;
err = pthread_create(&ntid, NULL, thread_fn, NULL);
if (err != 0)
err_quit("can't create thread: %s\n", strerror(err));
printids("main thread: ");
sleep(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个Drupal模块,在这些模块中我需要执行相同部分代码的5倍,所以,我认为我可以并行执行以提高执行速度,但我不想使用pcntl_fork,我想用pthread.现在的问题是:pthreadphp的库是否与中的相同c?我可以在php中实现内核级线程吗?因为我认为如果php pthread是用户级别我在我的情况下使用它没有任何优势感谢回复