3个线程使用的都是同一个info
代码 Info_t *info = (Info_t *)malloc(sizeof(Info_t))只创建了一个info
pthread_create(&threads[i],NULL,calMatrix,(void *)info)三个线程使用的是同一个
我把你的代码改了下:
#include <stdio.h>#include <stdlib.h>
#include <pthread.h>
int mtc[3] = { 0 } // result matrix
typedef struct
{
int prank
int *mta
int *mtb
}Info_t
void* calMatrix(void* arg)
{
int i
Info_t *info = (Info_t *)arg
int prank = info->prank
fprintf(stdout,"calMatrix : prank is %d\n",prank)
for(i = 0 i < 3 i++)
mtc[prank] += info->mta[i] * info->mtb[i]
return NULL
}
int main(int argc,char **argv)
{
int i,j,k = 0
int mta[3][3]
int mtb[3] = { 1 }
Info_t *info = (Info_t *)malloc(sizeof(Info_t)*3)
for(i = 0 i < 3 i++)
for(j = 0 j < 3 j++)
mta[i][j] = k++
/* 3 threads */
pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t)*3)
fprintf(stdout,"\n")fflush(stdout)
for(i = 0 i < 3 i++)
{
info[i].prank = i
info[i].mta = mta[i]
info[i].mtb = mtb
pthread_create(&threads[i],NULL,calMatrix,(void *)(&info[i]))
}
for(i = 0 i < 3 i++)
pthread_join(threads[i],NULL)
fprintf(stdout,"\n==== the matrix result ====\n\n")
fflush(stdout)
for(i = 0 i < 3 i++)
{
fprintf(stdout,"mtc[%d] = %d\n",i,mtc[i])
fflush(stdout)
}
return 0
}
矩阵的计算我忘记了,你运行看看结果对不对
线程自身用的内存,是在栈上系统自动分配,或自己配置( *** 作系统提供了可编程配置参数,但也是 *** 作系统在管理)。线程运行完成后返回栈内存 *** 作系统会自动回收。需要注意的是,如果是在线程运行中中,使用malloc或 *** 作系统的内存分配函数分配的内存,需要在线程返回前或返回后显示释放。自己编写代码,显示调用free或 *** 作系统提供的内存释放函数。这么高的悬赏,实例放后面。信号量(sem),如同进程一样,线程也可以通过信号量来实现通信,虽然是轻量级的。信号量函数的名字都以"sem_"打头。线程使用的基本信号量函数有四个。
信号量初始化。int sem_init (sem_t *sem , int pshared, unsigned int value)
这是对由sem指定的信号量进行初始化,设置好它的共享选项(linux 只支持为0,即表示它是当前进程的局部信号量),然后给它一个初始值VALUE。
等待信号量。给信号量减1,然后等待直到信号量的值大于0。
int sem_wait(sem_t *sem)
释放信号量。信号量值加1。并通知其他等待线程。
int sem_post(sem_t *sem)
销毁信号量。我们用完信号量后都它进行清理。归还占有的一切资源。
int sem_destroy(sem_t *sem) #include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__)return}
typedef struct _PrivInfo
{
sem_t s1
sem_t s2
time_t end_time
}PrivInfo
static void info_init (PrivInfo* thiz)
static void info_destroy (PrivInfo* thiz)
static void* pthread_func_1 (PrivInfo* thiz)
static void* pthread_func_2 (PrivInfo* thiz)
int main (int argc, char** argv)
{
pthread_t pt_1 = 0
pthread_t pt_2 = 0
int ret = 0
PrivInfo* thiz = NULL
thiz = (PrivInfo* )malloc (sizeof (PrivInfo))
if (thiz == NULL)
{
printf ("[%s]: Failed to malloc priv./n")
return -1
}
info_init (thiz)
ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz)
if (ret != 0)
{
perror ("pthread_1_create:")
}
ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz)
if (ret != 0)
{
perror ("pthread_2_create:")
}
pthread_join (pt_1, NULL)
pthread_join (pt_2, NULL)
info_destroy (thiz)
return 0
}
static void info_init (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
thiz->end_time = time(NULL) + 10
sem_init (&thiz->s1, 0, 1)
sem_init (&thiz->s2, 0, 0)
return
}
static void info_destroy (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
sem_destroy (&thiz->s1)
sem_destroy (&thiz->s2)
free (thiz)
thiz = NULL
return
}
static void* pthread_func_1 (PrivInfo* thiz)
{
return_if_fail(thiz != NULL)
while (time(NULL) < thiz->end_time)
{
sem_wait (&thiz->s2)
printf ("pthread1: pthread1 get the lock./n")
sem_post (&thiz->s1)
printf ("pthread1: pthread1 unlock/n")
sleep (1)
}
return
}
static void* pthread_func_2 (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
while (time (NULL) < thiz->end_time)
{
sem_wait (&thiz->s1)
printf ("pthread2: pthread2 get the unlock./n")
sem_post (&thiz->s2)
printf ("pthread2: pthread2 unlock./n")
sleep (1)
}
return
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)