先看下面的程序thread1.c:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void *thread_func(void *var);
int main(void)
{
pthread_t tid;
int var;
int i;
for (i=0; i<20; i++)
{
var = i;
pthread_create(&tid, NULL, thread_func, &var);
}
pthread_exit(NULL);
}
void *thread_func(void *var)
{
int x = *(int*)var;
pthread_detach(pthread_self());
printf("thread %d\n", x);
}
编译:gcc thread1.c -pthread -o thread1
运行:./thread1
这个程序我们期望的结果是:输出不重复的thread 0~19,但是实际上是有重复的。如果子线程中的赋值语句在主线程中下一个循环var = i之前执行,那么结果是正确的,然而如果是在之后才执行,下一个i就会覆盖掉前一个var,这样就会出现相同的结果。
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void *thread_func(void *var);
int main(void)
{
pthread_t tid;
int *var = NULL;
int i;
for (i=0; i<20; i++)
{
var = (int*)malloc(sizeof(int));
*var = i;
pthread_create(&tid, NULL, thread_func, var);
}
pthread_exit(NULL);
}
void *thread_func(void *var)
{
int x = *(int*)var;
if (var)
{
free(var);
var = NULL;
}
pthread_detach(pthread_self());
printf("thread %d\n", x);
}
编译:gcc thread2.c -pthread -o thread2
运行:./thread2
之前是因为在同一块内存上传值会造成覆盖,现在我们为每个新创建的线程都创建一块内存,这样他们每个线程都有一块内存供传值,就不会覆盖别人的内存了。
上面只是举个例子,不用中间变量var而是直接把i作为参数传递也是没什么问题的。关键是这个参数也许不是那么简单,可能是accept客户端socket描述符,所以这样举例子也是可以的。
当然,上面问题利用线程间同步也是可以解决的。