我从某人那里听说,代码构建中大量警告的大型项目比警告少的警告要慢得多(当然,编译器设置为具有高级警告敏感性).
有没有合理的解释,或者也许有人可以分享他们对这个主题的经验?
编写简单的 C 代码,尝试控制两个不同线程的输出:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
sem_t sem;
void* thread_func(void* aArgs)
{
printf("Entering thread %p with %d\n", (void*)pthread_self(), (int)aArgs);
int i = 0;
for(;i < 10; i++)
{
sem_wait(&sem);
if ((i % 2) == (int)aArgs)
printf("val is %d in thread %p \n", i, (void*)pthread_self());
sem_post(&sem);
}
}
int main()
{
pthread_t thread_1, thread_2;
sem_init(&sem, 0, 1);
pthread_create(&thread_1, NULL, (void*)thread_func, (void*)0);
pthread_create(&thread_2, NULL, (void*)thread_func, (void*)1);
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
sem_destroy(&sem);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是混合奇数和偶数的序列。但我从一个线程接收所有数字,然后从第二个线程接收所有其他数字,如下所示(即使我增加循环计数器幅度):
Entering thread 0xb75f2b40 with …Run Code Online (Sandbox Code Playgroud) 最近,我在"高级Linux编程"一书(http://www.advancedlinuxprogramming.com/alp-folder/alp-ch04-threads.pdf,第4.5章)中读到,在GNU/Linux上POSIX线程实现为进程和某种"管理器线程",它做一些控制工作.
当我从本书中运行以下示例时:
#include <stdio.h>
#include <pthread.h>
void* thread_func(void *arg)
{
fprintf(stderr, "thread: %d\n", (int)getpid());
while(1);
return NULL;
}
int main()
{
fprintf(stderr, "main: %d\n", (int)getpid());
pthread_t thread;
pthread_create(&thread, NULL, thread_func, NULL);
while(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已经收到了主线程和子线程的相同PID,而书中说它可以是不同的,并且还有另一个PID,它对应于所谓的"管理器线程".我试图找到关于这个"经理线程"的一些信息,但事实证明这很困难.
UPD.我对我的节目的行为毫不怀疑,但是对于行为有一些混淆,在书中解释 - 特别是在哪种情况下它可能是真的?