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
}
多线程随机选号程序以下程序运行后看起来比较有意思,像一个随机选号程序,但不是完全按照问题所说的写的 可供参考,要改很容易
//多线程随机选号程序示例
#include <stdio.h>
#include <Windows.h>
#include <ctime>
#include <cstdlib>
#include <process.h>
bool g_run = true//是否运行
void userInput(void*) //监视输入的线程函数
{
while (true)
{
if (getchar()=='\n') //是否输入回车
{
g_run = !g_run//回车运行 回车暂停
}
Sleep(10)//延迟
}
}
int main()
{
srand(time(0))//随机数种子
_beginthread(userInput,0,NULL)//开线程
while (true)
{
if (g_run)
{
system("cls")//清屏
int t = rand() % 1000+ 1//1-1000的随机数
printf("\n %d",t)//输出
}
Sleep(50)//延迟50毫秒
}
return 0
}
因为您传入的是t的地址:rc = pthread_create(&thread[t], NULL, PrintHello, &t)
所以在PrintHello函数中thread_arg = (int)(*((int*)args))读取时会读取到t的最新值,即8.
这是因为主线程一直在执行,for( t = 0t <NUM_THREADSt++ )很快执行完,跳出循环t的值为8,而在PrintHello中sleep(1)肯定会读取到t的最新值8.
直接传t的值即可,如下:
#include<stdio.h>#include <stdio.h>
#include <stdlib.h>
#include <stdint.h> // 加入这个头文件
#include <pthread.h>
#include <unistd.h>
#define NUM_THREADS 8
void *PrintHello(void *args)
{
int thread_arg
sleep(1)
thread_arg = (int)(uintptr_t)args //这样读取传来的t的值
printf("Hello from thread %d\n", thread_arg)
return NULL
}
int main(void)
{
int rc,t
pthread_t thread[NUM_THREADS]
for( t = 0 t < NUM_THREADS t++ )
{
printf("Creating thread %d\n", t)
rc = pthread_create(&thread[t], NULL, PrintHello, (void *)(uintptr_t)t) // 这样传t的值
if (rc)
{
printf("ERROR return code is %d\n", rc)
return EXIT_FAILURE
}
}
sleep(5)
for( t = 0 t < NUM_THREADS t++ )
pthread_join(thread[t], NULL)
return EXIT_SUCCESS
}