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
}
矩阵的计算我忘记了,你运行看看结果对不对
目录:Linux *** 作系统,C语言实现多线程
Windows *** 作系统,C语言实现多线程
Windows下的多线程(不带停止)
Linux *** 作系统,C语言实现多线程: #include <stdio.h>#include <stdlib.h>
#include <pthread.h>
void * ThreadOne ( void * threadArg )
{
printf ( "线程开始啦,参数是:%s\n" , (char *)threadArg )
return NULL
}
int main ( void )
{
pthread_t ThreadID /* 记录线程标识符 */
void * waitingResult /* 等待线程退出的等待结果 */
int errorCode /* 记录线程的错误代码 */
char * aMessage = "这是线程的参数"
/* 创建并启动线程ThreadOne。若返回值非零,则线程创建失败 */
errorCode = pthread_create( &ThreadID, NULL, ThreadOne, aMessage )
if ( errorCode != 0 )
{
printf ("线程ThreadOne创建失败。错误代码:%d\n", errorCode )
return EXIT_FAILURE
}
/* 等待线程标识符为的ThreadID的线程结束 */
errorCode = pthread_join( ThreadID, &waitingResult )
if ( errorCode != 0 )
{
printf ( "等待线程退出等待失败。错误代码:%d\n" , errorCode )
return EXIT_FAILURE
}
printf( "线程的返回值是%p\n", waitingResult )
return EXIT_SUCCESS
} Windows *** 作系统,C语言实现多线程: #include <stdio.h>
#include <windows.h>
DWORD APIENTRY ThreadOne ( LPVOID threadArg )
{
printf ( "线程开始啦,参数是:%s\n" , (char *)threadArg )
return 0
}
int main ( void )
{
HANDLE hThread /* 记录线程句柄 */
DWORD ThreadID /* 记录线程ID号 */
DWORD waitingResult /* 等待线程退出的等待结果 */
DWORD threadExitCode /* 记录线程的返回值 */
char * aMessage = "这是线程的参数"
/* 创建并启动线程ThreadOne,返回值为线程句柄,赋值给hThread */
hThread = CreateThread ( NULL, 0L, ThreadOne, (LPVOID)aMessage, 0L, &ThreadID )
if ( hThread == NULL )
{
printf ("线程ThreadOne创建失败。错误代码:%lu\n", GetLastError() )
return EXIT_FAILURE
}
/* 等待线程句柄为的hThread线程结束 */
waitingResult = WaitForSingleObject ( hThread, INFINITE )
if ( waitingResult == WAIT_FAILED )
{
printf ( "等待线程退出等待失败。错误代码:%lu\n" , GetLastError() )
return EXIT_FAILURE
}
if ( GetExitCodeThread ( hThread , &threadExitCode ) )
printf ( "线程的返回值是%lu\n", threadExitCode )
else
printf ( "获取线程的返回值获取失败。错误代码:%lu\n" , GetLastError() )
return EXIT_SUCCESS
} Windows下的多线程:(不带停止) #include <stdio.h>
#include <windows.h>
DWORD WINAPI duoxianchen(LPVOID lpParam)
int main(int argc, char *argv[])
{
int num=0
CreateThread(NULL,NULL,duoxianchen,&num,NULL, NULL)
while(1)
{
num++
printf("主线程! %05d\n",nu***eep(40)
}
return 0
}
DWORD WINAPI duoxianchen(LPVOID lpParam)
{
int* a=lpParam
while(1)
{
++*a
printf("副线程! %05d 0x%p\n",*a,a)
Sleep(80)
}
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)