c语言实现多线程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言实现多线程相关的知识,希望对你有一定的参考价值。
c语言实现多线程
目录:
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;
参考技术A
package cn.sdeit.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
public class ReadTxt
private int id;
public String getArticle(String path,int id)
String url = path+id+".txt";
String string = "";
String allString = "";
File file=new File(url);
try
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
while((string=reader.readLine())!=null)
allString += string+"\\r\\n";
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
return allString;
以上是关于c语言实现多线程的主要内容,如果未能解决你的问题,请参考以下文章