请教两个关于多线程的程序,哪位大哥帮帮我,在线等
1个回答
展开全部
也不知楼主你要什么语言的,就给你这两个代码吧
/*windows程序*/
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
DWORD WINAPI func1(LPVOID lpParam);
DWORD WINAPI func2(LPVOID lpParam);
LONG lCount=0;
int main(int argc, char* argv[])
{
HANDLE h1,h2;
DWORD dwThreadID;
DWORD dw = 1;
printf("lCount=%d ",lCount);
h1 = CreateThread(NULL, 0, func1, &dw, 0, &dwThreadID);
h2 = CreateThread(NULL, 0, func2, 0, 0, 0);
WaitForSingleObject(h1,INFINITE);//等待
WaitForSingleObject(h2,INFINITE);
printf("lCount=%d\n",lCount);
return 0;
}
DWORD WINAPI func1(LPVOID lpParam)
{
InterlockedIncrement(&lCount);
return 0;
}
DWORD WINAPI func2(LPVOID lpParam)
{
InterlockedIncrement(&lCount);
return 0;
}
/*C/C++程序*/
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
void *thread_function(void* arg);
char message[] = "Hello world!";
int main() {
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, (void*)message);
if (0 != res) {
perror("Thread creation faied");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
if (0 != res) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined, it returned %s\n", (char*)thread_result);
printf("Message is now %s\n",message);
exit(EXIT_FAILURE);
}
void *thread_function(void* arg) {
printf("thread_function is running. argument was %s\n",(char*)arg);
sleep(3);
strcpy(message,"bye!");
pthread_exit("Thank you for the cpu time");
}
/*windows程序*/
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
DWORD WINAPI func1(LPVOID lpParam);
DWORD WINAPI func2(LPVOID lpParam);
LONG lCount=0;
int main(int argc, char* argv[])
{
HANDLE h1,h2;
DWORD dwThreadID;
DWORD dw = 1;
printf("lCount=%d ",lCount);
h1 = CreateThread(NULL, 0, func1, &dw, 0, &dwThreadID);
h2 = CreateThread(NULL, 0, func2, 0, 0, 0);
WaitForSingleObject(h1,INFINITE);//等待
WaitForSingleObject(h2,INFINITE);
printf("lCount=%d\n",lCount);
return 0;
}
DWORD WINAPI func1(LPVOID lpParam)
{
InterlockedIncrement(&lCount);
return 0;
}
DWORD WINAPI func2(LPVOID lpParam)
{
InterlockedIncrement(&lCount);
return 0;
}
/*C/C++程序*/
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
void *thread_function(void* arg);
char message[] = "Hello world!";
int main() {
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, (void*)message);
if (0 != res) {
perror("Thread creation faied");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
if (0 != res) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined, it returned %s\n", (char*)thread_result);
printf("Message is now %s\n",message);
exit(EXIT_FAILURE);
}
void *thread_function(void* arg) {
printf("thread_function is running. argument was %s\n",(char*)arg);
sleep(3);
strcpy(message,"bye!");
pthread_exit("Thank you for the cpu time");
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询